SystemC  2.3.2
Accellera SystemC proof-of-concept library
sc_nbutils.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_nbutils.h -- External and friend functions for both sc_signed and
23 */
33 /*****************************************************************************
34 
35  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
36  changes you are making here.
37 
38  Name, Affiliation, Date:
39  Description of Modification:
40 
41  *****************************************************************************/
42 
43 // $Log: sc_nbutils.h,v $
44 // Revision 1.6 2011/09/08 16:12:15 acg
45 // Philipp A. Hartmann: fix issue with Sun machines wrt real math libraries.
46 //
47 // Revision 1.5 2011/08/26 23:00:01 acg
48 // Torsten Maehne: remove use of ieeefp.h.
49 //
50 // Revision 1.4 2011/08/15 16:43:24 acg
51 // Torsten Maehne: changes to remove unused argument warnings.
52 //
53 // Revision 1.3 2011/02/18 20:19:15 acg
54 // Andy Goodrich: updating Copyright notice.
55 //
56 // Revision 1.2 2010/09/06 16:35:48 acg
57 // Andy Goodrich: changed i386 to __i386__ in ifdef's.
58 //
59 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
60 // SystemC 2.3
61 //
62 // Revision 1.3 2006/01/13 18:49:32 acg
63 // Added $Log command so that CVS check in comments are reproduced in the
64 // source.
65 //
66 
67 #ifndef SC_NBUTILS_H
68 #define SC_NBUTILS_H
69 
70 #include <cmath>
71 #include <limits>
72 
76 #include "sysc/utils/sc_report.h"
77 #include <ios>
78 #include <ostream>
79 
80 namespace sc_dt
81 {
82 
83 //-----------------------------------------------------------------------------
84 //"sc_io_base"
85 //
86 // This inline function returns the type of an i/o stream's base as a SystemC
87 // base designator.
88 // stream_object = reference to the i/o stream whose base is to be returned.
89 //
90 //"sc_io_show_base"
91 //
92 // This inline function returns true if the base should be shown when a SystemC
93 // value is displayed via the supplied stream operator.
94 // stream_object = reference to the i/o stream to return showbase value for.
95 //-----------------------------------------------------------------------------
96 #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC)
97  inline sc_numrep
98  sc_io_base( ::std::ostream& os, sc_numrep def_base )
99  {
100  std::ios::fmtflags flags = os.flags() & std::ios::basefield;
101  if ( flags & ::std::ios::dec ) return SC_DEC;
102  if ( flags & ::std::ios::hex ) return SC_HEX;
103  if ( flags & ::std::ios::oct ) return SC_OCT;
104  return def_base;
105  }
106 
107  inline bool
108  sc_io_show_base( ::std::ostream& os )
109  {
110  return (os.flags() & ::std::ios::showbase) != 0 ;
111  }
112 #else // Other
113  inline sc_numrep
114  sc_io_base( ::std::ostream& /*unused*/, sc_numrep /*unused*/ )
115  {
116  return SC_DEC;
117  }
118  inline bool
119  sc_io_show_base( ::std::ostream& /*unused*/ )
120  {
121  return false;
122  }
123 #endif
124 
125 const std::string to_string( sc_numrep );
126 
127 inline
128 ::std::ostream&
129 operator << ( ::std::ostream& os, sc_numrep numrep )
130 {
131  os << to_string( numrep );
132  return os;
133 }
134 
135 // ----------------------------------------------------------------------------
136 
137 // One transition of the FSM to find base and sign of a number.
138 extern
140 fsm_move(char c, small_type &b, small_type &s, small_type &state);
141 
142 // Parse a character string into its equivalent binary bits.
143 extern
145  const char* src_p, int dst_n, sc_digit* data_p, sc_digit* ctrl_p=0
146 );
147 
148 
149 // Parse a character string into its equivalent hexadecimal bits.
150 extern
152  const char* src_p, int dst_n, sc_digit* data_p, sc_digit* ctrl_p=0
153 );
154 
155 
156 // Find the base and sign of a number in v.
157 extern
158 SC_API const char *
159 get_base_and_sign(const char *v, small_type &base, small_type &sign);
160 
161 // Create a number out of v in base.
162 extern
164 vec_from_str(int unb, int und, sc_digit *u,
165  const char *v, sc_numrep base = SC_NOBASE) ;
166 
167 
182 extern
183 SC_API void
184 vec_add(int ulen, const sc_digit *u,
185  int vlen, const sc_digit *v, sc_digit *w);
186 
187 extern
188 SC_API void
189 vec_add_on(int ulen, sc_digit *u,
190  int vlen, const sc_digit *v);
191 
192 extern
193 SC_API void
194 vec_add_on2(int ulen, sc_digit *u,
195  int vlen, const sc_digit *v);
196 
197 extern
198 SC_API void
199 vec_add_small(int ulen, const sc_digit *u,
200  sc_digit v, sc_digit *w);
201 
202 extern
203 SC_API void
204 vec_add_small_on(int ulen, sc_digit *u, sc_digit v);
205 
206 
211 extern
212 SC_API void
213 vec_sub(int ulen, const sc_digit *u,
214  int vlen, const sc_digit *v, sc_digit *w);
215 
216 extern
217 SC_API void
218 vec_sub_on(int ulen, sc_digit *u,
219  int vlen, const sc_digit *v);
220 
221 extern
222 SC_API void
223 vec_sub_on2(int ulen, sc_digit *u,
224  int vlen, const sc_digit *v);
225 
226 extern
227 SC_API void
228 vec_sub_small(int ulen, const sc_digit *u,
229  sc_digit v, sc_digit *w);
230 
231 extern
232 SC_API void
233 vec_sub_small_on(int ulen, sc_digit *u, sc_digit v);
234 
235 
240 extern
241 SC_API void
242 vec_mul(int ulen, const sc_digit *u,
243  int vlen, const sc_digit *v, sc_digit *w);
244 
245 extern
246 SC_API void
247 vec_mul_small(int ulen, const sc_digit *u,
248  sc_digit v, sc_digit *w);
249 
250 extern
251 SC_API void
252 vec_mul_small_on(int ulen, sc_digit *u, sc_digit v);
253 
254 
259 extern
260 SC_API void
261 vec_div_large(int ulen, const sc_digit *u,
262  int vlen, const sc_digit *v, sc_digit *w);
263 
264 extern
265 SC_API void
266 vec_div_small(int ulen, const sc_digit *u,
267  sc_digit v, sc_digit *w);
268 
269 
274 extern
275 SC_API void
276 vec_rem_large(int ulen, const sc_digit *u,
277  int vlen, const sc_digit *v, sc_digit *w);
278 
279 extern
281 vec_rem_small(int ulen, const sc_digit *u, sc_digit v);
282 
283 extern
285 vec_rem_on_small(int ulen, sc_digit *u, sc_digit v);
286 
287 
292 extern
293 SC_API int
294 vec_to_char(int ulen, const sc_digit *u,
295  int vlen, uchar *v);
296 
297 extern
298 SC_API void
299 vec_from_char(int ulen, const uchar *u,
300  int vlen, sc_digit *v);
301 
302 
307 extern
308 SC_API void
309 vec_shift_left(int ulen, sc_digit *u, int nsl);
310 
311 extern
312 SC_API void
313 vec_shift_right(int vlen, sc_digit *u, int nsr, sc_digit fill = 0);
314 
315 extern
316 SC_API void
317 vec_reverse(int unb, int und, sc_digit *ud,
318  int l, int r = 0);
319 
320 
321 // ----------------------------------------------------------------------------
322 // Various utility functions.
323 // ----------------------------------------------------------------------------
324 
325 // Return the low half part of d.
326 inline
327 sc_digit
329 {
330  return (d & HALF_DIGIT_MASK);
331 }
332 
333 // Return the high half part of d. The high part of the digit may have
334 // more bits than BITS_PER_HALF_DIGIT due to, e.g., overflow in the
335 // multiplication. Hence, in other functions that use high_half(),
336 // make sure that the result contains BITS_PER_HALF_DIGIT if
337 // necessary. This is done by high_half_masked().
338 inline
339 sc_digit
341 {
342  return (d >> BITS_PER_HALF_DIGIT);
343 }
344 
345 inline
346 sc_digit
348 {
349  return (high_half(d) & HALF_DIGIT_MASK);
350 }
351 
352 // Concatenate the high part h and low part l. Assumes that h and l
353 // are less than or equal to HALF_DIGIT_MASK;
354 inline
355 sc_digit
357 {
358  return ((h << BITS_PER_HALF_DIGIT) | l);
359 }
360 
361 // Create a number with n 1's.
362 inline
363 sc_digit
365 {
366  return (((sc_digit) 1 << n) - 1);
367 }
368 
369 // Create a number with one 1 and n 0's.
370 inline
371 sc_digit
373 {
374  return ((sc_digit) 1 << n);
375 }
376 
377 
378 // Find the digit that bit i is in.
379 inline
380 int
381 digit_ord(int i)
382 {
383  return (i / BITS_PER_DIGIT);
384 }
385 
386 // Find the bit in digit_ord(i) that bit i corressponds to.
387 inline
388 int
389 bit_ord(int i)
390 {
391  return (i % BITS_PER_DIGIT);
392 }
393 
394 
399 // Compare u and v and return r
400 // r = 0 if u == v
401 // r < 0 if u < v
402 // r > 0 if u > v
403 // - Assume that all the leading zero digits are already skipped.
404 // - ulen and/or vlen can be zero.
405 // - Every digit is less than or equal to DIGIT_MASK;
406 inline
407 int
408 vec_cmp(int ulen, const sc_digit *u,
409  int vlen, const sc_digit *v)
410 {
411 
412 #ifdef DEBUG_SYSTEMC
413  // sc_assert((ulen <= 0) || (u != NULL));
414  // sc_assert((vlen <= 0) || (v != NULL));
415 
416  // ulen and vlen can be equal to 0 because vec_cmp can be called
417  // after vec_skip_leading_zeros.
418  sc_assert((ulen >= 0) && (u != NULL));
419  sc_assert((vlen >= 0) && (v != NULL));
420  // If ulen > 0, then the leading digit of u must be non-zero.
421  sc_assert((ulen <= 0) || (u[ulen - 1] != 0));
422  sc_assert((vlen <= 0) || (v[vlen - 1] != 0));
423 #endif
424 
425  if (ulen != vlen)
426  return (ulen - vlen);
427 
428  // ulen == vlen >= 1
429  while ((--ulen >= 0) && (u[ulen] == v[ulen]))
430  ;
431 
432  if (ulen < 0)
433  return 0;
434 
435 #ifdef DEBUG_SYSTEMC
436  // Test to see if the result is wrong due to the presence of
437  // overflow bits.
438  sc_assert((u[ulen] & DIGIT_MASK) != (v[ulen] & DIGIT_MASK));
439 #endif
440 
441  return (int) (u[ulen] - v[ulen]);
442 
443 }
444 
445 // Find the index of the first non-zero digit.
446 // - ulen (before) = the number of digits in u.
447 // - the returned value = the index of the first non-zero digit.
448 // A negative value of -1 indicates that every digit in u is zero.
449 inline
450 int
451 vec_find_first_nonzero(int ulen, const sc_digit *u)
452 {
453 
454 #ifdef DEBUG_SYSTEMC
455  // sc_assert((ulen <= 0) || (u != NULL));
456  sc_assert((ulen > 0) && (u != NULL));
457 #endif
458 
459  while ((--ulen >= 0) && (! u[ulen]))
460  ;
461 
462  return ulen;
463 
464 }
465 
466 // Skip all the leading zero digits.
467 // - ulen (before) = the number of digits in u.
468 // - the returned value = the number of non-zero digits in u.
469 // - the returned value is non-negative.
470 inline
471 int
472 vec_skip_leading_zeros(int ulen, const sc_digit *u)
473 {
474 
475 #ifdef DEBUG_SYSTEMC
476  // sc_assert((ulen <= 0) || (u != NULL));
477  sc_assert((ulen > 0) && (u != NULL));
478 #endif
479 
480  return (1 + vec_find_first_nonzero(ulen, u));
481 
482 }
483 
484 // Compare u and v and return r
485 // r = 0 if u == v
486 // r < 0 if u < v
487 // r > 0 if u > v
488 inline
489 int
490 vec_skip_and_cmp(int ulen, const sc_digit *u,
491  int vlen, const sc_digit *v)
492 {
493 
494 #ifdef DEBUG_SYSTEMC
495  sc_assert((ulen > 0) && (u != NULL));
496  sc_assert((vlen > 0) && (v != NULL));
497 #endif
498 
499  ulen = vec_skip_leading_zeros(ulen, u);
500  vlen = vec_skip_leading_zeros(vlen, v);
501  // ulen and/or vlen can be equal to zero here.
502  return vec_cmp(ulen, u, vlen, v);
503 
504 }
505 
506 // Set u[i] = 0 where i = from ... (ulen - 1).
507 inline
508 void
509 vec_zero(int from, int ulen, sc_digit *u)
510 {
511 
512 #ifdef DEBUG_SYSTEMC
513  sc_assert((ulen > 0) && (u != NULL));
514 #endif
515 
516  for(int i = from; i < ulen; i++)
517  u[i] = 0;
518 
519 }
520 
521 // Set u[i] = 0 where i = 0 .. (ulen - 1).
522 inline
523 void
524 vec_zero(int ulen, sc_digit *u)
525 {
526  vec_zero(0, ulen, u);
527 }
528 
529 // Copy n digits from v to u.
530 inline
531 void
532 vec_copy(int n, sc_digit *u, const sc_digit *v)
533 {
534 
535 #ifdef DEBUG_SYSTEMC
536  sc_assert((n > 0) && (u != NULL) && (v != NULL));
537 #endif
538 
539  for (int i = 0; i < n; ++i)
540  u[i] = v[i];
541 }
542 
543 // Copy v to u, where ulen >= vlen, and zero the rest of the digits in u.
544 inline
545 void
547  int vlen, const sc_digit *v)
548 {
549 
550 #ifdef DEBUG_SYSTEMC
551  sc_assert((ulen > 0) && (u != NULL));
552  sc_assert((vlen > 0) && (v != NULL));
553  sc_assert(ulen >= vlen);
554 #endif
555 
556  vec_copy(vlen, u, v);
557  vec_zero(vlen, ulen, u);
558 
559 }
560 
561 // 2's-complement the digits in u.
562 inline
563 void
565 {
566 
567 #ifdef DEBUG_SYSTEMC
568  sc_assert((ulen > 0) && (u != NULL));
569 #endif
570 
571  sc_digit carry = 1;
572 
573  for (int i = 0; i < ulen; ++i) {
574  carry += (~u[i] & DIGIT_MASK);
575  u[i] = carry & DIGIT_MASK;
576  carry >>= BITS_PER_DIGIT;
577  }
578 
579 }
580 
581 
586 // u = v
587 // - v is an unsigned long or uint64, and positive integer.
588 template< class Type >
589 inline
590 void
591 from_uint(int ulen, sc_digit *u, Type v)
592 {
593 
594 #ifdef DEBUG_SYSTEMC
595  // sc_assert((ulen <= 0) || (u != NULL));
596  sc_assert((ulen > 0) && (u != NULL));
597  sc_assert(v >= 0);
598 #endif
599 
600  int i = 0;
601 
602  while (v && (i < ulen)) {
603 #ifndef _WIN32
604  u[i++] = static_cast<sc_digit>( v & DIGIT_MASK );
605 #else
606  u[i++] = ((sc_digit) v) & DIGIT_MASK;
607 #endif
608  v >>= BITS_PER_DIGIT;
609  }
610 
611  vec_zero(i, ulen, u);
612 
613 }
614 
615 
616 // Get u's sign and return its absolute value.
617 // u can be long, unsigned long, int64, or uint64.
618 template< class Type >
619 inline
621 get_sign(Type &u)
622 {
623  if (u > 0)
624  return SC_POS;
625 
626  if (u == 0)
627  return SC_ZERO;
628 
629  // no positive number representable for minimum value,
630  // leave as is to avoid Undefined Behaviour
631  if( SC_LIKELY_( u > (std::numeric_limits<Type>::min)() ) )
632  u = -u;
633 
634  return SC_NEG;
635 }
636 
637 
638 // Return us * vs:
639 // - Return SC_ZERO if either sign is SC_ZERO.
640 // - Return SC_POS if us == vs
641 // - Return SC_NEG if us != vs.
642 inline
645 {
646  if ((us == SC_ZERO) || (vs == SC_ZERO))
647  return SC_ZERO;
648 
649  if (us == vs)
650  return SC_POS;
651 
652  return SC_NEG;
653 }
654 
655 
660 #ifdef SC_MAX_NBITS
661 
662 SC_API void test_bound_failed(int nb);
663 
664 inline void
665 test_bound(int nb)
666 {
667  if (nb > SC_MAX_NBITS) {
668  test_bound_failed( nb );
669  sc_core::sc_abort(); // can't recover from here
670  }
671 }
672 
673 #endif
674 
675 template< class Type >
676 inline
677 void
678 div_by_zero(Type s)
679 {
680  if (s == 0) {
681  SC_REPORT_ERROR( sc_core::SC_ID_OPERATION_FAILED_,
682  "div_by_zero<Type>( Type ) : division by zero" );
683  sc_core::sc_abort(); // can't recover from here
684  }
685 }
686 
687 
692 // If u[i] is zero for every i = 0,..., ulen - 1, return SC_ZERO,
693 // else return s.
694 inline
696 check_for_zero(small_type s, int ulen, const sc_digit *u)
697 {
698 
699 #ifdef DEBUG_SYSTEMC
700  // sc_assert(ulen >= 0);
701  sc_assert((ulen > 0) && (u != NULL));
702 #endif
703 
704  if (vec_find_first_nonzero(ulen, u) < 0)
705  return SC_ZERO;
706 
707  return s;
708 
709 }
710 
711 // If u[i] is zero for every i = 0,..., ulen - 1, return true,
712 // else return false.
713 inline
714 bool
715 check_for_zero(int ulen, const sc_digit *u)
716 {
717 
718 #ifdef DEBUG_SYSTEMC
719  // sc_assert(ulen >= 0);
720  sc_assert((ulen > 0) && (u != NULL));
721 #endif
722 
723  if (vec_find_first_nonzero(ulen, u) < 0)
724  return true;
725 
726  return false;
727 
728 }
729 
730 inline
732 make_zero(int nd, sc_digit *d)
733 {
734  vec_zero(nd, d);
735  return SC_ZERO;
736 }
737 
738 
747 // Trim the extra leading bits of a signed or unsigned number.
748 inline
749 void
750 trim(small_type added, int nb, int nd, sc_digit *d)
751 {
752 #ifdef DEBUG_SYSTEMC
753  sc_assert((nb > 0) && (nd > 0) && (d != NULL));
754 #endif
755 
756  d[nd - 1] &= one_and_ones(bit_ord(nb - 1) + added);
757 }
758 
759 // Convert an (un)signed number from sign-magnitude representation to
760 // 2's complement representation and trim the extra bits.
761 inline
762 void
764  small_type s, int nb, int nd, sc_digit *d)
765 {
766  if (s == SC_NEG) {
767  vec_complement(nd, d);
768  trim(added, nb, nd, d);
769  }
770 }
771 
772 // Convert an (un)signed number from sign-magnitude representation to
773 // 2's complement representation but do not trim the extra bits.
774 inline
775 void
777 {
778  if (s == SC_NEG)
779  vec_complement(nd, d);
780 }
781 
782 
788 // Trim the extra leading bits off a signed number.
789 inline
790 void
791 trim_signed(int nb, int nd, sc_digit *d)
792 {
793 #ifdef DEBUG_SYSTEMC
794  sc_assert((nb > 0) && (nd > 0) && (d != NULL));
795 #endif
796 
797  d[nd - 1] &= one_and_ones(bit_ord(nb - 1) + 1);
798 }
799 
800 // Convert a signed number from 2's complement representation to
801 // sign-magnitude representation, and return its sign. nd is d's
802 // actual size, without zeros eliminated.
803 inline
806 {
807 
808 #ifdef DEBUG_SYSTEMC
809  sc_assert((nb > 0) && (nd > 0) && (d != NULL));
810 #endif
811 
812  small_type s;
813 
814  int xnb = bit_ord(nb - 1) + 1;
815 
816  // Test the sign bit.
817  if (d[nd - 1] & one_and_zeros(xnb - 1)) {
818  s = SC_NEG;
819  vec_complement(nd, d);
820  }
821  else
822  s = SC_POS;
823 
824  // Trim the last digit.
825  d[nd - 1] &= one_and_ones(xnb);
826 
827  // Check if the new number is zero.
828  if (s == SC_POS)
829  return check_for_zero(s, nd, d);
830 
831  return s;
832 
833 }
834 
835 // Convert a signed number from sign-magnitude representation to 2's
836 // complement representation, get its sign, convert back to
837 // sign-magnitude representation, and return its sign. nd is d's
838 // actual size, without zeros eliminated.
839 inline
842 {
843  convert_SM_to_2C(s, nd, d);
844  return convert_signed_2C_to_SM(nb, nd, d);
845 }
846 
847 // Convert a signed number from sign-magnitude representation to 2's
848 // complement representation and trim the extra bits.
849 inline
850 void
852 {
853  convert_SM_to_2C_trimmed(1, s, nb, nd, d);
854 }
855 
856 // Convert a signed number from sign-magnitude representation to 2's
857 // complement representation but do not trim the extra bits.
858 inline
859 void
861 {
862  convert_SM_to_2C(s, nd, d);
863 }
864 
865 
871 // Trim the extra leading bits off an unsigned number.
872 inline
873 void
874 trim_unsigned(int nb, int nd, sc_digit *d)
875 {
876 #ifdef DEBUG_SYSTEMC
877  sc_assert((nb > 0) && (nd > 0) && (d != NULL));
878 #endif
879 
880  d[nd - 1] &= one_and_ones(bit_ord(nb - 1));
881 }
882 
883 // Convert an unsigned number from 2's complement representation to
884 // sign-magnitude representation, and return its sign. nd is d's
885 // actual size, without zeros eliminated.
886 inline
889 {
890  trim_unsigned(nb, nd, d);
891  return check_for_zero(SC_POS, nd, d);
892 }
893 
894 // Convert an unsigned number from sign-magnitude representation to
895 // 2's complement representation, get its sign, convert back to
896 // sign-magnitude representation, and return its sign. nd is d's
897 // actual size, without zeros eliminated.
898 inline
901 {
902  convert_SM_to_2C(s, nd, d);
903  return convert_unsigned_2C_to_SM(nb, nd, d);
904 }
905 
906 // Convert an unsigned number from sign-magnitude representation to
907 // 2's complement representation and trim the extra bits.
908 inline
909 void
911 {
912  convert_SM_to_2C_trimmed(0, s, nb, nd, d);
913 }
914 
915 // Convert an unsigned number from sign-magnitude representation to
916 // 2's complement representation but do not trim the extra bits.
917 inline
918 void
920 {
921  convert_SM_to_2C(s, nd, d);
922 }
923 
924 
929 // Copy v to u.
930 inline
931 void
933  int unb, int und, sc_digit *ud,
934  int vnb, int vnd, const sc_digit *vd)
935 {
936 
937  if (und <= vnd) {
938 
939  vec_copy(und, ud, vd);
940 
941  if (unb <= vnb)
942  us = convert_signed_SM_to_2C_to_SM(us, unb, und, ud);
943 
944  }
945  else // und > vnd
946  vec_copy_and_zero(und, ud, vnd, vd);
947 
948 }
949 
950 // Copy v to u.
951 inline
952 void
954  int unb, int und, sc_digit *ud,
955  int /* vnb */, int vnd, const sc_digit *vd)
956 {
957 
958  if (und <= vnd)
959  vec_copy(und, ud, vd);
960 
961  else // und > vnd
962  vec_copy_and_zero(und, ud, vnd, vd);
963 
964  us = convert_unsigned_SM_to_2C_to_SM(us, unb, und, ud);
965 
966 }
967 
968 
973 // A version of set(i, v) without bound checking.
974 inline
975 void
976 safe_set(int i, bool v, sc_digit *d)
977 {
978 
979 #ifdef DEBUG_SYSTEMC
980  sc_assert((i >= 0) && (d != NULL));
981 #endif
982 
983  int bit_num = bit_ord(i);
984  int digit_num = digit_ord(i);
985 
986  if (v)
987  d[digit_num] |= one_and_zeros(bit_num);
988  else
989  d[digit_num] &= ~(one_and_zeros(bit_num));
990 
991 }
992 
993 
998 inline
999 bool
1000 is_nan( double v )
1001 {
1002  return std::numeric_limits<double>::has_quiet_NaN && (v != v);
1003 }
1004 
1005 inline
1006 bool
1007 is_inf( double v )
1008 {
1009  return v == std::numeric_limits<double>::infinity()
1010  || v == -std::numeric_limits<double>::infinity();
1011 }
1012 
1013 inline
1014 void
1015 is_bad_double(double v)
1016 {
1017 // Windows throws exception.
1018  if( is_nan(v) || is_inf(v) )
1019  SC_REPORT_ERROR( sc_core::SC_ID_VALUE_NOT_VALID_,
1020  "is_bad_double( double v ) : "
1021  "v is not finite - NaN or Inf" );
1022 }
1023 
1024 } // namespace sc_dt
1025 
1026 
1027 #endif
int vec_cmp(int ulen, const sc_digit *u, int vlen, const sc_digit *v)
Definition: sc_nbutils.h:408
SC_API sc_digit vec_rem_on_small(int ulen, sc_digit *u, sc_digit v)
SC_API void vec_reverse(int unb, int und, sc_digit *ud, int l, int r=0)
SC_API void vec_shift_left(int ulen, sc_digit *u, int nsl)
#define SC_ZERO
Definition: sc_nbdefs.h:116
#define sc_assert(expr)
Definition: sc_report.h:270
sc_concref_r< sc_bitref_r< T1 >, sc_bitref_r< T2 > > concat(sc_bitref_r< T1 >, sc_bitref_r< T2 >)
SC_API void vec_div_small(int ulen, const sc_digit *u, sc_digit v, sc_digit *w)
SC_API const char * get_base_and_sign(const char *v, small_type &base, small_type &sign)
int bit_ord(int i)
Definition: sc_nbutils.h:389
void copy_digits_unsigned(small_type &us, int unb, int und, sc_digit *ud, int, int vnd, const sc_digit *vd)
Definition: sc_nbutils.h:953
#define BITS_PER_DIGIT
Definition: sc_nbdefs.h:143
SC_API void vec_sub_on2(int ulen, sc_digit *u, int vlen, const sc_digit *v)
SC_API void vec_div_large(int ulen, const sc_digit *u, int vlen, const sc_digit *v, sc_digit *w)
small_type make_zero(int nd, sc_digit *d)
Definition: sc_nbutils.h:732
SC_API small_type fsm_move(char c, small_type &b, small_type &s, small_type &state)
void vec_copy(int n, sc_digit *u, const sc_digit *v)
Definition: sc_nbutils.h:532
void vec_complement(int ulen, sc_digit *u)
Definition: sc_nbutils.h:564
void from_uint(int ulen, sc_digit *u, Type v)
Definition: sc_nbutils.h:591
void convert_SM_to_2C(small_type s, int nd, sc_digit *d)
Definition: sc_nbutils.h:776
Report ids for the datatypes/int code.
small_type convert_signed_2C_to_SM(int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:805
void trim(small_type added, int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:750
SC_API small_type vec_from_str(int unb, int und, sc_digit *u, const char *v, sc_numrep base=SC_NOBASE)
int small_type
Definition: sc_nbdefs.h:124
sc_digit low_half(sc_digit d)
Definition: sc_nbutils.h:328
void trim_unsigned(int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:874
sc_digit one_and_ones(int n)
Definition: sc_nbutils.h:364
SC_API void vec_add_small(int ulen, const sc_digit *u, sc_digit v, sc_digit *w)
small_type mul_signs(small_type us, small_type vs)
Definition: sc_nbutils.h:644
unsigned int sc_digit
Definition: sc_nbdefs.h:179
SC_API void vec_shift_right(int vlen, sc_digit *u, int nsr, sc_digit fill=0)
uint64 const sc_uint_base int b
Definition: sc_fxval.h:1005
#define DIGIT_MASK
Definition: sc_nbdefs.h:145
SC_API void parse_binary_bits(const char *src_p, int dst_n, sc_digit *data_p, sc_digit *ctrl_p=0)
small_type convert_unsigned_2C_to_SM(int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:888
SC_API void vec_add(int ulen, const sc_digit *u, int vlen, const sc_digit *v, sc_digit *w)
#define BITS_PER_HALF_DIGIT
Definition: sc_nbdefs.h:151
#define SC_LIKELY_(x)
Definition: sc_cmnhdr.h:84
sc_digit high_half_masked(sc_digit d)
Definition: sc_nbutils.h:347
bool is_inf(double v)
Definition: sc_nbutils.h:1007
SC_API void vec_add_small_on(int ulen, sc_digit *u, sc_digit v)
SC_API void vec_add_on2(int ulen, sc_digit *u, int vlen, const sc_digit *v)
SC_API const std::string to_string(sc_enc)
unsigned char uchar
Definition: sc_nbdefs.h:120
void convert_unsigned_SM_to_2C(small_type s, int nd, sc_digit *d)
Definition: sc_nbutils.h:919
sc_numrep
Enumeration of number representations for character string conversion.
Definition: sc_nbdefs.h:97
#define SC_NEG
Definition: sc_nbdefs.h:115
void safe_set(int i, bool v, sc_digit *d)
Definition: sc_nbutils.h:976
SC_API void vec_mul_small(int ulen, const sc_digit *u, sc_digit v, sc_digit *w)
small_type get_sign(Type &u)
Definition: sc_nbutils.h:621
void div_by_zero(Type s)
Definition: sc_nbutils.h:678
SC_API void vec_mul(int ulen, const sc_digit *u, int vlen, const sc_digit *v, sc_digit *w)
SC_API int vec_to_char(int ulen, const sc_digit *u, int vlen, uchar *v)
SC_API void parse_hex_bits(const char *src_p, int dst_n, sc_digit *data_p, sc_digit *ctrl_p=0)
void convert_unsigned_SM_to_2C_trimmed(small_type s, int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:910
sc_digit high_half(sc_digit d)
Definition: sc_nbutils.h:340
void copy_digits_signed(small_type &us, int unb, int und, sc_digit *ud, int vnb, int vnd, const sc_digit *vd)
Definition: sc_nbutils.h:932
int vec_find_first_nonzero(int ulen, const sc_digit *u)
Definition: sc_nbutils.h:451
void convert_signed_SM_to_2C(small_type s, int nd, sc_digit *d)
Definition: sc_nbutils.h:860
int digit_ord(int i)
Definition: sc_nbutils.h:381
Report ids for the datatypes/bit code.
void vec_zero(int from, int ulen, sc_digit *u)
Definition: sc_nbutils.h:509
void convert_signed_SM_to_2C_trimmed(small_type s, int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:851
sc_digit one_and_zeros(int n)
Definition: sc_nbutils.h:372
SC_API void vec_sub_on(int ulen, sc_digit *u, int vlen, const sc_digit *v)
#define HALF_DIGIT_MASK
Definition: sc_nbdefs.h:153
SC_API void vec_add_on(int ulen, sc_digit *u, int vlen, const sc_digit *v)
#define SC_POS
Definition: sc_nbdefs.h:117
small_type convert_unsigned_SM_to_2C_to_SM(small_type s, int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:900
SC_API void vec_mul_small_on(int ulen, sc_digit *u, sc_digit v)
SC_API void vec_from_char(int ulen, const uchar *u, int vlen, sc_digit *v)
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report.h:223
small_type check_for_zero(small_type s, int ulen, const sc_digit *u)
Definition: sc_nbutils.h:696
SC_API void vec_sub_small_on(int ulen, sc_digit *u, sc_digit v)
Run-time logging and reporting facilities.
sc_numrep sc_io_base(::std::ostream &, sc_numrep)
Definition: sc_nbutils.h:114
SC_API void vec_sub_small(int ulen, const sc_digit *u, sc_digit v, sc_digit *w)
Top level header file for arbitrary precision signed/unsigned.
SC_API void vec_sub(int ulen, const sc_digit *u, int vlen, const sc_digit *v, sc_digit *w)
int vec_skip_leading_zeros(int ulen, const sc_digit *u)
Definition: sc_nbutils.h:472
void is_bad_double(double v)
Definition: sc_nbutils.h:1015
inline ::std::ostream & operator<<(::std::ostream &os, const sc_bit &a)
Definition: sc_bit.h:390
small_type convert_signed_SM_to_2C_to_SM(small_type s, int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:841
SC_API sc_digit vec_rem_small(int ulen, const sc_digit *u, sc_digit v)
void convert_SM_to_2C_trimmed(small_type added, small_type s, int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:763
void trim_signed(int nb, int nd, sc_digit *d)
Definition: sc_nbutils.h:791
SC_NORETURN_ SC_API void sc_abort()
void vec_copy_and_zero(int ulen, sc_digit *u, int vlen, const sc_digit *v)
Definition: sc_nbutils.h:546
bool is_nan(double v)
Definition: sc_nbutils.h:1000
int vec_skip_and_cmp(int ulen, const sc_digit *u, int vlen, const sc_digit *v)
Definition: sc_nbutils.h:490
bool sc_io_show_base(::std::ostream &)
Definition: sc_nbutils.h:119
SC_API void vec_rem_large(int ulen, const sc_digit *u, int vlen, const sc_digit *v, sc_digit *w)
#define SC_API
Definition: sc_cmnhdr.h:168