OpenCores
URL https://opencores.org/ocsvn/dblclockfft/dblclockfft/trunk

Subversion Repositories dblclockfft

[/] [dblclockfft/] [trunk/] [bench/] [cpp/] [twoc.cpp] - Blame information for rev 30

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 23 dgisselq
////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    twoc.cpp
4
//
5
// Project:     A Doubletime Pipelined FFT
6
//
7
// Purpose:     Some various two's complement related C++ helper routines.
8
//              Specifically, these help extract signed numbers from
9
//              packed bitfields, while guaranteeing that the upper bits
10
//              are properly sign extended (or not) as desired.
11
//
12
// Creator:     Dan Gisselquist, Ph.D.
13 30 dgisselq
//              Gisselquist Technology, LLC
14 23 dgisselq
//
15
///////////////////////////////////////////////////////////////////////////
16
//
17
// Copyright (C) 2015, Gisselquist Technology, LLC
18
//
19
// This program is free software (firmware): you can redistribute it and/or
20
// modify it under the terms of  the GNU General Public License as published
21
// by the Free Software Foundation, either version 3 of the License, or (at
22
// your option) any later version.
23
//
24
// This program is distributed in the hope that it will be useful, but WITHOUT
25
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
26
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
// for more details.
28
//
29
// You should have received a copy of the GNU General Public License along
30
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
31
// target there if the PDF file isn't present.)  If not, see
32
// <http://www.gnu.org/licenses/> for a copy.
33
//
34
// License:     GPL, v3, as defined and found on www.gnu.org,
35
//              http://www.gnu.org/licenses/gpl.html
36
//
37
//
38
///////////////////////////////////////////////////////////////////////////
39 29 dgisselq
#include <stdio.h>
40
#include <assert.h>
41 23 dgisselq
#include "twoc.h"
42
 
43
long    sbits(const long val, const int bits) {
44
        long    r;
45
 
46
        r = val & ((1l<<bits)-1);
47
        if (r & (1l << (bits-1)))
48
                r |= (-1l << bits);
49
        return r;
50
}
51
 
52
unsigned long   ubits(const long val, const int bits) {
53
        unsigned long r = val & ((1l<<bits)-1);
54
        return r;
55
}
56
 
57 29 dgisselq
unsigned long   rndbits(const long val, const int bits_in, const int bits_out) {
58
        long    s = sbits(val, bits_in); // Signed input value
59
        long    t = s;  // Truncated input value
60
        long    r;      // Result
61 23 dgisselq
 
62 29 dgisselq
        // printf("RNDBITS(%08lx, %d, %d)\n", val, bits_in, bits_out);
63
        // printf("S = %lx\n", s);
64
        // printf("T = %lx\n", t);
65
        // assert(bits_in > bits_out);
66
        if (bits_in == bits_out)
67
                r = s;
68
        else if (bits_in-1 == bits_out) {
69
                t = sbits(val>>1, bits_out);
70
                // printf("TEST! S = %ld, T = %ld\n", s, t);
71
                if (3 == (s&3))
72
                        t = t+1;
73
                r = t;
74
        } else {
75
                // A. 0XXXX.0xxxxx -> 0XXXX
76
                // B. 0XXX0.100000 -> 0XXX0;
77
                // C. 0XXX1.100000 -> 0XXX1+1;
78
                // D. 0XXXX.1zzzzz -> 0XXXX+1;
79
                // E. 1XXXX.0xxxxx -> 1XXXX
80
                // F. 1XXX0.100000 ->  ??? XXX0;
81
                // G. 1XXX1.100000 ->  ??? XXX1+1;
82
                // H. 1XXXX.1zzzzz -> 1XXXX+1;
83
                t = sbits(val>>(bits_in-bits_out), bits_out); // Truncated value
84
                if (0 == ((s >> (bits_in-bits_out-1))&1)) {
85
                        // printf("A\n");
86
                        r = t;
87
                } else if (0 != (s & ((1<<(bits_in-bits_out-1))-1))) {
88
                        // printf("D\n");
89
                        r = t+1;
90
                } else if (t&1) {
91
                        // printf("C\n");
92
                        r = t+1;
93
                } else { // 3 ..?11
94
                        // printf("B\n");
95
                        r = t;
96
                }
97
        } return r;
98
}
99
 
100
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.