sc_nbdefs.hh revision 12837
112837Sgabeblack@google.com/***************************************************************************** 212837Sgabeblack@google.com 312837Sgabeblack@google.com Licensed to Accellera Systems Initiative Inc. (Accellera) under one or 412837Sgabeblack@google.com more contributor license agreements. See the NOTICE file distributed 512837Sgabeblack@google.com with this work for additional information regarding copyright ownership. 612837Sgabeblack@google.com Accellera licenses this file to you under the Apache License, Version 2.0 712837Sgabeblack@google.com (the "License"); you may not use this file except in compliance with the 812837Sgabeblack@google.com License. You may obtain a copy of the License at 912837Sgabeblack@google.com 1012837Sgabeblack@google.com http://www.apache.org/licenses/LICENSE-2.0 1112837Sgabeblack@google.com 1212837Sgabeblack@google.com Unless required by applicable law or agreed to in writing, software 1312837Sgabeblack@google.com distributed under the License is distributed on an "AS IS" BASIS, 1412837Sgabeblack@google.com WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 1512837Sgabeblack@google.com implied. See the License for the specific language governing 1612837Sgabeblack@google.com permissions and limitations under the License. 1712837Sgabeblack@google.com 1812837Sgabeblack@google.com *****************************************************************************/ 1912837Sgabeblack@google.com 2012837Sgabeblack@google.com/***************************************************************************** 2112837Sgabeblack@google.com sc_nbdefs.h -- Top level header file for arbitrary precision signed/unsigned 2212837Sgabeblack@google.com arithmetic. This file defines all the constants needed. 2312837Sgabeblack@google.com *****************************************************************************/ 2412837Sgabeblack@google.com 2512837Sgabeblack@google.com#ifndef __SYSTEMC_DT_SC_NBDEFS_H__ 2612837Sgabeblack@google.com#define __SYSTEMC_DT_SC_NBDEFS_H__ 2712837Sgabeblack@google.com 2812837Sgabeblack@google.com#include <stdint.h> 2912837Sgabeblack@google.com 3012837Sgabeblack@google.com#include <climits> 3112837Sgabeblack@google.com 3212837Sgabeblack@google.comnamespace sc_dt 3312837Sgabeblack@google.com{ 3412837Sgabeblack@google.com 3512837Sgabeblack@google.com// ---------------------------------------------------------------------------- 3612837Sgabeblack@google.com// ENUM : sc_numrep 3712837Sgabeblack@google.com// 3812837Sgabeblack@google.com// Enumeration of number representations for character string conversion. 3912837Sgabeblack@google.com// ---------------------------------------------------------------------------- 4012837Sgabeblack@google.com 4112837Sgabeblack@google.comenum sc_numrep 4212837Sgabeblack@google.com{ 4312837Sgabeblack@google.com SC_NOBASE = 0, 4412837Sgabeblack@google.com SC_BIN = 2, 4512837Sgabeblack@google.com SC_OCT = 8, 4612837Sgabeblack@google.com SC_DEC = 10, 4712837Sgabeblack@google.com SC_HEX = 16, 4812837Sgabeblack@google.com SC_BIN_US, 4912837Sgabeblack@google.com SC_BIN_SM, 5012837Sgabeblack@google.com SC_OCT_US, 5112837Sgabeblack@google.com SC_OCT_SM, 5212837Sgabeblack@google.com SC_HEX_US, 5312837Sgabeblack@google.com SC_HEX_SM, 5412837Sgabeblack@google.com SC_CSD 5512837Sgabeblack@google.com}; 5612837Sgabeblack@google.com 5712837Sgabeblack@google.com 5812837Sgabeblack@google.com// Sign of a number: 5912837Sgabeblack@google.com#define SC_NEG -1 // Negative number 6012837Sgabeblack@google.com#define SC_ZERO 0 // Zero 6112837Sgabeblack@google.com#define SC_POS 1 // Positive number 6212837Sgabeblack@google.com#define SC_NOSIGN 2 // Uninitialized sc_signed number 6312837Sgabeblack@google.com 6412837Sgabeblack@google.comtypedef unsigned char uchar; 6512837Sgabeblack@google.com 6612837Sgabeblack@google.com// A small_type number is at least a char. Defining an int is probably 6712837Sgabeblack@google.com// better for alignment. 6812837Sgabeblack@google.comtypedef int small_type; 6912837Sgabeblack@google.com 7012837Sgabeblack@google.com// Attributes of a byte. 7112837Sgabeblack@google.com#define BITS_PER_BYTE 8 7212837Sgabeblack@google.com#define BYTE_RADIX 256 7312837Sgabeblack@google.com#define BYTE_MASK 255 7412837Sgabeblack@google.com 7512837Sgabeblack@google.com// LOG2_BITS_PER_BYTE = log2(BITS_PER_BYTE), assuming that 7612837Sgabeblack@google.com// BITS_PER_BYTE is a power of 2. 7712837Sgabeblack@google.com#define LOG2_BITS_PER_BYTE 3 7812837Sgabeblack@google.com 7912837Sgabeblack@google.com// Attributes of the unsigned long. These definitions are used mainly in 8012837Sgabeblack@google.com// the functions that are aware of the internal representation of digits, 8112837Sgabeblack@google.com// e.g., get/set_packed_rep(). 8212837Sgabeblack@google.com#define BYTES_PER_DIGIT_TYPE 4 8312837Sgabeblack@google.com#define BITS_PER_DIGIT_TYPE 32 8412837Sgabeblack@google.com 8512837Sgabeblack@google.com// Attributes of a digit, i.e., unsigned long less the overflow bits. 8612837Sgabeblack@google.com#define BYTES_PER_DIGIT 4 8712837Sgabeblack@google.com#define BITS_PER_DIGIT 30 8812837Sgabeblack@google.com#define DIGIT_RADIX (1ul << BITS_PER_DIGIT) 8912837Sgabeblack@google.com#define DIGIT_MASK (DIGIT_RADIX - 1) 9012837Sgabeblack@google.com// Make sure that BYTES_PER_DIGIT = ceil(BITS_PER_DIGIT / BITS_PER_BYTE). 9112837Sgabeblack@google.com 9212837Sgabeblack@google.com// Similar attributes for the half of a digit. Note that 9312837Sgabeblack@google.com// HALF_DIGIT_RADIX is equal to the square root of DIGIT_RADIX. These 9412837Sgabeblack@google.com// definitions are used mainly in the multiplication routines. 9512837Sgabeblack@google.com#define BITS_PER_HALF_DIGIT (BITS_PER_DIGIT / 2) 9612837Sgabeblack@google.com#define HALF_DIGIT_RADIX (1ul << BITS_PER_HALF_DIGIT) 9712837Sgabeblack@google.com#define HALF_DIGIT_MASK (HALF_DIGIT_RADIX - 1) 9812837Sgabeblack@google.com 9912837Sgabeblack@google.com// DIV_CEIL2(x, y) = ceil(x / y). x and y are positive numbers. 10012837Sgabeblack@google.com#define DIV_CEIL2(x, y) (((x) - 1) / (y) + 1) 10112837Sgabeblack@google.com 10212837Sgabeblack@google.com// DIV_CEIL(x) = ceil(x / BITS_PER_DIGIT) = the number of digits to 10312837Sgabeblack@google.com// store x bits. x is a positive number. 10412837Sgabeblack@google.com#define DIV_CEIL(x) DIV_CEIL2(x, BITS_PER_DIGIT) 10512837Sgabeblack@google.com 10612837Sgabeblack@google.com#ifdef SC_MAX_NBITS 10712837Sgabeblack@google.comstatic const int MAX_NDIGITS = DIV_CEIL(SC_MAX_NBITS) + 2; 10812837Sgabeblack@google.com// Consider a number with x bits another with y bits. The maximum 10912837Sgabeblack@google.com// number of bits happens when we multiply them. The result will have 11012837Sgabeblack@google.com// (x + y) bits. Assume that x + y <= SC_MAX_NBITS. Then, DIV_CEIL(x) + 11112837Sgabeblack@google.com// DIV_CEIL(y) <= DIV_CEIL(SC_MAX_NBITS) + 2. This is the reason for +2 11212837Sgabeblack@google.com// above. With this change, MAX_NDIGITS must be enough to hold the 11312837Sgabeblack@google.com// result of any operation. 11412837Sgabeblack@google.com#endif 11512837Sgabeblack@google.com 11612837Sgabeblack@google.com// Support for "digit" vectors used to hold the values of sc_signed, 11712837Sgabeblack@google.com// sc_unsigned, sc_bv_base, and sc_lv_base data types. This type is also used 11812837Sgabeblack@google.com// in the concatenation support. An sc_digit is currently an unsigned 32-bit 11912837Sgabeblack@google.com// quantity. The typedef used is an unsigned int, rather than an unsigned long, 12012837Sgabeblack@google.com// since the unsigned long data type varies in size between 32-bit and 64-bit 12112837Sgabeblack@google.com// machines. 12212837Sgabeblack@google.com 12312837Sgabeblack@google.comtypedef unsigned int sc_digit; // 32-bit unsigned integer 12412837Sgabeblack@google.com 12512837Sgabeblack@google.com// Support for the long long type. This type is not in the standard 12612837Sgabeblack@google.com// but is usually supported by compilers. 12712837Sgabeblack@google.comtypedef int64_t int64; 12812837Sgabeblack@google.comtypedef uint64_t uint64; 12912837Sgabeblack@google.com 13012837Sgabeblack@google.comstatic const uint64 UINT64_ZERO = 0ULL; 13112837Sgabeblack@google.comstatic const uint64 UINT64_ONE = 1ULL; 13212837Sgabeblack@google.comstatic const uint64 UINT64_32ONES = 0x00000000ffffffffULL; 13312837Sgabeblack@google.com 13412837Sgabeblack@google.com// Bits per ... 13512837Sgabeblack@google.com// will be deleted in the future. Use numeric_limits instead 13612837Sgabeblack@google.com#define BITS_PER_CHAR 8 13712837Sgabeblack@google.com#define BITS_PER_INT (sizeof(int) * BITS_PER_CHAR) 13812837Sgabeblack@google.com#define BITS_PER_LONG (sizeof(long) * BITS_PER_CHAR) 13912837Sgabeblack@google.com#define BITS_PER_INT64 (sizeof(::sc_dt::int64) * BITS_PER_CHAR) 14012837Sgabeblack@google.com#define BITS_PER_UINT (sizeof(unsigned int) * BITS_PER_CHAR) 14112837Sgabeblack@google.com#define BITS_PER_ULONG (sizeof(unsigned long) * BITS_PER_CHAR) 14212837Sgabeblack@google.com#define BITS_PER_UINT64 (sizeof(::sc_dt::uint64) * BITS_PER_CHAR) 14312837Sgabeblack@google.com 14412837Sgabeblack@google.com// Digits per ... 14512837Sgabeblack@google.com#define DIGITS_PER_CHAR 1 14612837Sgabeblack@google.com#define DIGITS_PER_INT ((BITS_PER_INT+29)/30) 14712837Sgabeblack@google.com#define DIGITS_PER_LONG ((BITS_PER_LONG+29)/30) 14812837Sgabeblack@google.com#define DIGITS_PER_INT64 ((BITS_PER_INT64+29)/30) 14912837Sgabeblack@google.com#define DIGITS_PER_UINT ((BITS_PER_UINT+29)/30) 15012837Sgabeblack@google.com#define DIGITS_PER_ULONG ((BITS_PER_ULONG+29)/30) 15112837Sgabeblack@google.com#define DIGITS_PER_UINT64 ((BITS_PER_UINT64+29)/30) 15212837Sgabeblack@google.com 15312837Sgabeblack@google.com// Above, BITS_PER_X is mainly used for sc_signed, and BITS_PER_UX is 15412837Sgabeblack@google.com// mainly used for sc_unsigned. 15512837Sgabeblack@google.com 15612837Sgabeblack@google.comstatic const small_type NB_DEFAULT_BASE = SC_DEC; 15712837Sgabeblack@google.com 15812837Sgabeblack@google.com// For sc_int code: 15912837Sgabeblack@google.com 16012837Sgabeblack@google.comtypedef int64 int_type; 16112837Sgabeblack@google.comtypedef uint64 uint_type; 16212837Sgabeblack@google.com#define SC_INTWIDTH 64 16312837Sgabeblack@google.comstatic const uint64 UINT_ZERO = UINT64_ZERO; 16412837Sgabeblack@google.comstatic const uint64 UINT_ONE = UINT64_ONE; 16512837Sgabeblack@google.com 16612837Sgabeblack@google.com} // namespace sc_dt 16712837Sgabeblack@google.com 16812837Sgabeblack@google.com#endif 169