SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_fixed.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_fixed.h -
23
24 Original Author: Martin Janssen, Synopsys, Inc.
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31 changes you are making here.
32
33 Name, Affiliation, Date:
34 Description of Modification:
35
36 *****************************************************************************/
37
38// $Log: sc_fixed.h,v $
39// Revision 1.2 2011/01/19 18:57:40 acg
40// Andy Goodrich: changes for IEEE_1666_2011.
41//
42// Revision 1.1.1.1 2006/12/15 20:20:04 acg
43// SystemC 2.3
44//
45// Revision 1.3 2006/01/13 18:53:57 acg
46// Andy Goodrich: added $Log command so that CVS comments are reproduced in
47// the source.
48//
49
50#ifndef SC_FIXED_H
51#define SC_FIXED_H
52
53
55
56
57namespace sc_dt
58{
59
60// classes defined in this module
61template <int W, int I, sc_q_mode Q, sc_o_mode O, int N> class sc_fixed;
62template <int W, int I, sc_q_mode Q, sc_o_mode O, int N> class sc_fixed_fast;
63
64
65// ----------------------------------------------------------------------------
66// TEMPLATE CLASS : sc_fixed
67//
68// "Constrained" signed fixed-point class; arbitrary precision.
69// ----------------------------------------------------------------------------
70
71template <int W, int I,
74class sc_fixed : public sc_fix
75{
76
77public:
78
79 // constructors
80
81 explicit sc_fixed( sc_fxnum_observer* = 0 );
82 explicit sc_fixed( const sc_fxcast_switch&, sc_fxnum_observer* = 0 );
83
84#define DECL_CTORS_T_A(tp) \
85 sc_fixed( tp, sc_fxnum_observer* = 0 ); \
86 sc_fixed( tp, const sc_fxcast_switch&, sc_fxnum_observer* = 0 );
87
88#define DECL_CTORS_T_B(tp) \
89 explicit sc_fixed( tp, sc_fxnum_observer* = 0 ); \
90 sc_fixed( tp, const sc_fxcast_switch&, sc_fxnum_observer* = 0 );
91
93 DECL_CTORS_T_A(unsigned int)
94 DECL_CTORS_T_A(long)
95 DECL_CTORS_T_A(unsigned long)
96 DECL_CTORS_T_A(float)
97 DECL_CTORS_T_A(double)
98 DECL_CTORS_T_A(const char*)
109
110#undef DECL_CTORS_T_A
111#undef DECL_CTORS_T_B
112
113 // copy constructor
114
116
117
118 // assignment operators
119
121
122#define DECL_ASN_OP_T(op,tp) \
123 sc_fixed& operator op ( tp );
124
125#define DECL_ASN_OP_OTHER(op) \
126 DECL_ASN_OP_T(op,int64) \
127 DECL_ASN_OP_T(op,uint64) \
128 DECL_ASN_OP_T(op,const sc_int_base&) \
129 DECL_ASN_OP_T(op,const sc_uint_base&) \
130 DECL_ASN_OP_T(op,const sc_signed&) \
131 DECL_ASN_OP_T(op,const sc_unsigned&)
132
133#define DECL_ASN_OP(op) \
134 DECL_ASN_OP_T(op,int) \
135 DECL_ASN_OP_T(op,unsigned int) \
136 DECL_ASN_OP_T(op,long) \
137 DECL_ASN_OP_T(op,unsigned long) \
138 DECL_ASN_OP_T(op,float) \
139 DECL_ASN_OP_T(op,double) \
140 DECL_ASN_OP_T(op,const char*) \
141 DECL_ASN_OP_T(op,const sc_fxval&) \
142 DECL_ASN_OP_T(op,const sc_fxval_fast&) \
143 DECL_ASN_OP_T(op,const sc_fxnum&) \
144 DECL_ASN_OP_T(op,const sc_fxnum_fast&) \
145 DECL_ASN_OP_OTHER(op)
146
147 DECL_ASN_OP(=)
148
149 DECL_ASN_OP(*=)
150 DECL_ASN_OP(/=)
151 DECL_ASN_OP(+=)
152 DECL_ASN_OP(-=)
153
154 DECL_ASN_OP_T(<<=,int)
155 DECL_ASN_OP_T(>>=,int)
156
157 DECL_ASN_OP_T(&=,const sc_fix&)
158 DECL_ASN_OP_T(&=,const sc_fix_fast&)
159 DECL_ASN_OP_T(|=,const sc_fix&)
160 DECL_ASN_OP_T(|=,const sc_fix_fast&)
161 DECL_ASN_OP_T(^=,const sc_fix&)
162 DECL_ASN_OP_T(^=,const sc_fix_fast&)
163
164#undef DECL_ASN_OP_T
165#undef DECL_ASN_OP_OTHER
166#undef DECL_ASN_OP
167
168
169 // auto-increment and auto-decrement
170
171 sc_fxval operator ++ ( int );
172 sc_fxval operator -- ( int );
173
176
177};
178
179
180// ----------------------------------------------------------------------------
181// TEMPLATE CLASS : sc_fixed_fast
182//
183// "Constrained" signed fixed-point class; limited precision.
184// ----------------------------------------------------------------------------
185
186template <int W, int I,
190{
191
192public:
193
194 // constructors
195
196 explicit sc_fixed_fast( sc_fxnum_fast_observer* = 0 );
197 explicit sc_fixed_fast( const sc_fxcast_switch&,
199
200#define DECL_CTORS_T_A(tp) \
201 sc_fixed_fast( tp, sc_fxnum_fast_observer* = 0 ); \
202 sc_fixed_fast( tp, const sc_fxcast_switch&, \
203 sc_fxnum_fast_observer* = 0 );
204
205#define DECL_CTORS_T_B(tp) \
206 explicit sc_fixed_fast( tp, sc_fxnum_fast_observer* = 0 ); \
207 sc_fixed_fast( tp, const sc_fxcast_switch&, \
208 sc_fxnum_fast_observer* = 0 );
209
210 DECL_CTORS_T_A(int)
211 DECL_CTORS_T_A(unsigned int)
212 DECL_CTORS_T_A(long)
213 DECL_CTORS_T_A(unsigned long)
214 DECL_CTORS_T_A(float)
215 DECL_CTORS_T_A(double)
216 DECL_CTORS_T_A(const char*)
217 DECL_CTORS_T_A(const sc_fxval&)
219 DECL_CTORS_T_A(const sc_fxnum&)
220 DECL_CTORS_T_A(const sc_fxnum_fast&)
223 DECL_CTORS_T_B(const sc_int_base&)
224 DECL_CTORS_T_B(const sc_uint_base&)
225 DECL_CTORS_T_B(const sc_signed&)
226 DECL_CTORS_T_B(const sc_unsigned&)
227
228#undef DECL_CTORS_T_A
229#undef DECL_CTORS_T_B
230
231 // copy constructor
232
233 sc_fixed_fast( const sc_fixed_fast<W,I,Q,O,N>& );
234
235
236 // assignment operators
237
238 sc_fixed_fast& operator = ( const sc_fixed_fast<W,I,Q,O,N>& );
239
240#define DECL_ASN_OP_T(op,tp) \
241 sc_fixed_fast& operator op ( tp );
242
243#define DECL_ASN_OP_OTHER(op) \
244 DECL_ASN_OP_T(op,int64) \
245 DECL_ASN_OP_T(op,uint64) \
246 DECL_ASN_OP_T(op,const sc_int_base&) \
247 DECL_ASN_OP_T(op,const sc_uint_base&) \
248 DECL_ASN_OP_T(op,const sc_signed&) \
249 DECL_ASN_OP_T(op,const sc_unsigned&)
250
251#define DECL_ASN_OP(op) \
252 DECL_ASN_OP_T(op,int) \
253 DECL_ASN_OP_T(op,unsigned int) \
254 DECL_ASN_OP_T(op,long) \
255 DECL_ASN_OP_T(op,unsigned long) \
256 DECL_ASN_OP_T(op,float) \
257 DECL_ASN_OP_T(op,double) \
258 DECL_ASN_OP_T(op,const char*) \
259 DECL_ASN_OP_T(op,const sc_fxval&) \
260 DECL_ASN_OP_T(op,const sc_fxval_fast&) \
261 DECL_ASN_OP_T(op,const sc_fxnum&) \
262 DECL_ASN_OP_T(op,const sc_fxnum_fast&) \
263 DECL_ASN_OP_OTHER(op)
264
265 DECL_ASN_OP(=)
266
267 DECL_ASN_OP(*=)
268 DECL_ASN_OP(/=)
269 DECL_ASN_OP(+=)
270 DECL_ASN_OP(-=)
271
272 DECL_ASN_OP_T(<<=,int)
273 DECL_ASN_OP_T(>>=,int)
274
275 DECL_ASN_OP_T(&=,const sc_fix&)
276 DECL_ASN_OP_T(&=,const sc_fix_fast&)
277 DECL_ASN_OP_T(|=,const sc_fix&)
278 DECL_ASN_OP_T(|=,const sc_fix_fast&)
279 DECL_ASN_OP_T(^=,const sc_fix&)
280 DECL_ASN_OP_T(^=,const sc_fix_fast&)
281
282#undef DECL_ASN_OP_T
283#undef DECL_ASN_OP_OTHER
284#undef DECL_ASN_OP
285
286
287 // auto-increment and auto-decrement
288
291
294
295};
296
297
298// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
299
300// ----------------------------------------------------------------------------
301// TEMPLATE CLASS : sc_fixed
302//
303// "Constrained" signed fixed-point class; arbitrary precision.
304// ----------------------------------------------------------------------------
305
306template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
307inline
309: sc_fix( W, I, Q, O, N, observer_ )
310{}
311
312template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
313inline
315 sc_fxnum_observer* observer_ )
316: sc_fix( W, I, Q, O, N, cast_sw, observer_ )
317{}
318
319#define DEFN_CTORS_T(tp) \
320template<int W, int I, sc_q_mode Q, sc_o_mode O, int N> \
321inline \
322sc_fixed<W,I,Q,O,N>::sc_fixed( tp a, \
323 sc_fxnum_observer* observer_ ) \
324: sc_fix( a, W, I, Q, O, N, observer_ ) \
325{} \
326 \
327template<int W, int I, sc_q_mode Q, sc_o_mode O, int N> \
328inline \
329sc_fixed<W,I,Q,O,N>::sc_fixed( tp a, \
330 const sc_fxcast_switch& cast_sw, \
331 sc_fxnum_observer* observer_ ) \
332: sc_fix( a, W, I, Q, O, N, cast_sw, observer_ ) \
333{}
334
335DEFN_CTORS_T(int)
336DEFN_CTORS_T(unsigned int)
337DEFN_CTORS_T(long)
338DEFN_CTORS_T(unsigned long)
339DEFN_CTORS_T(float)
340DEFN_CTORS_T(double)
341DEFN_CTORS_T(const char*)
352
353#undef DEFN_CTORS_T
354
355// copy constructor
356
357template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
358inline
360: sc_fix( a, W, I, Q, O, N )
361{}
362
363
364// assignment operators
365
366template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
367inline
370{
372 return *this;
373}
374
375#define DEFN_ASN_OP_T(op,tp) \
376template<int W, int I, sc_q_mode Q, sc_o_mode O, int N> \
377inline \
378sc_fixed<W,I,Q,O,N>& \
379sc_fixed<W,I,Q,O,N>::operator op ( tp a ) \
380{ \
381 sc_fix::operator op ( a ); \
382 return *this; \
383}
384
385#define DEFN_ASN_OP_OTHER(op) \
386DEFN_ASN_OP_T(op,int64) \
387DEFN_ASN_OP_T(op,uint64) \
388DEFN_ASN_OP_T(op,const sc_int_base&) \
389DEFN_ASN_OP_T(op,const sc_uint_base&) \
390DEFN_ASN_OP_T(op,const sc_signed&) \
391DEFN_ASN_OP_T(op,const sc_unsigned&)
392
393#define DEFN_ASN_OP(op) \
394DEFN_ASN_OP_T(op,int) \
395DEFN_ASN_OP_T(op,unsigned int) \
396DEFN_ASN_OP_T(op,long) \
397DEFN_ASN_OP_T(op,unsigned long) \
398DEFN_ASN_OP_T(op,float) \
399DEFN_ASN_OP_T(op,double) \
400DEFN_ASN_OP_T(op,const char*) \
401DEFN_ASN_OP_T(op,const sc_fxval&) \
402DEFN_ASN_OP_T(op,const sc_fxval_fast&) \
403DEFN_ASN_OP_T(op,const sc_fxnum&) \
404DEFN_ASN_OP_T(op,const sc_fxnum_fast&) \
405DEFN_ASN_OP_OTHER(op)
406
408
409DEFN_ASN_OP(*=)
410DEFN_ASN_OP(/=)
411DEFN_ASN_OP(+=)
412DEFN_ASN_OP(-=)
413
414DEFN_ASN_OP_T(<<=,int)
415DEFN_ASN_OP_T(>>=,int)
416
417DEFN_ASN_OP_T(&=,const sc_fix&)
418DEFN_ASN_OP_T(&=,const sc_fix_fast&)
419DEFN_ASN_OP_T(|=,const sc_fix&)
420DEFN_ASN_OP_T(|=,const sc_fix_fast&)
421DEFN_ASN_OP_T(^=,const sc_fix&)
422DEFN_ASN_OP_T(^=,const sc_fix_fast&)
423
424#undef DEFN_ASN_OP_T
425#undef DEFN_ASN_OP_OTHER
426#undef DEFN_ASN_OP
427
428
429// auto-increment and auto-decrement
430
431template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
432inline
435{
436 return sc_fxval( sc_fix::operator ++ ( 0 ) );
437}
438
439template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
440inline
443{
444 return sc_fxval( sc_fix::operator -- ( 0 ) );
445}
446
447template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
448inline
451{
453 return *this;
454}
455
456template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
457inline
460{
462 return *this;
463}
464
465
466// ----------------------------------------------------------------------------
467// TEMPLATE CLASS : sc_fixed_fast
468//
469// "Constrained" signed fixed-point class; limited precision.
470// ----------------------------------------------------------------------------
471
472template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
473inline
475: sc_fix_fast( W, I, Q, O, N, observer_ )
476{}
477
478template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
479inline
481 sc_fxnum_fast_observer* observer_ )
482: sc_fix_fast( W, I, Q, O, N, cast_sw, observer_ )
483{}
484
485#define DEFN_CTORS_T(tp) \
486template<int W, int I, sc_q_mode Q, sc_o_mode O, int N> \
487inline \
488sc_fixed_fast<W,I,Q,O,N>::sc_fixed_fast( tp a, \
489 sc_fxnum_fast_observer* observer_ ) \
490: sc_fix_fast( a, W, I, Q, O, N, observer_ ) \
491{} \
492 \
493template<int W, int I, sc_q_mode Q, sc_o_mode O, int N> \
494inline \
495sc_fixed_fast<W,I,Q,O,N>::sc_fixed_fast( tp a, \
496 const sc_fxcast_switch& cast_sw, \
497 sc_fxnum_fast_observer* observer_ ) \
498: sc_fix_fast( a, W, I, Q, O, N, cast_sw, observer_ ) \
499{}
500
501DEFN_CTORS_T(int)
502DEFN_CTORS_T(unsigned int)
503DEFN_CTORS_T(long)
504DEFN_CTORS_T(unsigned long)
505DEFN_CTORS_T(float)
506DEFN_CTORS_T(double)
507DEFN_CTORS_T(const char*)
508DEFN_CTORS_T(const sc_fxval&)
509DEFN_CTORS_T(const sc_fxval_fast&)
510DEFN_CTORS_T(const sc_fxnum&)
511DEFN_CTORS_T(const sc_fxnum_fast&)
514DEFN_CTORS_T(const sc_int_base&)
515DEFN_CTORS_T(const sc_uint_base&)
516DEFN_CTORS_T(const sc_signed&)
517DEFN_CTORS_T(const sc_unsigned&)
518
519#undef DEFN_CTORS_T
520
521// copy constructor
522
523template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
524inline
526: sc_fix_fast( a, W, I, Q, O, N )
527{}
528
529
530// assignment operators
531
532template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
533inline
536{
538 return *this;
539}
540
541#define DEFN_ASN_OP_T(op,tp) \
542template<int W, int I, sc_q_mode Q, sc_o_mode O, int N> \
543inline \
544sc_fixed_fast<W,I,Q,O,N>& \
545sc_fixed_fast<W,I,Q,O,N>::operator op ( tp a ) \
546{ \
547 sc_fix_fast::operator op ( a ); \
548 return *this; \
549}
550
551#define DEFN_ASN_OP_OTHER(op) \
552DEFN_ASN_OP_T(op,int64) \
553DEFN_ASN_OP_T(op,uint64) \
554DEFN_ASN_OP_T(op,const sc_int_base&) \
555DEFN_ASN_OP_T(op,const sc_uint_base&) \
556DEFN_ASN_OP_T(op,const sc_signed&) \
557DEFN_ASN_OP_T(op,const sc_unsigned&)
558
559#define DEFN_ASN_OP(op) \
560DEFN_ASN_OP_T(op,int) \
561DEFN_ASN_OP_T(op,unsigned int) \
562DEFN_ASN_OP_T(op,long) \
563DEFN_ASN_OP_T(op,unsigned long) \
564DEFN_ASN_OP_T(op,float) \
565DEFN_ASN_OP_T(op,double) \
566DEFN_ASN_OP_T(op,const char*) \
567DEFN_ASN_OP_T(op,const sc_fxval&) \
568DEFN_ASN_OP_T(op,const sc_fxval_fast&) \
569DEFN_ASN_OP_T(op,const sc_fxnum&) \
570DEFN_ASN_OP_T(op,const sc_fxnum_fast&) \
571DEFN_ASN_OP_OTHER(op)
572
574
575DEFN_ASN_OP(*=)
576DEFN_ASN_OP(/=)
577DEFN_ASN_OP(+=)
578DEFN_ASN_OP(-=)
579
580DEFN_ASN_OP_T(<<=,int)
581DEFN_ASN_OP_T(>>=,int)
582
583DEFN_ASN_OP_T(&=,const sc_fix&)
584DEFN_ASN_OP_T(&=,const sc_fix_fast&)
585DEFN_ASN_OP_T(|=,const sc_fix&)
586DEFN_ASN_OP_T(|=,const sc_fix_fast&)
587DEFN_ASN_OP_T(^=,const sc_fix&)
588DEFN_ASN_OP_T(^=,const sc_fix_fast&)
589
590#undef DEFN_ASN_OP_T
591#undef DEFN_ASN_OP_OTHER
592#undef DEFN_ASN_OP
593
594
595// auto-increment and auto-decrement
596
597template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
598inline
599sc_fxval_fast
601{
602 return sc_fxval_fast( sc_fix_fast::operator ++ ( 0 ) );
603}
604
605template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
606inline
609{
610 return sc_fxval_fast( sc_fix_fast::operator -- ( 0 ) );
611}
612
613template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
614inline
617{
619 return *this;
620}
621
622template<int W, int I, sc_q_mode Q, sc_o_mode O, int N>
623inline
626{
628 return *this;
629}
630
631} // namespace sc_dt
632
633
634#endif
635
636// Taf!
#define DECL_CTORS_T_A(tp)
Definition: sc_fixed.h:200
#define DECL_CTORS_T_B(tp)
Definition: sc_fixed.h:205
#define DEFN_ASN_OP(op)
Definition: sc_fixed.h:559
#define DEFN_ASN_OP_T(op, tp)
Definition: sc_fixed.h:541
#define DEFN_CTORS_T(tp)
Definition: sc_fixed.h:485
#define DECL_ASN_OP_T(op, tp)
Definition: sc_fixed.h:240
#define DECL_ASN_OP(op)
Definition: sc_fixed.h:251
sc_q_mode
Definition: sc_fxdefs.h:91
unsigned long long uint64
Definition: sc_nbdefs.h:216
const sc_q_mode SC_DEFAULT_Q_MODE_
Definition: sc_fxdefs.h:203
const sc_o_mode SC_DEFAULT_O_MODE_
Definition: sc_fxdefs.h:204
long long int64
Definition: sc_nbdefs.h:215
sc_o_mode
Definition: sc_fxdefs.h:120
const int SC_DEFAULT_N_BITS_
Definition: sc_fxdefs.h:205
sc_fix & operator=(const sc_fix &)
Definition: sc_fix.h:1108
sc_fix & operator--()
Definition: sc_fix.h:1211
sc_fix & operator++()
Definition: sc_fix.h:1203
sc_fix_fast & operator--()
Definition: sc_fix.h:1903
sc_fix_fast(sc_fxnum_fast_observer *=0)
Definition: sc_fix.h:1227
sc_fix_fast & operator++()
Definition: sc_fix.h:1895
sc_fix_fast & operator=(const sc_fix_fast &)
Definition: sc_fix.h:1800
sc_fixed & operator--()
Definition: sc_fixed.h:459
sc_fixed(sc_fxnum_observer *=0)
Definition: sc_fixed.h:308
sc_fixed & operator=(const sc_fixed< W, I, Q, O, N > &)
Definition: sc_fixed.h:369
sc_fixed & operator++()
Definition: sc_fixed.h:450
sc_fixed_fast(sc_fxnum_fast_observer *=0)
Definition: sc_fixed.h:474
sc_fixed_fast & operator--()
Definition: sc_fixed.h:625
sc_fixed_fast & operator=(const sc_fixed_fast< W, I, Q, O, N > &)
Definition: sc_fixed.h:535
sc_fixed_fast & operator++()
Definition: sc_fixed.h:616
friend class sc_fxval_fast
Definition: sc_fxnum.h:1027