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  scfx_pow10.cpp -
23
24  Original Author: Robert Graulich, Synopsys, Inc.
25                   Martin Janssen,  Synopsys, Inc.
26
27 *****************************************************************************/
28
29/*****************************************************************************
30
31  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
32  changes you are making here.
33
34      Name, Affiliation, Date:
35  Description of Modification:
36
37 *****************************************************************************/
38
39
40// $Log: scfx_pow10.cpp,v $
41// Revision 1.1.1.1  2006/12/15 20:20:04  acg
42// SystemC 2.3
43//
44// Revision 1.3  2006/01/13 18:53:58  acg
45// Andy Goodrich: added $Log command so that CVS comments are reproduced in
46// the source.
47//
48
49#include "systemc/ext/dt/fx/scfx_pow10.hh"
50
51namespace sc_dt
52{
53
54// ----------------------------------------------------------------------------
55//  CLASS : scfx_pow10
56//
57//  Class to compute (and cache) powers of 10 in arbitrary precision.
58// ----------------------------------------------------------------------------
59
60scfx_pow10::scfx_pow10()
61{
62    m_pos[0] = scfx_rep(10.0);
63    m_neg[0] = scfx_rep(0.1);
64
65    for (int i = 1; i < SCFX_POW10_TABLE_SIZE; i++) {
66        m_pos[i].set_nan();
67        m_neg[i].set_nan();
68    }
69}
70
71scfx_pow10::~scfx_pow10() {}
72
73const scfx_rep
74scfx_pow10::operator() (int i)
75{
76    if (i == 0) {
77        return scfx_rep(1.0);
78    }
79
80    if (i > 0) {
81        int bit = scfx_find_msb(i);
82        scfx_rep result = *pos(bit);
83        if (bit) {
84            while (--bit >= 0) {
85                if ((1 << bit) & i) {
86                    scfx_rep *tmp = mult_scfx_rep(result, *pos(bit));
87                    result = *tmp;
88                    delete tmp;
89                }
90            }
91        }
92        return result;
93    } else {
94        i = -i;
95        int bit = scfx_find_msb(i);
96        scfx_rep result = *neg(bit);
97        if (bit) {
98            while (--bit >= 0) {
99                if ((1 << bit) & i) {
100                    scfx_rep *tmp = mult_scfx_rep(result, *neg(bit));
101                    result = *tmp;
102                    delete tmp;
103                }
104            }
105        }
106        return result;
107    }
108}
109
110
111scfx_rep *
112scfx_pow10::pos(int i)
113{
114    if (!m_pos[i].is_normal()) {
115        multiply(m_pos[i], *pos(i - 1), *pos(i - 1));
116    }
117    return &m_pos[i];
118}
119
120scfx_rep *
121scfx_pow10::neg(int i)
122{
123    if (!m_neg[i].is_normal()) {
124        multiply(m_neg[i], *neg(i - 1), *neg(i - 1));
125    }
126    return &m_neg[i];
127}
128
129} // namespace sc_dt
130