SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_signed_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_signed_inlines.h -- sc_signed and sc_bigint<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_SIGNED_INLINES_H
32#define SC_SIGNED_INLINES_H
33
34#include "sc_vector_utils.h"
35
36namespace sc_dt {
37
38// +------------------------------------------------------------------------------------------------
39// |"sc_signed_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_signed::sc_signed"
89// |
90// | These are the object constructors the sc_signed class that are take
91// | 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),
117 ndigits(((nbits+BITS_PER_DIGIT-1)/BITS_PER_DIGIT)), 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();
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();
148 digit = base_vec;
149 SC_FREE_DIGIT(false)
150 *this = v.to_uint64();
151}
152
153inline
155 sc_value_base(v), nbits(), ndigits(), digit()
156{
157 nbits = v.length();
159 if ( ndigits > SC_BASE_VEC_DIGITS ) {
160 digit = new sc_digit[ndigits];
161 SC_FREE_DIGIT(true)
162 } else {
163 digit = base_vec;
164 SC_FREE_DIGIT(false)
165 }
166 int low_bit;
167 bool reversed = false;
168 int high_bit;
169 if ( v.m_left >= v.m_right ) {
170 high_bit = v.m_left;
171 low_bit = v.m_right;
172 }
173 else {
174 reversed = true;
175 low_bit = v.m_left;
176 high_bit = v.m_right;
177 }
178 vector_extract( v.m_obj_p->digit, digit, high_bit, low_bit );
179
180 if ( reversed ) {
181 vector_reverse_bits( digit, high_bit-low_bit, 0 );
182 }
183 adjust_hod();
184}
185
186inline
188 sc_value_base(v), nbits(), ndigits(), digit()
189{
190 nbits = v.length();
192 if ( ndigits > SC_BASE_VEC_DIGITS ) {
193 digit = new sc_digit[ndigits];
194 SC_FREE_DIGIT(true)
195 } else {
196 digit = base_vec;
197 SC_FREE_DIGIT(false)
198 }
199 digit[ndigits-1] = 0;
200 int low_bit;
201 bool reversed = false;
202 int high_bit;
203 if ( v.m_left >= v.m_right ) {
204 high_bit = v.m_left;
205 low_bit = v.m_right;
206 }
207 else {
208 reversed = true;
209 low_bit = v.m_left;
210 high_bit = v.m_right;
211 }
212 vector_extract( v.m_obj_p->digit, digit, high_bit, low_bit );
213
214 if ( reversed ) {
215 vector_reverse_bits( digit, high_bit-low_bit, 0 );
216 }
217 adjust_hod();
218}
219
220inline
222 sc_value_base(), nbits(), ndigits(), digit()
223{
224 int nb = v.length();
225 if( nb > 0 ) {
226 nbits = num_bits( nb );
227 } else {
228 char msg[BUFSIZ];
229 std::snprintf(msg, sizeof(msg), "%s::%s( sc_bv_base ) : nb = %d is not valid",
230 "sc_signed", "sc_signed", nb );
232 }
234 if ( ndigits > SC_BASE_VEC_DIGITS ) {
235 digit = new sc_digit[ndigits];
236 SC_FREE_DIGIT(true)
237 } else {
238 digit = base_vec;
239 SC_FREE_DIGIT(false)
240 }
241 *this = v;
242}
243
244inline
246 sc_value_base(), nbits(), ndigits(), digit()
247{
248 int nb = v.length();
249 if( nb > 0 ) {
250 nbits = num_bits( nb );
251 } else {
252 char msg[BUFSIZ];
253 std::snprintf(msg, sizeof(msg), "%s::%s( sc_lv_base ) : nb = %d is not valid",
254 "sc_signed", "sc_signed", nb );
256 }
258 if ( ndigits > SC_BASE_VEC_DIGITS ) {
259 digit = new sc_digit[ndigits];
260 SC_FREE_DIGIT(true)
261 } else {
262 digit = base_vec;
263 SC_FREE_DIGIT(false)
264 }
265 *this = v;
266}
267
268// +----------------------------------------------------------------------------
269// |"sc_signed::operator ="
270// |
271// | These are the assignment operators for the sc_signed class.
272// |
273// | Arguments:
274// | from = value to be assigned this object instance.
275// | Result:
276// | Constant reference to this object instance.
277// +----------------------------------------------------------------------------
278inline const sc_signed&
280{
282 adjust_hod();
283 return *this;
284}
285
286inline const sc_signed&
288{
290 adjust_hod();
291 return *this;
292}
293
294inline const sc_signed&
296{
297 int to_hod = get_hod();
298 sc_digit* to_p = get_digits();
299 *to_p++ = (sc_digit)from;
300 if ( to_hod > 0 ) {
301 *to_p++ = from >> 32;
302 }
303 if ( to_hod > 1 ) {
304 vector_fill( from < 0 ? -1 : 0, to_hod-2, to_p );
305 }
306 adjust_hod();
307 return *this;
308}
309
310inline const sc_signed&
312{
313 int to_hod = get_hod();
314 sc_digit* to_p = get_digits();
315 *to_p++ = from;
316 if ( sizeof(from) > 4 ) { // 64-bit build.
317 if ( to_hod > 0 ) {
318 *to_p++ = ((int64)from) >> 32; // eliminate 32-bit warnings.
319 }
320 if ( to_hod > 1 ) {
321 vector_fill( from < 0 ? -1 : 0, to_hod-2, to_p );
322 }
323 }
324 else if ( to_hod > 0 ) { // 32-bit build.
325 vector_fill( from < 0 ? -1 : 0, to_hod-1, to_p );
326 }
327 adjust_hod();
328 return *this;
329}
330
331inline const sc_signed&
333{
334 int to_hod = get_hod();
335 sc_digit* to_p = get_digits();
336 *to_p++ = (sc_digit)from;
337 if ( to_hod > 0 ) {
338 *to_p++ = from >> 32;
339 }
340 if ( to_hod > 1 ) {
341 vector_fill( 0, to_hod - 2, to_p );
342 }
343 adjust_hod();
344 return *this;
345}
346
347inline const sc_signed&
348sc_signed::operator = ( unsigned long from )
349{
350 int to_hod = get_hod();
351 sc_digit* to_p = get_digits();
352 *to_p++ = from;
353 if ( sizeof(from) > 4 ) { // 64-bit build.
354 if ( to_hod > 0 ) {
355 *to_p++ = ((uint64)from) >> 32; // eliminate 32-bit warnings.
356 }
357 if ( to_hod > 1 ) {
358 vector_fill( 0, to_hod-2, to_p );
359 }
360 }
361 else if ( to_hod > 0 ) { // 32-bit build.
362 vector_fill( 0, to_hod-1, to_p );
363 }
364 adjust_hod();
365 return *this;
366}
367
368inline const sc_signed&
370{
371 return *this = (int64)from;
372}
373
374inline const sc_signed&
376{
377 return *this = (uint64)from;
378}
379
380inline
381const sc_signed&
383{
384 return operator=(sc_unsigned(v));
385}
386
387
388inline
389const sc_signed&
391{
392 return operator=(sc_unsigned(v));
393}
394
395// +----------------------------------------------------------------------------
396// |"sc_signed::reverse"
397// |
398// | This method reverses the bits in this object instance.
399// +----------------------------------------------------------------------------
400inline
401void
403{
405}
406
407// +----------------------------------------------------------------------------
408// |"sc_signed::to_double"
409// |
410// | This method returns this object instance's value as a double.
411// |
412// | Result:
413// | double value representing this object instance.
414// +----------------------------------------------------------------------------
415inline
416double
418{
419 int hod = ndigits-1;
420
421 double v = 0.0;
422 if ( 0 > (int)digit[hod] ) {
424 vector_copy( ndigits, digit, to_p );
426 for ( ; hod >= 0; --hod ) {
427 v = v * DIGIT_RADIX + to_p[hod];
428 }
429 v = -v;
430 }
431 else {
432 for ( ; hod >= 0; --hod ) {
433 v = v * DIGIT_RADIX + digit[hod];
434 }
435 }
436 return v;
437}
438
439inline sc_signed
441{
442 return operator>>(v.to_int());
443}
444
445inline const sc_signed&
447{
448 return operator>>=(v.to_int());
449}
450
453{
454 return operator<<(v.to_int());
455}
456
457inline const sc_signed&
459{
460 return operator<<=(v.to_int());
461}
462
463} // namespace sc_dt
464
465#endif // SC_SIGNED_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)
sc_digit_heap SC_API sc_temporary_digits
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_twos_complement(const int target_n, sc_digit *target_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 * allocate(size_t digits_n)
Definition: sc_nbutils.h:288
sc_unsigned operator>>(int v) const
sc_unsigned operator<<(int v) const
sc_signed operator>>(int v) const
Definition: sc_signed.h:1074
const sc_signed & operator=(const sc_signed &v)
double to_double() const
sc_signed operator<<(int v) const
Definition: sc_signed.h:1028
const sc_signed & operator>>=(int v)
Definition: sc_signed.h:1121
int get_hod() const
Definition: sc_signed.h:1201
sc_digit * digit
Definition: sc_signed.h:1152
const sc_signed & operator<<=(int v)
Definition: sc_signed.h:1052
sc_digit * get_digits() const
Definition: sc_signed.h:1198
int to_int() const
Definition: sc_signed.h:1497
friend class sc_unsigned
Definition: sc_signed.h:558
int get_digits_n() const
Definition: sc_signed.h:1200
int length() const
Definition: sc_signed.h:807
sc_digit base_vec[SC_BASE_VEC_DIGITS >0?SC_BASE_VEC_DIGITS:1]
Definition: sc_signed.h:1153
sc_signed(int nb=sc_length_param().len())
Definition: sc_signed.h:1626
uint64 to_uint64() const
sc_digit * get_digits() const
Definition: sc_unsigned.h:1186
int get_digits_n() const
Definition: sc_unsigned.h:1188
int to_int() const
Definition: sc_unsigned.h:1582