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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zasm/] [twoc.cpp] - Blame information for rev 47

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

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

powered by: WebSVN 2.1.0

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