scfx_pow10.cpp revision 12027:1eb7dc7aa10b
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 "sysc/datatypes/fx/scfx_pow10.h" 50 51 52namespace sc_dt 53{ 54 55// ---------------------------------------------------------------------------- 56// CLASS : scfx_pow10 57// 58// Class to compute (and cache) powers of 10 in arbitrary precision. 59// ---------------------------------------------------------------------------- 60 61scfx_pow10::scfx_pow10() 62{ 63 m_pos[0] = scfx_rep( 10.0 ); 64 m_neg[0] = scfx_rep( 0.1 ); 65 66 for( int i = 1; i < SCFX_POW10_TABLE_SIZE; i ++ ) 67 { 68 m_pos[i].set_nan(); 69 m_neg[i].set_nan(); 70 } 71} 72 73scfx_pow10::~scfx_pow10() 74{} 75 76 77const scfx_rep 78scfx_pow10::operator() ( int i ) 79{ 80 if( i == 0 ) { 81 return scfx_rep( 1.0 ); 82 } 83 84 if( i > 0 ) 85 { 86 int bit = scfx_find_msb( i ); 87 scfx_rep result = *pos( bit ); 88 if( bit ) 89 { 90 while( -- bit >= 0 ) 91 { 92 if( ( 1 << bit ) & i ) 93 { 94 scfx_rep* tmp = mult_scfx_rep( result, *pos( bit ) ); 95 result = *tmp; 96 delete tmp; 97 } 98 } 99 } 100 return result; 101 } 102 else 103 { 104 i = -i; 105 int bit = scfx_find_msb( i ); 106 scfx_rep result = *neg( bit ); 107 if( bit ) 108 { 109 while( -- bit >= 0 ) 110 { 111 if( ( 1 << bit ) & i ) 112 { 113 scfx_rep* tmp = mult_scfx_rep( result, *neg( bit ) ); 114 result = *tmp; 115 delete tmp; 116 } 117 } 118 } 119 return result; 120 } 121} 122 123 124scfx_rep* 125scfx_pow10::pos( int i ) 126{ 127 if( ! m_pos[i].is_normal() ) 128 { 129 multiply( m_pos[i], *pos( i - 1 ), *pos( i - 1 ) ); 130 } 131 return &m_pos[i]; 132} 133 134scfx_rep* 135scfx_pow10::neg( int i ) 136{ 137 if( ! m_neg[i].is_normal() ) 138 { 139 multiply( m_neg[i], *neg( i - 1 ), *neg( i - 1 ) ); 140 } 141 return &m_neg[i]; 142} 143 144} // namespace sc_dt 145 146 147// Taf! 148