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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [twoc.cpp] - Blame information for rev 115

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

Line No. Rev Author Line
1 28 dgisselq
////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    twoc.cpp
4
//
5 46 dgisselq
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6 28 dgisselq
//
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 91 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
18 28 dgisselq
//
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 46 dgisselq
#include <stdio.h>
40
#include <assert.h>
41 28 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 46 dgisselq
bool    sfits(const long val, const int bits) {
53
        return (sbits(val, bits) == bits);
54
}
55
 
56 28 dgisselq
unsigned long   ubits(const long val, const int bits) {
57
        unsigned long r = val & ((1l<<bits)-1);
58
        return r;
59
}
60
 
61 46 dgisselq
unsigned long   rndbits(const long val, const int bits_in, const int bits_out) {
62
        long    s = sbits(val, bits_in); // Signed input value
63
        long    t = s;  // Truncated input value
64
        long    r;      // Result
65 28 dgisselq
 
66 46 dgisselq
        // printf("RNDBITS(%08lx, %d, %d)\n", val, bits_in, bits_out);
67
        // printf("S = %lx\n", s);
68
        // printf("T = %lx\n", t);
69
        // assert(bits_in > bits_out);
70
        if (bits_in == bits_out)
71
                r = s;
72
        else if (bits_in-1 == bits_out) {
73
                t = sbits(val>>1, bits_out);
74
                // printf("TEST! S = %ld, T = %ld\n", s, t);
75
                if (3 == (s&3))
76
                        t = t+1;
77
                r = t;
78
        } else {
79
                // A. 0XXXX.0xxxxx -> 0XXXX
80
                // B. 0XXX0.100000 -> 0XXX0;
81
                // C. 0XXX1.100000 -> 0XXX1+1;
82
                // D. 0XXXX.1zzzzz -> 0XXXX+1;
83
                // E. 1XXXX.0xxxxx -> 1XXXX
84
                // F. 1XXX0.100000 ->  ??? XXX0;
85
                // G. 1XXX1.100000 ->  ??? XXX1+1;
86
                // H. 1XXXX.1zzzzz -> 1XXXX+1;
87
                t = sbits(val>>(bits_in-bits_out), bits_out); // Truncated value
88
                if (0 == ((s >> (bits_in-bits_out-1))&1)) {
89
                        // printf("A\n");
90
                        r = t;
91
                } else if (0 != (s & ((1<<(bits_in-bits_out-1))-1))) {
92
                        // printf("D\n");
93
                        r = t+1;
94
                } else if (t&1) {
95
                        // printf("C\n");
96
                        r = t+1;
97
                } else { // 3 ..?11
98
                        // printf("B\n");
99
                        r = t;
100
                }
101
        } return r;
102
}
103
 
104
 

powered by: WebSVN 2.1.0

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