SystemC 3.0.0
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 arithmetic. This file defines all the constants needed.
24
25 Original Author: Ali Dasdan, Synopsys, Inc.
26
27 *****************************************************************************/
28
29/*****************************************************************************
30
31 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
32 changes you are making here.
33
34 Name, Affiliation, Date: Torsten Maehne, Berner Fachhochschule, 2016-09-24
35 Description of Modification: Move constant definitions to the header so that
36 that their value is known at compile time.
37
38 *****************************************************************************/
39
40// $Log: sc_nbdefs.h,v $
41// Revision 1.7 2011/02/18 20:19:15 acg
42// Andy Goodrich: updating Copyright notice.
43//
44// Revision 1.6 2011/02/18 20:09:34 acg
45// Philipp A. Hartmann: added alternative #define for Windows to guard.
46//
47// Revision 1.5 2011/01/20 16:52:20 acg
48// Andy Goodrich: changes for IEEE 1666 2011.
49//
50// Revision 1.4 2010/02/08 18:35:55 acg
51// Andy Goodrich: Philipp Hartmann's changes for Solaris and Linux 64.
52//
53// Revision 1.2 2009/05/22 16:06:29 acg
54// Andy Goodrich: process control updates.
55//
56// Revision 1.1.1.1 2006/12/15 20:20:05 acg
57// SystemC 2.3
58//
59// Revision 1.3 2006/01/13 18:49:32 acg
60// Added $Log command so that CVS check in comments are reproduced in the
61// source.
62//
63
64#ifndef SC_NBDEFS_H
65#define SC_NBDEFS_H
66
67
69
70#include <climits>
71#include <cstdint>
72
73
75#include "sysc/utils/sc_string.h" // For sc_numrep
76
77// Activate support mixed operands for concatenation via the comma operator
78#define SC_DT_MIXED_COMMA_OPERATORS
79
80
81namespace sc_dt
82{
83// BIGINT CONFIGURATIONS
84//
85// One of these three #defines should be defined, but only one:
86//
87// SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_NO_BASE_CLASS:
88// Configure sc_bigint and sc_biguint so that they do not have parent classes. That is,
89// sc_signed is not a parent of sc_bigint, and sc_unsigned is not a parent of sc_biguint.
90//
91// SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_STORAGE:
92// Configure sc_bigint and sc_biguint so they have storage for their values rather than
93// relying on sc_signed and sc_unsigned to provide it.
94//
95// SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE:
96// Configure sc_bigint and sc_biguint so that sc_signed and sc_unsigned provide the storage
97// for their values. This includes the small vector support to eliminate malloc and free
98// for smaller values. (See SC_BASE_VEC_DIGITS below).
99
100// #define SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_NO_BASE_CLASS
101#define SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_STORAGE
102// #define SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE
103
104#if !defined(SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_NO_BASE_CLASS) && \
105 !defined(SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_STORAGE) && \
106 !defined(SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE)
107#error no BIGINT_CONFIG specified!
108#endif
109
110// SC_FREE_DIGIT - this macro is present to allow SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE to
111// dispense with having an m_free boolean value, since it is sufficient to check the value of
112// 'digit' against the address of 'base_vec' to determine if digit should be freed. If we settle on
113// SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE as the configuration to use in the long run, all the
114// instances of SC_FREE_DIGIT may be removed.
115
116#if defined(SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE)
117# define SC_FREE_DIGIT(FLAG)
118#else
119# define SC_FREE_DIGIT(FLAG) { m_free = FLAG; }
120#endif
121
122// SC_BASE_VEC_DIGITS - controls the size of the compile-time buffer contained in sc_signed and
123// sc_unsigned values. This buffer is used in place of a malloc of storage for the object
124// instance's value. The compile-time buffer's size is a trade-off between preventing malloc/free
125// invocations for the storage, and the footprint of sc_signed and sc_unsigned instances.
126
127#define SC_BASE_VEC_DIGITS 8
128
129#if !defined(SC_BASE_VEC_DIGITS)
130#error no SC_BASE_VEC_DIGITS specified!
131#endif
132
133typedef unsigned char uchar;
134
135// A small_type number is at least a char. Defining an int is probably
136// better for alignment.
137typedef int small_type;
138
139// Attributes of a byte.
140#define BITS_PER_BYTE 8
141#define BYTE_RADIX 256
142#define BYTE_MASK 255
143
144// LOG2_BITS_PER_BYTE = log2(BITS_PER_BYTE), assuming that
145// BITS_PER_BYTE is a power of 2.
146#define LOG2_BITS_PER_BYTE 3
147
148// Attributes of the unsigned int. These definitions are used mainly in
149// the functions that are aware of the internal representation of
150// digits, e.g., get/set_packed_rep().
151#define BYTES_PER_DIGIT_TYPE 4
152#define BITS_PER_DIGIT_TYPE 32
153
154// Support for "digit" vectors used to hold the values of sc_signed,
155// sc_unsigned, sc_bv_base, and sc_lv_base data types. This type is also used
156// in the concatenation support. An sc_digit is currently an unsigned 32-bit
157// quantity. The typedef used is an unsigned int, rather than an unsigned long,
158// since the unsigned long data type varies in size between 32-bit and 64-bit
159// machines.
160
161typedef unsigned int sc_digit; // type holding "digits" in big values.
162
163#define BYTES_PER_DIGIT (std::numeric_limits<sc_digit>::digits/8)
164#define BITS_PER_DIGIT 32
165
166
167#define DIGIT_RADIX ((sc_carry)1 << BITS_PER_DIGIT)
168#define DIGIT_MASK (DIGIT_RADIX - 1)
169
170// If the number of valid bits in an sc_digit is the same as the number of
171// bits in the sc_digit optimize for that fact.
172//
173// SC_BIT_INDEX - return the index of this bit within its sc_digit.
174// SC_DIGIT_INDEX - return the index within an sc_digit vector of the
175// supplied bit index.
176// SC_MASK_DIGIT - mask the supplied sc_digit, keeping the valid bits
177// within an sc_digit.
178// SC_BIT_MASK - this is the mask for digits below the supplied bit
179// SC_BIT_MASK1 - this is the mask for digits below, and including
180// the supplied bit
181// SC_DIGIT_COUNT - this is the number of digits required to accommodate
182// the supplied number of bits.
183
184#if BITS_PER_DIGIT == 32
185# define SC_BIT_INDEX(BIT) ( (BIT)&(std::numeric_limits<sc_digit>::digits-1) )
186# define SC_DIGIT_INDEX(BIT_INDEX) ((BIT_INDEX)>>5)
187# define SC_MASK_DIGIT(v) (v)
188# define SC_DIGIT_COUNT(BIT_WIDTH) ((BIT_WIDTH+BITS_PER_DIGIT-1)/BITS_PER_DIGIT)
189#else
190# define SC_BIT_INDEX(BIT) ((BIT)%BITS_PER_DIGIT)
191# define SC_DIGIT_INDEX(BIT) ((BIT)/BITS_PER_DIGIT)
192# define SC_MASK_DIGIT(v) ((v) & DIGIT_MASK)
193# define SC_DIGIT_COUNT(BIT) (SC_DIGIT_INDEX(BIT)+1)
194#endif
195#define SC_BIT_MASK(BITS) ~( (std::numeric_limits<sc_digit>::max() ) << SC_BIT_INDEX(BITS) )
196#define SC_BIT_MASK1(BITS) ~( (std::numeric_limits<sc_digit>::max()-1) << SC_BIT_INDEX(BITS) )
197
198// Make sure that BYTES_PER_DIGIT = ceil(BITS_PER_DIGIT / BITS_PER_BYTE).
199
200// Similar attributes for the half of a digit. Note that
201// HALF_DIGIT_RADIX is equal to the square root of DIGIT_RADIX. These
202// definitions are used mainly in the multiplication routines.
203#define BITS_PER_HALF_DIGIT (BITS_PER_DIGIT / 2)
204#define HALF_DIGIT_RADIX (1ul << BITS_PER_HALF_DIGIT)
205#define HALF_DIGIT_MASK (HALF_DIGIT_RADIX - 1)
206
207// DIV_CEIL2(x, y) = ceil(x / y). x and y are positive numbers.
208#define DIV_CEIL2(x, y) (((x) - 1) / (y) + 1)
209
210// DIV_CEIL(x) = ceil(x / BITS_PER_DIGIT) = the number of digits to
211// store x bits. x is a positive number.
212#define DIV_CEIL(x) DIV_CEIL2(x, BITS_PER_DIGIT)
213
214
215using int64 = long long;
216using uint64 = unsigned long long;
217
218constexpr uint64 UINT64_ZERO = 0ULL;
219constexpr uint64 UINT64_ONE = 1ULL;
220constexpr uint64 UINT64_32ONES = 0x00000000ffffffffULL;
221
222#if BITS_PER_DIGIT < 32
223 typedef unsigned int sc_carry; // type of carry temporaries.
224#else
225 typedef uint64 sc_carry; // type of carry temporaries.
226#endif
227
228// Bits per ...
229// will be deleted in the future. Use numeric_limits instead
230#define BITS_PER_CHAR 8
231#define BITS_PER_INT (sizeof(int) * BITS_PER_CHAR)
232#define BITS_PER_LONG (sizeof(long) * BITS_PER_CHAR)
233#define BITS_PER_INT64 (sizeof(::sc_dt::int64) * BITS_PER_CHAR)
234#define BITS_PER_UINT (sizeof(unsigned int) * BITS_PER_CHAR)
235#define BITS_PER_ULONG (sizeof(unsigned long) * BITS_PER_CHAR)
236#define BITS_PER_UINT64 (sizeof(::sc_dt::uint64) * BITS_PER_CHAR)
237
238// Digits per ...
239#define DIGITS_PER_CHAR 1
240#define DIGITS_PER_INT ((BITS_PER_INT+(BITS_PER_DIGIT-1))/BITS_PER_DIGIT)
241#define DIGITS_PER_LONG ((BITS_PER_LONG+(BITS_PER_DIGIT-1))/BITS_PER_DIGIT)
242#define DIGITS_PER_INT64 ((BITS_PER_INT64+(BITS_PER_DIGIT-1))/BITS_PER_DIGIT)
243#define DIGITS_PER_UINT ((BITS_PER_UINT+(BITS_PER_DIGIT-1))/BITS_PER_DIGIT)
244#define DIGITS_PER_ULONG ((BITS_PER_ULONG+(BITS_PER_DIGIT-1))/BITS_PER_DIGIT)
245#define DIGITS_PER_UINT64 ((BITS_PER_UINT64+(BITS_PER_DIGIT-1))/BITS_PER_DIGIT)
246
247// Above, BITS_PER_X is mainly used for sc_signed, and BITS_PER_UX is
248// mainly used for sc_unsigned.
249
251
253
254// For sc_int code:
255#define LLWIDTH BITS_PER_INT64
256#define INTWIDTH BITS_PER_INT
257
260#define SC_INTWIDTH 64
263
264// Table providing the number of bits on in a byte, used in sc_vector_utils.h.
265constexpr int byte_one_bits[256] = {
266 0, // 0x0
267 1, // 0x1
268 1, // 0x2
269 2, // 0x3
270 1, // 0x4
271 2, // 0x5
272 2, // 0x6
273 3, // 0x7
274 1, // 0x8
275 2, // 0x9
276 2, // 0xa
277 3, // 0xb
278 2, // 0xc
279 3, // 0xd
280 3, // 0xe
281 4, // 0xf
282 1, // 0x10
283 2, // 0x11
284 2, // 0x12
285 3, // 0x13
286 2, // 0x14
287 3, // 0x15
288 3, // 0x16
289 4, // 0x17
290 2, // 0x18
291 3, // 0x19
292 3, // 0x1a
293 4, // 0x1b
294 3, // 0x1c
295 4, // 0x1d
296 4, // 0x1e
297 5, // 0x1f
298 1, // 0x20
299 2, // 0x21
300 2, // 0x22
301 3, // 0x23
302 2, // 0x24
303 3, // 0x25
304 3, // 0x26
305 4, // 0x27
306 2, // 0x28
307 3, // 0x29
308 3, // 0x2a
309 4, // 0x2b
310 3, // 0x2c
311 4, // 0x2d
312 4, // 0x2e
313 5, // 0x2f
314 2, // 0x30
315 3, // 0x31
316 3, // 0x32
317 4, // 0x33
318 3, // 0x34
319 4, // 0x35
320 4, // 0x36
321 5, // 0x37
322 3, // 0x38
323 4, // 0x39
324 4, // 0x3a
325 5, // 0x3b
326 4, // 0x3c
327 5, // 0x3d
328 5, // 0x3e
329 6, // 0x3f
330 1, // 0x40
331 2, // 0x41
332 2, // 0x42
333 3, // 0x43
334 2, // 0x44
335 3, // 0x45
336 3, // 0x46
337 4, // 0x47
338 2, // 0x48
339 3, // 0x49
340 3, // 0x4a
341 4, // 0x4b
342 3, // 0x4c
343 4, // 0x4d
344 4, // 0x4e
345 5, // 0x4f
346 2, // 0x50
347 3, // 0x51
348 3, // 0x52
349 4, // 0x53
350 3, // 0x54
351 4, // 0x55
352 4, // 0x56
353 5, // 0x57
354 3, // 0x58
355 4, // 0x59
356 4, // 0x5a
357 5, // 0x5b
358 4, // 0x5c
359 5, // 0x5d
360 5, // 0x5e
361 6, // 0x5f
362 2, // 0x60
363 3, // 0x61
364 3, // 0x62
365 4, // 0x63
366 3, // 0x64
367 4, // 0x65
368 4, // 0x66
369 5, // 0x67
370 3, // 0x68
371 4, // 0x69
372 4, // 0x6a
373 5, // 0x6b
374 4, // 0x6c
375 5, // 0x6d
376 5, // 0x6e
377 6, // 0x6f
378 3, // 0x70
379 4, // 0x71
380 4, // 0x72
381 5, // 0x73
382 4, // 0x74
383 5, // 0x75
384 5, // 0x76
385 6, // 0x77
386 4, // 0x78
387 5, // 0x79
388 5, // 0x7a
389 6, // 0x7b
390 5, // 0x7c
391 6, // 0x7d
392 6, // 0x7e
393 7, // 0x7f
394 1, // 0x80
395 2, // 0x81
396 2, // 0x82
397 3, // 0x83
398 2, // 0x84
399 3, // 0x85
400 3, // 0x86
401 4, // 0x87
402 2, // 0x88
403 3, // 0x89
404 3, // 0x8a
405 4, // 0x8b
406 3, // 0x8c
407 4, // 0x8d
408 4, // 0x8e
409 5, // 0x8f
410 2, // 0x90
411 3, // 0x91
412 3, // 0x92
413 4, // 0x93
414 3, // 0x94
415 4, // 0x95
416 4, // 0x96
417 5, // 0x97
418 3, // 0x98
419 4, // 0x99
420 4, // 0x9a
421 5, // 0x9b
422 4, // 0x9c
423 5, // 0x9d
424 5, // 0x9e
425 6, // 0x9f
426 2, // 0xa0
427 3, // 0xa1
428 3, // 0xa2
429 4, // 0xa3
430 3, // 0xa4
431 4, // 0xa5
432 4, // 0xa6
433 5, // 0xa7
434 3, // 0xa8
435 4, // 0xa9
436 4, // 0xaa
437 5, // 0xab
438 4, // 0xac
439 5, // 0xad
440 5, // 0xae
441 6, // 0xaf
442 3, // 0xb0
443 4, // 0xb1
444 4, // 0xb2
445 5, // 0xb3
446 4, // 0xb4
447 5, // 0xb5
448 5, // 0xb6
449 6, // 0xb7
450 4, // 0xb8
451 5, // 0xb9
452 5, // 0xba
453 6, // 0xbb
454 5, // 0xbc
455 6, // 0xbd
456 6, // 0xbe
457 7, // 0xbf
458 2, // 0xc0
459 3, // 0xc1
460 3, // 0xc2
461 4, // 0xc3
462 3, // 0xc4
463 4, // 0xc5
464 4, // 0xc6
465 5, // 0xc7
466 3, // 0xc8
467 4, // 0xc9
468 4, // 0xca
469 5, // 0xcb
470 4, // 0xcc
471 5, // 0xcd
472 5, // 0xce
473 6, // 0xcf
474 3, // 0xd0
475 4, // 0xd1
476 4, // 0xd2
477 5, // 0xd3
478 4, // 0xd4
479 5, // 0xd5
480 5, // 0xd6
481 6, // 0xd7
482 4, // 0xd8
483 5, // 0xd9
484 5, // 0xda
485 6, // 0xdb
486 5, // 0xdc
487 6, // 0xdd
488 6, // 0xde
489 7, // 0xdf
490 3, // 0xe0
491 4, // 0xe1
492 4, // 0xe2
493 5, // 0xe3
494 4, // 0xe4
495 5, // 0xe5
496 5, // 0xe6
497 6, // 0xe7
498 4, // 0xe8
499 5, // 0xe9
500 5, // 0xea
501 6, // 0xeb
502 5, // 0xec
503 6, // 0xed
504 6, // 0xee
505 7, // 0xef
506 4, // 0xf0
507 5, // 0xf1
508 5, // 0xf2
509 6, // 0xf3
510 5, // 0xf4
511 6, // 0xf5
512 6, // 0xf6
513 7, // 0xf7
514 5, // 0xf8
515 6, // 0xf9
516 6, // 0xfa
517 7, // 0xfb
518 6, // 0xfc
519 7, // 0xfd
520 7, // 0xfe
521 8, // 0xff
522};
523
524} // namespace sc_dt
525
526
527#endif
int small_type
Definition: sc_nbdefs.h:137
@ SC_DEC
Definition: sc_string.h:60
constexpr uint64 UINT_ONE
Definition: sc_nbdefs.h:262
constexpr uint64 UINT64_32ONES
Definition: sc_nbdefs.h:220
unsigned long long uint64
Definition: sc_nbdefs.h:216
constexpr int byte_one_bits[256]
Definition: sc_nbdefs.h:265
constexpr uint64 UINT64_ZERO
Definition: sc_nbdefs.h:218
int64 int_type
Definition: sc_nbdefs.h:258
constexpr small_type NB_DEFAULT_BASE
Definition: sc_nbdefs.h:252
constexpr uint64 UINT_ZERO
Definition: sc_nbdefs.h:261
uint64 uint_type
Definition: sc_nbdefs.h:259
unsigned int sc_digit
Definition: sc_nbdefs.h:161
unsigned char uchar
Definition: sc_nbdefs.h:133
uint64 sc_carry
Definition: sc_nbdefs.h:225
constexpr uint64 UINT64_ONE
Definition: sc_nbdefs.h:219
long long int64
Definition: sc_nbdefs.h:215
::std::ios::fmtflags fmtflags
Definition: sc_nbdefs.h:250