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,
35                               2016-09-24
36  Description of Modification: Move constant definitions to the header so that
37                               that their value is known at compile time.
38
39 *****************************************************************************/
40
41// $Log: sc_nbdefs.h,v $
42// Revision 1.7  2011/02/18 20:19:15  acg
43//  Andy Goodrich: updating Copyright notice.
44//
45// Revision 1.6  2011/02/18 20:09:34  acg
46//  Philipp A. Hartmann: added alternative #define for Windows to guard.
47//
48// Revision 1.5  2011/01/20 16:52:20  acg
49//  Andy Goodrich: changes for IEEE 1666 2011.
50//
51// Revision 1.4  2010/02/08 18:35:55  acg
52//  Andy Goodrich: Philipp Hartmann's changes for Solaris and Linux 64.
53//
54// Revision 1.2  2009/05/22 16:06:29  acg
55//  Andy Goodrich: process control updates.
56//
57// Revision 1.1.1.1  2006/12/15 20:20:05  acg
58// SystemC 2.3
59//
60// Revision 1.3  2006/01/13 18:49:32  acg
61// Added $Log command so that CVS check in comments are reproduced in the
62// source.
63//
64
65#ifndef __SYSTEMC_EXT_DT_INT_SC_NBDEFS_HH__
66#define __SYSTEMC_EXT_DT_INT_SC_NBDEFS_HH__
67
68#include <stdint.h>
69
70#include <climits>
71
72namespace sc_dt
73{
74
75// ----------------------------------------------------------------------------
76//  ENUM : sc_numrep
77//
78//  Enumeration of number representations for character string conversion.
79// ----------------------------------------------------------------------------
80
81enum sc_numrep
82{
83    SC_NOBASE = 0,
84    SC_BIN = 2,
85    SC_OCT = 8,
86    SC_DEC = 10,
87    SC_HEX = 16,
88    SC_BIN_US,
89    SC_BIN_SM,
90    SC_OCT_US,
91    SC_OCT_SM,
92    SC_HEX_US,
93    SC_HEX_SM,
94    SC_CSD
95};
96
97
98// Sign of a number:
99#define SC_NEG -1 // Negative number
100#define SC_ZERO 0 // Zero
101#define SC_POS 1 // Positive number
102#define SC_NOSIGN 2 // Uninitialized sc_signed number
103
104typedef unsigned char uchar;
105
106// A small_type number is at least a char. Defining an int is probably
107// better for alignment.
108typedef int small_type;
109
110// Attributes of a byte.
111#define BITS_PER_BYTE 8
112#define BYTE_RADIX 256
113#define BYTE_MASK 255
114
115// LOG2_BITS_PER_BYTE = log2(BITS_PER_BYTE), assuming that
116// BITS_PER_BYTE is a power of 2.
117#define LOG2_BITS_PER_BYTE 3
118
119// Attributes of the unsigned long. These definitions are used mainly in
120// the functions that are aware of the internal representation of
121// digits, e.g., get/set_packed_rep().
122#define BYTES_PER_DIGIT_TYPE 4
123#define BITS_PER_DIGIT_TYPE 32
124
125// Attributes of a digit, i.e., unsigned long less the overflow bits.
126#define BYTES_PER_DIGIT 4
127#define BITS_PER_DIGIT 30
128#define DIGIT_RADIX (1ul << BITS_PER_DIGIT)
129#define DIGIT_MASK (DIGIT_RADIX - 1)
130// Make sure that BYTES_PER_DIGIT = ceil(BITS_PER_DIGIT / BITS_PER_BYTE).
131
132// Similar attributes for the half of a digit. Note that
133// HALF_DIGIT_RADIX is equal to the square root of DIGIT_RADIX. These
134// definitions are used mainly in the multiplication routines.
135#define BITS_PER_HALF_DIGIT (BITS_PER_DIGIT / 2)
136#define HALF_DIGIT_RADIX (1ul << BITS_PER_HALF_DIGIT)
137#define HALF_DIGIT_MASK (HALF_DIGIT_RADIX - 1)
138
139// DIV_CEIL2(x, y) = ceil(x / y). x and y are positive numbers.
140#define DIV_CEIL2(x, y) (((x) - 1) / (y) + 1)
141
142// DIV_CEIL(x) = ceil(x / BITS_PER_DIGIT) = the number of digits to
143// store x bits. x is a positive number.
144#define DIV_CEIL(x) DIV_CEIL2(x, BITS_PER_DIGIT)
145
146#ifdef SC_MAX_NBITS
147static const int MAX_NDIGITS = DIV_CEIL(SC_MAX_NBITS) + 2;
148// Consider a number with x bits another with y bits. The maximum
149// number of bits happens when we multiply them. The result will have
150// (x + y) bits. Assume that x + y <= SC_MAX_NBITS. Then, DIV_CEIL(x) +
151// DIV_CEIL(y) <= DIV_CEIL(SC_MAX_NBITS) + 2. This is the reason for +2
152// above. With this change, MAX_NDIGITS must be enough to hold the
153// result of any operation.
154#endif
155
156// Support for "digit" vectors used to hold the values of sc_signed,
157// sc_unsigned, sc_bv_base,  and sc_lv_base data types. This type is also used
158// in the concatenation support. An sc_digit is currently an unsigned 32-bit
159// quantity. The typedef used is an unsigned int, rather than an unsigned long,
160// since the unsigned long data type varies in size between 32-bit and 64-bit
161// machines.
162
163typedef unsigned int sc_digit; // 32-bit unsigned integer
164
165// Support for the long long type. This type is not in the standard
166// but is usually supported by compilers.
167#if defined(__x86_64__) || defined(__aarch64__)
168typedef long long int64;
169typedef unsigned long long uint64;
170#else
171typedef int64_t int64;
172typedef uint64_t uint64;
173#endif
174
175static const uint64 UINT64_ZERO = 0ULL;
176static const uint64 UINT64_ONE = 1ULL;
177static const uint64 UINT64_32ONES = 0x00000000ffffffffULL;
178
179
180// Bits per ...
181// will be deleted in the future. Use numeric_limits instead
182#define BITS_PER_CHAR 8
183#define BITS_PER_INT (sizeof(int) * BITS_PER_CHAR)
184#define BITS_PER_LONG (sizeof(long) * BITS_PER_CHAR)
185#define BITS_PER_INT64 (sizeof(::sc_dt::int64) * BITS_PER_CHAR)
186#define BITS_PER_UINT (sizeof(unsigned int) * BITS_PER_CHAR)
187#define BITS_PER_ULONG (sizeof(unsigned long) * BITS_PER_CHAR)
188#define BITS_PER_UINT64 (sizeof(::sc_dt::uint64) * BITS_PER_CHAR)
189
190// Digits per ...
191#define DIGITS_PER_CHAR 1
192#define DIGITS_PER_INT ((BITS_PER_INT + 29) / 30)
193#define DIGITS_PER_LONG ((BITS_PER_LONG + 29) / 30)
194#define DIGITS_PER_INT64 ((BITS_PER_INT64 + 29) / 30)
195#define DIGITS_PER_UINT ((BITS_PER_UINT + 29) / 30)
196#define DIGITS_PER_ULONG ((BITS_PER_ULONG + 29) / 30)
197#define DIGITS_PER_UINT64 ((BITS_PER_UINT64 + 29) / 30)
198
199// Above, BITS_PER_X is mainly used for sc_signed, and BITS_PER_UX is
200// mainly used for sc_unsigned.
201
202static const small_type NB_DEFAULT_BASE = SC_DEC;
203
204// For sc_int code:
205
206typedef int64 int_type;
207typedef uint64 uint_type;
208#define SC_INTWIDTH 64
209static const uint64 UINT_ZERO = UINT64_ZERO;
210static const uint64 UINT_ONE = UINT64_ONE;
211
212} // namespace sc_dt
213
214#endif // __SYSTEMC_EXT_DT_INT_SC_NBDEFS_HH__
215