sign_extension.cpp revision 12855:588919e0e4aa
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  sign_extension.cpp --
23
24  Original Author: Philipp A. Hartmann, OFFIS, 2012-07-17
25
26 -----------------------------------------------------------------------------
27
28 This test demonatrates a bug in the sc_biguint constructor(?), where
29 construction from bit-wise expressions with "unsigned int" and sc_bigint
30 may lead to wrongly sign-extended values (at least on 32-bit machines) when
31 the destination size is bigger than the source size.
32
33 *****************************************************************************/
34
35#include <systemc>
36using sc_dt::sc_int;
37using sc_dt::sc_uint;
38using sc_dt::sc_bigint;
39using sc_dt::sc_biguint;
40using sc_dt::SC_BIN;
41
42#define DUMP_VAR( Var ) \
43  std::cout << #Var "=" << (Var).to_string(SC_BIN) << "\n"
44
45int sc_main(int, char*[])
46{
47
48    sc_int<31>     v_sc_int_31_min     = (1<<30); // 010...0 (31 bit)
49    sc_bigint<31>  v_sc_bigint_31_min  = (1<<30); // 010...0
50    unsigned int   v_uint_max          = ~0;      // 11....1 (32-bit)
51    sc_uint<32>    v_sc_uint32_max     = ~0;      // 11....1 (32-bit)
52    sc_biguint<32> v_sc_biguint_32_max = -1;      // 11....1 (32-bit)
53
54    DUMP_VAR( v_sc_int_31_min );
55    DUMP_VAR( v_sc_bigint_31_min );
56    DUMP_VAR( v_sc_uint32_max );
57    DUMP_VAR( v_sc_biguint_32_max );
58
59    sc_bigint<64> int31_uint         = (v_sc_int_31_min    & v_uint_max);
60    // the following expression yields 0b1....10...0 on 32-bit machines
61    sc_bigint<64> bigint31_uint      = (v_sc_bigint_31_min & v_uint_max);
62    sc_bigint<64> bigint31_uint32    = (v_sc_bigint_31_min & v_sc_uint32_max);
63    sc_bigint<64> bigint31_biguint32 = (v_sc_bigint_31_min & v_sc_biguint_32_max);
64
65    DUMP_VAR( v_sc_bigint_31_min & v_uint_max );
66    DUMP_VAR( v_sc_bigint_31_min & v_sc_biguint_32_max );
67
68    DUMP_VAR(int31_uint);
69    DUMP_VAR(bigint31_uint); // <--
70    DUMP_VAR(bigint31_uint32);
71    DUMP_VAR(bigint31_biguint32);
72
73    return 0;
74}
75
76