SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_unsigned_inlines.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_unsigned_inlines.h -- sc_unsigned and sc_biguint<W> inline implementations.
23
24 This file contains inline implementations that require the other "int"
25 headers to have been included before its contents can be resolved.
26
27 Original Author: Andy Goodrich, Cadence Design Systems
28
29 *****************************************************************************/
30
31#ifndef SC_UNSIGNED_INLINES_H
32#define SC_UNSIGNED_INLINES_H
33
34#include "sc_vector_utils.h"
35
36namespace sc_dt {
37
38// +------------------------------------------------------------------------------------------------
39// |"sc_unsigned_subref_r shift operators"
40// |
41// | These are the shift operators for part selections.
42// |
43// | Notes:
44// | (1) There are definitions for shift types of int, sc_signed, and sc_unsigned. All other
45// | native C++ integer types will be matched to the int shift type by the compiler.
46// | Arguments:
47// | shift = amount to shift by.
48// | Result:
49// | sc_unsigned instance representing the shifted value.
50// +------------------------------------------------------------------------------------------------
51inline
53{
54 return ((sc_unsigned)*this)<<(shift);
55}
56
57inline
59{
60 return this->operator<<( v.to_int() );
61}
62
63inline
65{
66 return this->operator<<( v.to_int() );
67}
68
69inline
71{
72 return ((sc_unsigned)*this)>>(shift);
73}
74
75inline
77{
78 return this->operator>>( v.to_int() );
79}
80
81inline
83{
84 return this->operator>>( v.to_int() );
85}
86
87// +----------------------------------------------------------------------------
88// |"sc_unsigned::sc_unsigned"
89// |
90// | These are the object constructors the sc_unsigned class that are
91// | take another object as a constructor.
92// |
93// | Arguments:
94// | v = other object.
95// +----------------------------------------------------------------------------
96
97inline
99 sc_value_base(v), nbits(v.nbits), ndigits(v.ndigits), digit()
100{
101 if ( ndigits > SC_BASE_VEC_DIGITS ) {
102 digit = new sc_digit[ndigits];
103 SC_FREE_DIGIT(true)
104 }
105 else {
106 digit = base_vec;
107 SC_FREE_DIGIT(false)
108 }
109
111}
112
113
114inline
116 sc_value_base(v), nbits(v.nbits+1),
117 ndigits( SC_DIGIT_COUNT(nbits) ), digit()
118{
119 if ( ndigits > SC_BASE_VEC_DIGITS ) {
120 digit = new sc_digit[ndigits];
121 SC_FREE_DIGIT(true)
122 } else {
123 digit = base_vec;
124 SC_FREE_DIGIT(false)
125 }
126
128 adjust_hod();
129}
130
131inline
133 sc_value_base(v), nbits(), ndigits(), digit()
134{
135 nbits = v.length()+1;
137 digit = base_vec;
138 SC_FREE_DIGIT(false)
139 *this = v.to_uint64();
140}
141
142inline
144 sc_value_base(v), nbits(), ndigits(), digit()
145{
146 nbits = v.length() + 1;
148 digit = base_vec;
149 SC_FREE_DIGIT(false)
150 makezero();
151 *this = v.to_uint64();
152}
153
154inline
156 sc_value_base(v), nbits(), ndigits(), digit()
157{
158 int low_bit;
159 bool reversed = false;
160 int high_bit;
161
162 nbits = v.length() + 1;
164 if ( ndigits > SC_BASE_VEC_DIGITS ) {
165 digit = new sc_digit[ndigits];
166 SC_FREE_DIGIT(true)
167 } else {
168 digit = base_vec;
169 SC_FREE_DIGIT(false)
170 }
171 if ( v.m_left >= v.m_right ) {
172 high_bit = v.m_left;
173 low_bit = v.m_right;
174 }
175 else {
176 reversed = true;
177 low_bit = v.m_left;
178 high_bit = v.m_right;
179 }
180 vector_extract( v.m_obj_p->digit, digit, high_bit, low_bit );
181 if ( reversed ) {
182 vector_reverse_bits( digit, high_bit-low_bit, 0 );
183 }
184 adjust_hod();
185 if (0) std::cout << "@@@@ " << nbits << " " << ndigits << " " << low_bit << " " << high_bit << std::endl;
186}
187
188inline
190 sc_value_base(v), nbits(), ndigits(), digit()
191{
192 nbits = v.length() + 1;
194 if ( ndigits > SC_BASE_VEC_DIGITS ) {
195 digit = new sc_digit[ndigits];
196 SC_FREE_DIGIT(true)
197 } else {
198 digit = base_vec;
199 SC_FREE_DIGIT(false)
200 }
201 digit[ndigits-1] = 0;
202 int low_bit;
203 bool reversed = false;
204 int high_bit;
205 if ( v.m_left >= v.m_right ) {
206 high_bit = v.m_left;
207 low_bit = v.m_right;
208 }
209 else {
210 reversed = true;
211 low_bit = v.m_left;
212 high_bit = v.m_right;
213 }
214 vector_extract( v.m_obj_p->digit, digit, high_bit, low_bit );
215
216 if ( reversed ) {
217 vector_reverse_bits( digit, high_bit-low_bit, 0 );
218 }
219}
220
221inline
223 sc_value_base(), nbits(), ndigits(), digit()
224{
225 int nb = v.length();
226 if( nb > 0 ) {
227 nbits = num_bits( nb );
228 } else {
229 char msg[BUFSIZ];
230 std::snprintf( msg, sizeof(msg), "%s::%s( sc_bv_base ) : nb = %d is not valid",
231 "sc_unsigned", "sc_unsigned", nb );
233 }
235 if ( ndigits > SC_BASE_VEC_DIGITS ) {
236 digit = new sc_digit[ndigits];
237 SC_FREE_DIGIT(true)
238 } else {
239 digit = base_vec;
240 SC_FREE_DIGIT(false)
241 }
242 *this = v;
243}
244
245inline
247 sc_value_base(), nbits(), ndigits(), digit()
248{
249 int nb = v.length();
250 if( nb > 0 ) {
251 nbits = num_bits( nb );
252 } else {
253 char msg[BUFSIZ];
254 std::snprintf( msg, sizeof(msg), "%s::%s( sc_lv_base ) : nb = %d is not valid",
255 "sc_unsigned", "sc_unsigned", nb );
257 }
259 if ( ndigits > SC_BASE_VEC_DIGITS ) {
260 digit = new sc_digit[ndigits];
261 SC_FREE_DIGIT(true)
262 } else {
263 digit = base_vec;
264 SC_FREE_DIGIT(false)
265 }
266 *this = v;
267}
268
269// +----------------------------------------------------------------------------
270// |"sc_unsigned::operator ="
271// |
272// | These are the assignment operators for the sc_unsigned class.
273// |
274// | Arguments:
275// | from = value to be assigned this object instance.
276// | Result:
277// | Constant reference to this object instance.
278// +----------------------------------------------------------------------------
279// extern bool print_it;
280inline const sc_unsigned&
282{
284 adjust_hod();
285 return *this;
286}
287
288inline const sc_unsigned&
290{
292 adjust_hod();
293 return *this;
294}
295
296inline const sc_unsigned&
298{
299 int to_hod = get_hod();
300 sc_digit* to_p = get_digits();
301 *to_p++ = (sc_digit)from;
302 if ( to_hod > 0 ) {
303 *to_p++ = from >> BITS_PER_DIGIT;
304 }
305 if ( to_hod > 1 ) {
306 vector_fill( from < 0 ? -1 : 0, to_hod-2, to_p );
307 }
308 adjust_hod();
309 return *this;
310}
311
312inline const sc_unsigned&
314{
315 int to_hod = get_hod();
316 sc_digit* to_p = get_digits();
317 *to_p++ = from;
318 if ( sizeof(long) > 4 ) { // 64-bit system.
319 if ( to_hod > 0 ) {
320 *to_p++ = ((int64)from) >> 32; // eliminate 32-bit warnings.
321 }
322 if ( to_hod > 1 ) {
323 vector_fill( from < 0 ? -1 : 0, to_hod-2, to_p );
324 }
325 }
326 else if ( to_hod > 0 ) { // 32-bit system.
327 vector_fill( from < 0 ? -1 : 0, to_hod-1, to_p );
328 }
329 adjust_hod();
330 return *this;
331}
332
333inline const sc_unsigned&
335{
336 int to_hod = get_hod();
337 sc_digit* to_p = get_digits();
338 *to_p++ = (sc_digit)from;
339 if ( to_hod > 0 ) {
340 *to_p++ = from >> 32;
341 }
342 if ( to_hod > 1 ) {
343 vector_fill( 0, to_hod - 2, to_p );
344 }
345 adjust_hod();
346 return *this;
347}
348
349inline const sc_unsigned&
350sc_unsigned::operator = ( unsigned long from )
351{
352 int to_hod = get_hod();
353 sc_digit* to_p = get_digits();
354 *to_p++ = from;
355 if ( sizeof(long) > 4 ) { // 64-bit system.
356 if ( to_hod > 0 ) {
357 *to_p++ = ((uint64)from) >> 32; // eliminate 32-bit warnings.
358 }
359 if ( to_hod > 1 ) {
360 vector_fill( 0, to_hod-2, to_p );
361 }
362 }
363 else if ( to_hod > 0 ) { // 32-bit system.
364 vector_fill( 0, to_hod-1, to_p );
365 }
366 adjust_hod();
367 return *this;
368}
369
370inline const sc_unsigned&
372{
373 return *this = (int64)from;
374}
375
376inline const sc_unsigned&
378{
379 return *this = (uint64)from;
380}
381
382inline
383const sc_unsigned&
385{
386 return operator=(sc_unsigned(v));
387}
388
389
390inline
391const sc_unsigned&
393{
394 return operator=(sc_unsigned(v));
395}
396
397// +----------------------------------------------------------------------------
398// |"sc_unsigned::reverse"
399// |
400// | This method reverses the bits in this object instance.
401// +----------------------------------------------------------------------------
402inline
403void
405{
407}
408
409// +----------------------------------------------------------------------------
410// |"sc_unsigned::to_double"
411// |
412// | This method returns this object instance's value as a double.
413// |
414// | Result:
415// | double value representing this object instance.
416// +----------------------------------------------------------------------------
417inline
418double
420{
421 int hod = ndigits-1;
422
423 double v = 0.0;
424 for ( ; hod >= 0; --hod ) {
425 v = v * DIGIT_RADIX + digit[hod];
426 }
427
428 return v;
429}
430
431inline sc_unsigned
433{
434 return operator>>(v.to_int());
435}
436
437inline const sc_unsigned&
439{
440 return operator>>=(v.to_int());
441}
442
445{
446 return operator<<(v.to_int());
447}
448
449inline const sc_unsigned&
451{
452 return operator<<=(v.to_int());
453}
454
455
456} // namespace sc_dt
457
458#endif // SC_UNSIGNED_INLINES_H
#define DIGIT_RADIX
Definition: sc_nbdefs.h:167
#define DIV_CEIL(x)
Definition: sc_nbdefs.h:212
#define SC_DIGIT_COUNT(BIT_WIDTH)
Definition: sc_nbdefs.h:188
#define BITS_PER_DIGIT
Definition: sc_nbdefs.h:164
#define SC_FREE_DIGIT(FLAG)
Definition: sc_nbdefs.h:119
#define SC_BASE_VEC_DIGITS
Definition: sc_nbdefs.h:127
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report.h:217
const char SC_ID_INIT_FAILED_[]
void vector_fill(const sc_digit fill, const int to_hod, sc_digit *to_p)
unsigned long long uint64
Definition: sc_nbdefs.h:216
void vector_extract(const sc_digit *source_p, sc_digit *destination_p, const int high_bit, const int low_bit)
void vector_copy(const int from_n, const sc_digit *from_p, const int to_n, sc_digit *to_p)
void vector_reverse_bits(sc_digit *target_p, int high_i, int low_i)
unsigned int sc_digit
Definition: sc_nbdefs.h:161
long long int64
Definition: sc_nbdefs.h:215
int length() const
Definition: sc_bv_base.h:194
int length() const
Definition: sc_lv_base.h:203
uint64 to_uint64() const
Definition: sc_int_base.h:1114
sc_digit * digit
Definition: sc_signed.h:1152
sc_digit * get_digits() const
Definition: sc_signed.h:1198
int to_int() const
Definition: sc_signed.h:1497
int get_digits_n() const
Definition: sc_signed.h:1200
uint64 to_uint64() const
sc_unsigned operator>>(int v) const
sc_unsigned operator<<(int v) const
double to_double() const
sc_digit base_vec[SC_BASE_VEC_DIGITS >0?SC_BASE_VEC_DIGITS:1]
Definition: sc_unsigned.h:1141
sc_digit * get_digits() const
Definition: sc_unsigned.h:1186
int get_hod() const
Definition: sc_unsigned.h:1189
const sc_unsigned & operator=(const sc_unsigned &v)
sc_unsigned operator<<(int v) const
Definition: sc_unsigned.h:1021
int get_digits_n() const
Definition: sc_unsigned.h:1188
const sc_unsigned & operator>>=(int v)
Definition: sc_unsigned.h:1112
const sc_unsigned & operator<<=(int v)
Definition: sc_unsigned.h:1045
int to_int() const
Definition: sc_unsigned.h:1582
sc_unsigned(int nb=sc_length_param().len())
Definition: sc_unsigned.h:1670
int length() const
Definition: sc_unsigned.h:808
sc_unsigned operator>>(int v) const
Definition: sc_unsigned.h:1067