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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [twoc.cpp] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 51 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    twoc.cpp
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
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
//              Gisselquist Technology, LLC
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
//
17
// Copyright (C) 2015-2017, 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
//
40
//
41
#include <stdio.h>
42
#include <assert.h>
43
#include "twoc.h"
44
 
45
long    sbits(const long val, const int bits) {
46
        long    r;
47
 
48
        r = val & ((1l<<bits)-1);
49
        if (r & (1l << (bits-1)))
50
                r |= (-1l << bits);
51
        return r;
52
}
53
 
54
bool    sfits(const long val, const int bits) {
55
        return (sbits(val, bits) == bits);
56
}
57
 
58
unsigned long   ubits(const long val, const int bits) {
59
        unsigned long r = val & ((1l<<bits)-1);
60
        return r;
61
}
62
 
63
unsigned long   rndbits(const long val, const int bits_in, const int bits_out) {
64
        long    s = sbits(val, bits_in); // Signed input value
65
        long    t = s;  // Truncated input value
66
        long    r;      // Result
67
 
68
        // printf("RNDBITS(%08lx, %d, %d)\n", val, bits_in, bits_out);
69
        // printf("S = %lx\n", s);
70
        // printf("T = %lx\n", t);
71
        // assert(bits_in > bits_out);
72
        if (bits_in == bits_out)
73
                r = s;
74
        else if (bits_in-1 == bits_out) {
75
                t = sbits(val>>1, bits_out);
76
                // printf("TEST! S = %ld, T = %ld\n", s, t);
77
                if (3 == (s&3))
78
                        t = t+1;
79
                r = t;
80
        } else {
81
                // A. 0XXXX.0xxxxx -> 0XXXX
82
                // B. 0XXX0.100000 -> 0XXX0;
83
                // C. 0XXX1.100000 -> 0XXX1+1;
84
                // D. 0XXXX.1zzzzz -> 0XXXX+1;
85
                // E. 1XXXX.0xxxxx -> 1XXXX
86
                // F. 1XXX0.100000 ->  ??? XXX0;
87
                // G. 1XXX1.100000 ->  ??? XXX1+1;
88
                // H. 1XXXX.1zzzzz -> 1XXXX+1;
89
                t = sbits(val>>(bits_in-bits_out), bits_out); // Truncated value
90
                if (0 == ((s >> (bits_in-bits_out-1))&1)) {
91
                        // printf("A\n");
92
                        r = t;
93
                } else if (0 != (s & ((1<<(bits_in-bits_out-1))-1))) {
94
                        // printf("D\n");
95
                        r = t+1;
96
                } else if (t&1) {
97
                        // printf("C\n");
98
                        r = t+1;
99
                } else { // 3 ..?11
100
                        // printf("B\n");
101
                        r = t;
102
                }
103
        } return r;
104
}
105
 
106
 

powered by: WebSVN 2.1.0

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