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_fxdefs.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_fxdefs.h,v $
39// Revision 1.1.1.1  2006/12/15 20:20:04  acg
40// SystemC 2.3
41//
42// Revision 1.3  2006/01/13 18:53:57  acg
43// Andy Goodrich: added $Log command so that CVS comments are reproduced in
44// the source.
45//
46
47#ifndef __SYSTEMC_EXT_DT_FX_SC_FXDEFS_HH__
48#define __SYSTEMC_EXT_DT_FX_SC_FXDEFS_HH__
49
50#include "../../utils/messages.hh"
51#include "../../utils/sc_report_handler.hh"
52#include "../int/sc_nbutils.hh"
53#include "messages.hh"
54
55#if ULONG_MAX > 0xffffffffUL
56#   define SC_LONG_64 1
57#else
58#   define SC_LONG_64 0
59#endif
60
61namespace sc_dt
62{
63
64// ----------------------------------------------------------------------------
65//  ENUM : sc_enc
66//
67//  Enumeration of sign encodings.
68// ----------------------------------------------------------------------------
69
70enum sc_enc
71{
72    SC_TC_, // two's complement
73    SC_US_ // unsigned
74};
75
76const std::string to_string(sc_enc);
77
78inline ::std::ostream &
79operator << (::std::ostream &os, sc_enc enc)
80{
81    return os << to_string(enc);
82}
83
84
85// ----------------------------------------------------------------------------
86//  ENUM : sc_q_mode
87//
88//  Enumeration of quantization modes.
89// ----------------------------------------------------------------------------
90
91enum sc_q_mode
92{
93    SC_RND, // rounding to plus infinity
94    SC_RND_ZERO, // rounding to zero
95    SC_RND_MIN_INF, // rounding to minus infinity
96    SC_RND_INF, // rounding to infinity
97    SC_RND_CONV, // convergent rounding
98    SC_TRN, // truncation
99    SC_TRN_ZERO // truncation to zero
100};
101
102const std::string to_string(sc_q_mode);
103
104inline ::std::ostream &
105operator << (::std::ostream &os, sc_q_mode q_mode)
106{
107    return os << to_string(q_mode);
108}
109
110
111// ----------------------------------------------------------------------------
112//  ENUM : sc_o_mode
113//
114//  Enumeration of overflow modes.
115// ----------------------------------------------------------------------------
116
117enum sc_o_mode
118{
119    SC_SAT, // saturation
120    SC_SAT_ZERO, // saturation to zero
121    SC_SAT_SYM, // symmetrical saturation
122    SC_WRAP, // wrap-around (*)
123    SC_WRAP_SM // sign magnitude wrap-around (*)
124};
125
126// (*) uses the number of saturated bits argument, see the documentation.
127
128const std::string to_string( sc_o_mode );
129
130inline ::std::ostream &
131operator << (::std::ostream &os, sc_o_mode o_mode)
132{
133    return os << to_string(o_mode);
134}
135
136
137// ----------------------------------------------------------------------------
138//  ENUM : sc_switch
139//
140//  Enumeration of switch states.
141// ----------------------------------------------------------------------------
142
143enum sc_switch
144{
145    SC_OFF,
146    SC_ON
147};
148
149const std::string to_string(sc_switch);
150
151inline ::std::ostream &
152operator << (::std::ostream &os, sc_switch sw)
153{
154    return os << to_string(sw);
155}
156
157
158// ----------------------------------------------------------------------------
159//  ENUM : sc_fmt
160//
161//  Enumeration of formats for character string conversion.
162// ----------------------------------------------------------------------------
163
164enum sc_fmt
165{
166    SC_F, // fixed
167    SC_E // scientific
168};
169
170const std::string to_string(sc_fmt);
171
172inline ::std::ostream &
173operator << (::std::ostream &os, sc_fmt fmt)
174{
175    return os << to_string(fmt);
176}
177
178
179// ----------------------------------------------------------------------------
180//  Built-in & default fixed-point type parameter values.
181// ----------------------------------------------------------------------------
182
183const int SC_BUILTIN_WL_ = 32;
184const int SC_BUILTIN_IWL_ = 32;
185const sc_q_mode SC_BUILTIN_Q_MODE_ = SC_TRN;
186const sc_o_mode SC_BUILTIN_O_MODE_ = SC_WRAP;
187const int SC_BUILTIN_N_BITS_ = 0;
188
189const int SC_DEFAULT_WL_ = SC_BUILTIN_WL_;
190const int SC_DEFAULT_IWL_ = SC_BUILTIN_IWL_;
191const sc_q_mode SC_DEFAULT_Q_MODE_ = SC_BUILTIN_Q_MODE_;
192const sc_o_mode SC_DEFAULT_O_MODE_ = SC_BUILTIN_O_MODE_;
193const int SC_DEFAULT_N_BITS_ = SC_BUILTIN_N_BITS_;
194
195
196// ----------------------------------------------------------------------------
197//  Built-in & default fixed-point cast switch parameter values.
198// ----------------------------------------------------------------------------
199
200const sc_switch SC_BUILTIN_CAST_SWITCH_ = SC_ON;
201const sc_switch SC_DEFAULT_CAST_SWITCH_ = SC_BUILTIN_CAST_SWITCH_;
202
203
204// ----------------------------------------------------------------------------
205//  Built-in & default fixed-point value type parameter values.
206// ----------------------------------------------------------------------------
207
208const int SC_BUILTIN_DIV_WL_ = 64;
209const int SC_BUILTIN_CTE_WL_ = 64;
210const int SC_BUILTIN_MAX_WL_ = 1024;
211
212
213#if defined(SC_FXDIV_WL) && (SC_FXDIV_WL > 0)
214const int SC_DEFAULT_DIV_WL_ = SC_FXDIV_WL;
215#else
216const int SC_DEFAULT_DIV_WL_ = SC_BUILTIN_DIV_WL_;
217#endif
218
219#if defined(SC_FXCTE_WL) && (SC_FXCTE_WL > 0)
220const int SC_DEFAULT_CTE_WL_ = SC_FXCTE_WL;
221#else
222const int SC_DEFAULT_CTE_WL_ = SC_BUILTIN_CTE_WL_;
223#endif
224
225#if defined(SC_FXMAX_WL) && (SC_FXMAX_WL > 0 || SC_FXMAX_WL == -1)
226const int SC_DEFAULT_MAX_WL_ = SC_FXMAX_WL;
227#else
228const int SC_DEFAULT_MAX_WL_ = SC_BUILTIN_MAX_WL_;
229#endif
230
231
232// ----------------------------------------------------------------------------
233//  Dedicated error reporting and checking.
234// ----------------------------------------------------------------------------
235
236#define SC_ERROR_IF_IMPL_(cnd, id, msg) \
237    do { \
238        if (cnd) { \
239            SC_REPORT_ERROR(id, msg); \
240            sc_core::sc_abort(); /* can't recover from here */ \
241        } \
242    } while ( false )
243
244#ifdef DEBUG_SYSTEMC
245#   define SC_ASSERT_(cnd, msg) \
246    SC_ERROR_IF_IMPL_(!(cnd), sc_core::SC_ID_INTERNAL_ERROR_, msg)
247#else
248#   define SC_ASSERT_(cnd, msg) (void(0))
249#endif
250
251#define SC_ERROR_IF_(cnd,id) SC_ERROR_IF_IMPL_(cnd, id, 0)
252
253#define SC_CHECK_WL_(wl) SC_ERROR_IF_((wl) <= 0, sc_core::SC_ID_INVALID_WL_)
254
255#define SC_CHECK_N_BITS_(n_bits) \
256    SC_ERROR_IF_((n_bits) < 0, sc_core::SC_ID_INVALID_N_BITS_)
257
258#define SC_CHECK_DIV_WL_(div_wl) \
259    SC_ERROR_IF_((div_wl) <= 0, sc_core::SC_ID_INVALID_DIV_WL_)
260
261#define SC_CHECK_CTE_WL_(cte_wl) \
262    SC_ERROR_IF_((cte_wl) <= 0, sc_core::SC_ID_INVALID_CTE_WL_)
263
264#define SC_CHECK_MAX_WL_(max_wl) \
265    SC_ERROR_IF_((max_wl) <= 0 && (max_wl) != -1, \
266            sc_core::SC_ID_INVALID_MAX_WL_)
267
268
269// ----------------------------------------------------------------------------
270//  Generic observer macros.
271// ----------------------------------------------------------------------------
272
273#define SC_OBSERVER_(object, observer_type, event) \
274{ \
275    if ((object).observer() != 0) { \
276        observer_type observer = (object).lock_observer(); \
277        observer->event((object)); \
278        (object).unlock_observer(observer); \
279    } \
280}
281
282#define SC_OBSERVER_DEFAULT_(observer_type) \
283{ \
284    if (m_observer == 0 && observer_type::default_observer != 0) \
285        m_observer = (*observer_type::default_observer)(); \
286}
287
288} // namespace sc_dt
289
290#endif // __SYSTEMC_EXT_DT_FX_SC_FXDEFS_HH__
291