sc_nbdefs.hh revision 12837
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  sc_nbdefs.h -- Top level header file for arbitrary precision signed/unsigned
22                 arithmetic. This file defines all the constants needed.
23 *****************************************************************************/
24
25#ifndef __SYSTEMC_DT_SC_NBDEFS_H__
26#define __SYSTEMC_DT_SC_NBDEFS_H__
27
28#include <stdint.h>
29
30#include <climits>
31
32namespace sc_dt
33{
34
35// ----------------------------------------------------------------------------
36//  ENUM : sc_numrep
37//
38//  Enumeration of number representations for character string conversion.
39// ----------------------------------------------------------------------------
40
41enum sc_numrep
42{
43    SC_NOBASE = 0,
44    SC_BIN = 2,
45    SC_OCT = 8,
46    SC_DEC = 10,
47    SC_HEX = 16,
48    SC_BIN_US,
49    SC_BIN_SM,
50    SC_OCT_US,
51    SC_OCT_SM,
52    SC_HEX_US,
53    SC_HEX_SM,
54    SC_CSD
55};
56
57
58// Sign of a number:
59#define SC_NEG -1 // Negative number
60#define SC_ZERO 0 // Zero
61#define SC_POS 1 // Positive number
62#define SC_NOSIGN 2 // Uninitialized sc_signed number
63
64typedef unsigned char uchar;
65
66// A small_type number is at least a char. Defining an int is probably
67// better for alignment.
68typedef int small_type;
69
70// Attributes of a byte.
71#define BITS_PER_BYTE 8
72#define BYTE_RADIX 256
73#define BYTE_MASK 255
74
75// LOG2_BITS_PER_BYTE = log2(BITS_PER_BYTE), assuming that
76// BITS_PER_BYTE is a power of 2.
77#define LOG2_BITS_PER_BYTE 3
78
79// Attributes of the unsigned long. These definitions are used mainly in
80// the functions that are aware of the internal representation of digits,
81// e.g., get/set_packed_rep().
82#define BYTES_PER_DIGIT_TYPE 4
83#define BITS_PER_DIGIT_TYPE 32
84
85// Attributes of a digit, i.e., unsigned long less the overflow bits.
86#define BYTES_PER_DIGIT 4
87#define BITS_PER_DIGIT 30
88#define DIGIT_RADIX (1ul << BITS_PER_DIGIT)
89#define DIGIT_MASK (DIGIT_RADIX - 1)
90// Make sure that BYTES_PER_DIGIT = ceil(BITS_PER_DIGIT / BITS_PER_BYTE).
91
92// Similar attributes for the half of a digit. Note that
93// HALF_DIGIT_RADIX is equal to the square root of DIGIT_RADIX. These
94// definitions are used mainly in the multiplication routines.
95#define BITS_PER_HALF_DIGIT (BITS_PER_DIGIT / 2)
96#define HALF_DIGIT_RADIX (1ul << BITS_PER_HALF_DIGIT)
97#define HALF_DIGIT_MASK (HALF_DIGIT_RADIX - 1)
98
99// DIV_CEIL2(x, y) = ceil(x / y). x and y are positive numbers.
100#define DIV_CEIL2(x, y) (((x) - 1) / (y) + 1)
101
102// DIV_CEIL(x) = ceil(x / BITS_PER_DIGIT) = the number of digits to
103// store x bits. x is a positive number.
104#define DIV_CEIL(x) DIV_CEIL2(x, BITS_PER_DIGIT)
105
106#ifdef SC_MAX_NBITS
107static const int MAX_NDIGITS = DIV_CEIL(SC_MAX_NBITS) + 2;
108// Consider a number with x bits another with y bits. The maximum
109// number of bits happens when we multiply them. The result will have
110// (x + y) bits. Assume that x + y <= SC_MAX_NBITS. Then, DIV_CEIL(x) +
111// DIV_CEIL(y) <= DIV_CEIL(SC_MAX_NBITS) + 2. This is the reason for +2
112// above. With this change, MAX_NDIGITS must be enough to hold the
113// result of any operation.
114#endif
115
116// Support for "digit" vectors used to hold the values of sc_signed,
117// sc_unsigned, sc_bv_base,  and sc_lv_base data types. This type is also used
118// in the concatenation support. An sc_digit is currently an unsigned 32-bit
119// quantity. The typedef used is an unsigned int, rather than an unsigned long,
120// since the unsigned long data type varies in size between 32-bit and 64-bit
121// machines.
122
123typedef unsigned int sc_digit;	// 32-bit unsigned integer
124
125// Support for the long long type. This type is not in the standard
126// but is usually supported by compilers.
127typedef int64_t int64;
128typedef uint64_t uint64;
129
130static const uint64 UINT64_ZERO   = 0ULL;
131static const uint64 UINT64_ONE    = 1ULL;
132static const uint64 UINT64_32ONES = 0x00000000ffffffffULL;
133
134// Bits per ...
135// will be deleted in the future. Use numeric_limits instead
136#define BITS_PER_CHAR    8
137#define BITS_PER_INT    (sizeof(int) * BITS_PER_CHAR)
138#define BITS_PER_LONG   (sizeof(long) * BITS_PER_CHAR)
139#define BITS_PER_INT64  (sizeof(::sc_dt::int64) * BITS_PER_CHAR)
140#define BITS_PER_UINT   (sizeof(unsigned int) * BITS_PER_CHAR)
141#define BITS_PER_ULONG  (sizeof(unsigned long) * BITS_PER_CHAR)
142#define BITS_PER_UINT64 (sizeof(::sc_dt::uint64) * BITS_PER_CHAR)
143
144// Digits per ...
145#define DIGITS_PER_CHAR   1
146#define DIGITS_PER_INT    ((BITS_PER_INT+29)/30)
147#define DIGITS_PER_LONG   ((BITS_PER_LONG+29)/30)
148#define DIGITS_PER_INT64  ((BITS_PER_INT64+29)/30)
149#define DIGITS_PER_UINT   ((BITS_PER_UINT+29)/30)
150#define DIGITS_PER_ULONG  ((BITS_PER_ULONG+29)/30)
151#define DIGITS_PER_UINT64 ((BITS_PER_UINT64+29)/30)
152
153// Above, BITS_PER_X is mainly used for sc_signed, and BITS_PER_UX is
154// mainly used for sc_unsigned.
155
156static const small_type NB_DEFAULT_BASE = SC_DEC;
157
158// For sc_int code:
159
160typedef int64 int_type;
161typedef uint64 uint_type;
162#define SC_INTWIDTH 64
163static const uint64 UINT_ZERO = UINT64_ZERO;
164static const uint64 UINT_ONE = UINT64_ONE;
165
166} // namespace sc_dt
167
168#endif
169