scfx_pow10.cc revision 12854
12428SN/A/*****************************************************************************
22428SN/A
32428SN/A  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
42428SN/A  more contributor license agreements.  See the NOTICE file distributed
52428SN/A  with this work for additional information regarding copyright ownership.
62428SN/A  Accellera licenses this file to you under the Apache License, Version 2.0
72428SN/A  (the "License"); you may not use this file except in compliance with the
82428SN/A  License.  You may obtain a copy of the License at
92428SN/A
102428SN/A    http://www.apache.org/licenses/LICENSE-2.0
112428SN/A
122428SN/A  Unless required by applicable law or agreed to in writing, software
132428SN/A  distributed under the License is distributed on an "AS IS" BASIS,
142428SN/A  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
152428SN/A  implied.  See the License for the specific language governing
162428SN/A  permissions and limitations under the License.
172428SN/A
182428SN/A *****************************************************************************/
192428SN/A
202428SN/A/*****************************************************************************
212428SN/A
222428SN/A  scfx_pow10.cpp -
232428SN/A
242428SN/A  Original Author: Robert Graulich, Synopsys, Inc.
252428SN/A                   Martin Janssen,  Synopsys, Inc.
262428SN/A
272665Ssaidi@eecs.umich.edu *****************************************************************************/
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu/*****************************************************************************
302428SN/A
312428SN/A  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
322428SN/A  changes you are making here.
332428SN/A
342428SN/A      Name, Affiliation, Date:
356214Snate@binkert.org  Description of Modification:
362428SN/A
375569Snate@binkert.org *****************************************************************************/
385569Snate@binkert.org
395569Snate@binkert.org
405569Snate@binkert.org// $Log: scfx_pow10.cpp,v $
415569Snate@binkert.org// Revision 1.1.1.1  2006/12/15 20:20:04  acg
425569Snate@binkert.org// SystemC 2.3
435569Snate@binkert.org//
445569Snate@binkert.org// Revision 1.3  2006/01/13 18:53:58  acg
455569Snate@binkert.org// Andy Goodrich: added $Log command so that CVS comments are reproduced in
465569Snate@binkert.org// the source.
475569Snate@binkert.org//
485569Snate@binkert.org
495569Snate@binkert.org#include "systemc/ext/dt/fx/scfx_pow10.hh"
502428SN/A
515569Snate@binkert.orgnamespace sc_dt
525569Snate@binkert.org{
535569Snate@binkert.org
545569Snate@binkert.org// ----------------------------------------------------------------------------
552428SN/A//  CLASS : scfx_pow10
562428SN/A//
572428SN/A//  Class to compute (and cache) powers of 10 in arbitrary precision.
585569Snate@binkert.org// ----------------------------------------------------------------------------
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