SystemC  2.3.2
Accellera SystemC proof-of-concept library
sc_nbdefs.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements. See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License. You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied. See the License for the specific language governing
16  permissions and limitations under the License.
17 
18  *****************************************************************************/
19 
20 /*****************************************************************************
21 
22  sc_nbdefs.h -- Top level header file for arbitrary precision signed/unsigned
23 */
33 /*****************************************************************************
34 
35  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
36  changes you are making here.
37 
38  Name, Affiliation, Date: Torsten Maehne, Berner Fachhochschule, 2016-09-24
39  Description of Modification: Move constant definitions to the header so that
40  that their value is known at compile time.
41 
42  *****************************************************************************/
43 
44 // $Log: sc_nbdefs.h,v $
45 // Revision 1.7 2011/02/18 20:19:15 acg
46 // Andy Goodrich: updating Copyright notice.
47 //
48 // Revision 1.6 2011/02/18 20:09:34 acg
49 // Philipp A. Hartmann: added alternative #define for Windows to guard.
50 //
51 // Revision 1.5 2011/01/20 16:52:20 acg
52 // Andy Goodrich: changes for IEEE 1666 2011.
53 //
54 // Revision 1.4 2010/02/08 18:35:55 acg
55 // Andy Goodrich: Philipp Hartmann's changes for Solaris and Linux 64.
56 //
57 // Revision 1.2 2009/05/22 16:06:29 acg
58 // Andy Goodrich: process control updates.
59 //
60 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
61 // SystemC 2.3
62 //
63 // Revision 1.3 2006/01/13 18:49:32 acg
64 // Added $Log command so that CVS check in comments are reproduced in the
65 // source.
66 //
67 
68 #ifndef SC_NBDEFS_H
69 #define SC_NBDEFS_H
70 
71 
72 #include "sysc/kernel/sc_cmnhdr.h"
73 
74 #include <climits>
75 
76 #if defined(__sun) || defined(__sun__)
77 # include <inttypes.h>
78 #elif !defined(WIN32) && !defined(_WIN32)
79 # include <stdint.h>
80 #endif
81 
82 #include "sysc/kernel/sc_constants.h" // For SC_MAX_NBITS
83 
84 // Activate support mixed operands for concatenation via the comma operator
85 #define SC_DT_MIXED_COMMA_OPERATORS
86 
87 
88 namespace sc_dt
89 {
90 
98 {
99  SC_NOBASE = 0,
100  SC_BIN = 2,
101  SC_OCT = 8,
102  SC_DEC = 10,
103  SC_HEX = 16,
111 };
112 
113 
114 // Sign of a number:
115 #define SC_NEG -1 // Negative number
116 #define SC_ZERO 0 // Zero
117 #define SC_POS 1 // Positive number
118 #define SC_NOSIGN 2 // Uninitialized sc_signed number
119 
120 typedef unsigned char uchar;
121 
122 // A small_type number is at least a char. Defining an int is probably
123 // better for alignment.
124 typedef int small_type;
125 
126 // Attributes of a byte.
127 #define BITS_PER_BYTE 8
128 #define BYTE_RADIX 256
129 #define BYTE_MASK 255
130 
131 // LOG2_BITS_PER_BYTE = log2(BITS_PER_BYTE), assuming that
132 // BITS_PER_BYTE is a power of 2.
133 #define LOG2_BITS_PER_BYTE 3
134 
135 // Attributes of the unsigned long. These definitions are used mainly in
136 // the functions that are aware of the internal representation of
137 // digits, e.g., get/set_packed_rep().
138 #define BYTES_PER_DIGIT_TYPE 4
139 #define BITS_PER_DIGIT_TYPE 32
140 
141 // Attributes of a digit, i.e., unsigned long less the overflow bits.
142 #define BYTES_PER_DIGIT 4
143 #define BITS_PER_DIGIT 30
144 #define DIGIT_RADIX (1ul << BITS_PER_DIGIT)
145 #define DIGIT_MASK (DIGIT_RADIX - 1)
146 // Make sure that BYTES_PER_DIGIT = ceil(BITS_PER_DIGIT / BITS_PER_BYTE).
147 
148 // Similar attributes for the half of a digit. Note that
149 // HALF_DIGIT_RADIX is equal to the square root of DIGIT_RADIX. These
150 // definitions are used mainly in the multiplication routines.
151 #define BITS_PER_HALF_DIGIT (BITS_PER_DIGIT / 2)
152 #define HALF_DIGIT_RADIX (1ul << BITS_PER_HALF_DIGIT)
153 #define HALF_DIGIT_MASK (HALF_DIGIT_RADIX - 1)
154 
155 // DIV_CEIL2(x, y) = ceil(x / y). x and y are positive numbers.
156 #define DIV_CEIL2(x, y) (((x) - 1) / (y) + 1)
157 
158 // DIV_CEIL(x) = ceil(x / BITS_PER_DIGIT) = the number of digits to
159 // store x bits. x is a positive number.
160 #define DIV_CEIL(x) DIV_CEIL2(x, BITS_PER_DIGIT)
161 
162 #ifdef SC_MAX_NBITS
163 static const int MAX_NDIGITS = DIV_CEIL(SC_MAX_NBITS) + 2;
164 // Consider a number with x bits another with y bits. The maximum
165 // number of bits happens when we multiply them. The result will have
166 // (x + y) bits. Assume that x + y <= SC_MAX_NBITS. Then, DIV_CEIL(x) +
167 // DIV_CEIL(y) <= DIV_CEIL(SC_MAX_NBITS) + 2. This is the reason for +2
168 // above. With this change, MAX_NDIGITS must be enough to hold the
169 // result of any operation.
170 #endif
171 
172 // Support for "digit" vectors used to hold the values of sc_signed,
173 // sc_unsigned, sc_bv_base, and sc_lv_base data types. This type is also used
174 // in the concatenation support. An sc_digit is currently an unsigned 32-bit
175 // quantity. The typedef used is an unsigned int, rather than an unsigned long,
176 // since the unsigned long data type varies in size between 32-bit and 64-bit
177 // machines.
178 
179 typedef unsigned int sc_digit; // 32-bit unsigned integer
180 
181 // Support for the long long type. This type is not in the standard
182 // but is usually supported by compilers.
183 #ifndef _WIN32
184 # if defined(__x86_64__) || defined(__aarch64__)
185  typedef long long int64;
186  typedef unsigned long long uint64;
187 # else
188  typedef int64_t int64;
189  typedef uint64_t uint64;
190 # endif
191 #else
192  typedef __int64 int64;
193  typedef unsigned __int64 uint64;
194 #endif
195 
196 #if !defined(_WIN32) || defined(__MINGW32__)
197  static const uint64 UINT64_ZERO = 0ULL;
198  static const uint64 UINT64_ONE = 1ULL;
199  static const uint64 UINT64_32ONES = 0x00000000ffffffffULL;
200 #else
201  static const uint64 UINT64_ZERO = 0i64;
202  static const uint64 UINT64_ONE = 1i64;
203  static const uint64 UINT64_32ONES = 0x00000000ffffffffi64;
204 #endif
205 
206 
207 // Bits per ...
208 // will be deleted in the future. Use numeric_limits instead
209 #define BITS_PER_CHAR 8
210 #define BITS_PER_INT (sizeof(int) * BITS_PER_CHAR)
211 #define BITS_PER_LONG (sizeof(long) * BITS_PER_CHAR)
212 #define BITS_PER_INT64 (sizeof(::sc_dt::int64) * BITS_PER_CHAR)
213 #define BITS_PER_UINT (sizeof(unsigned int) * BITS_PER_CHAR)
214 #define BITS_PER_ULONG (sizeof(unsigned long) * BITS_PER_CHAR)
215 #define BITS_PER_UINT64 (sizeof(::sc_dt::uint64) * BITS_PER_CHAR)
216 
217 // Digits per ...
218 #define DIGITS_PER_CHAR 1
219 #define DIGITS_PER_INT ((BITS_PER_INT+29)/30)
220 #define DIGITS_PER_LONG ((BITS_PER_LONG+29)/30)
221 #define DIGITS_PER_INT64 ((BITS_PER_INT64+29)/30)
222 #define DIGITS_PER_UINT ((BITS_PER_UINT+29)/30)
223 #define DIGITS_PER_ULONG ((BITS_PER_ULONG+29)/30)
224 #define DIGITS_PER_UINT64 ((BITS_PER_UINT64+29)/30)
225 
226 // Above, BITS_PER_X is mainly used for sc_signed, and BITS_PER_UX is
227 // mainly used for sc_unsigned.
228 
229 static const small_type NB_DEFAULT_BASE = SC_DEC;
230 
231 // For sc_int code:
232 
233  typedef int64 int_type;
234  typedef uint64 uint_type;
235 # define SC_INTWIDTH 64
236  static const uint64 UINT_ZERO = UINT64_ZERO;
237  static const uint64 UINT_ONE = UINT64_ONE;
238 
239 } // namespace sc_dt
240 
241 #endif
Default constants whose values may need to be.
int64_t int64
Definition: sc_nbdefs.h:188
int small_type
Definition: sc_nbdefs.h:124
static const uint64 UINT_ONE
Definition: sc_nbdefs.h:237
static const small_type NB_DEFAULT_BASE
Definition: sc_nbdefs.h:229
uint64_t uint64
Definition: sc_nbdefs.h:189
unsigned int sc_digit
Definition: sc_nbdefs.h:179
int64 int_type
Definition: sc_nbdefs.h:233
unsigned char uchar
Definition: sc_nbdefs.h:120
sc_numrep
Enumeration of number representations for character string conversion.
Definition: sc_nbdefs.h:97
static const uint64 UINT64_32ONES
Definition: sc_nbdefs.h:199
uint64 uint_type
Definition: sc_nbdefs.h:234
static const uint64 UINT_ZERO
Definition: sc_nbdefs.h:236
#define DIV_CEIL(x)
Definition: sc_nbdefs.h:160
static const uint64 UINT64_ZERO
Definition: sc_nbdefs.h:197
static const uint64 UINT64_ONE
Definition: sc_nbdefs.h:198