00001 /* Decimal 64-bit format module for the decNumber C Library 00002 Copyright (C) 2005 Free Software Foundation, Inc. 00003 Contributed by IBM Corporation. Author Mike Cowlishaw. 00004 00005 This file is part of GCC. 00006 00007 GCC is free software; you can redistribute it and/or modify it under 00008 the terms of the GNU General Public License as published by the Free 00009 Software Foundation; either version 2, or (at your option) any later 00010 version. 00011 00012 In addition to the permissions in the GNU General Public License, 00013 the Free Software Foundation gives you unlimited permission to link 00014 the compiled version of this file into combinations with other 00015 programs, and to distribute those combinations without any 00016 restriction coming from the use of this file. (The General Public 00017 License restrictions do apply in other respects; for example, they 00018 cover modification of the file, and distribution when not linked 00019 into a combine executable.) 00020 00021 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 00022 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00023 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00024 for more details. 00025 00026 You should have received a copy of the GNU General Public License 00027 along with GCC; see the file COPYING. If not, write to the Free 00028 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 00029 02110-1301, USA. */ 00030 00031 /* ------------------------------------------------------------------ */ 00032 /* This module comprises the routines for decimal64 format numbers. */ 00033 /* Conversions are supplied to and from decNumber and String. */ 00034 /* */ 00035 /* No arithmetic routines are included; decNumber provides these. */ 00036 /* */ 00037 /* Error handling is the same as decNumber (qv.). */ 00038 /* ------------------------------------------------------------------ */ 00039 #include <string.h> /* [for memset/memcpy] */ 00040 #include <stdio.h> /* [for printf] */ 00041 00042 #define DECNUMDIGITS 16 /* we need decNumbers with space for 16 */ 00043 #include "config.h" 00044 #include "decNumber.h" /* base number library */ 00045 #include "decNumberLocal.h" /* decNumber local types, etc. */ 00046 #include "decimal64.h" /* our primary include */ 00047 #include "decUtility.h" /* utility routines */ 00048 00049 #if DECTRACE || DECCHECK 00050 void decimal64Show (const decimal64 *); /* for debug */ 00051 void decNumberShow (const decNumber *); /* .. */ 00052 #endif 00053 00054 /* Useful macro */ 00055 /* Clear a structure (e.g., a decNumber) */ 00056 #define DEC_clear(d) memset(d, 0, sizeof(*d)) 00057 00058 /* ------------------------------------------------------------------ */ 00059 /* decimal64FromNumber -- convert decNumber to decimal64 */ 00060 /* */ 00061 /* ds is the target decimal64 */ 00062 /* dn is the source number (assumed valid) */ 00063 /* set is the context, used only for reporting errors */ 00064 /* */ 00065 /* The set argument is used only for status reporting and for the */ 00066 /* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */ 00067 /* digits or an overflow is detected). If the exponent is out of the */ 00068 /* valid range then Overflow or Underflow will be raised. */ 00069 /* After Underflow a subnormal result is possible. */ 00070 /* */ 00071 /* DEC_Clamped is set if the number has to be 'folded down' to fit, */ 00072 /* by reducing its exponent and multiplying the coefficient by a */ 00073 /* power of ten, or if the exponent on a zero had to be clamped. */ 00074 /* ------------------------------------------------------------------ */ 00075 decimal64 * 00076 decimal64FromNumber (decimal64 * d64, const decNumber * dn, decContext * set) 00077 { 00078 uInt status = 0; /* status accumulator */ 00079 Int pad = 0; /* coefficient pad digits */ 00080 decNumber dw; /* work */ 00081 decContext dc; /* .. */ 00082 uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */ 00083 uInt comb, exp; /* work */ 00084 00085 /* If the number is finite, and has too many digits, or the exponent */ 00086 /* could be out of range then we reduce the number under the */ 00087 /* appropriate constraints */ 00088 if (!(dn->bits & DECSPECIAL)) 00089 { /* not a special value */ 00090 Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */ 00091 if (dn->digits > DECIMAL64_Pmax /* too many digits */ 00092 || ae > DECIMAL64_Emax /* likely overflow */ 00093 || ae < DECIMAL64_Emin) 00094 { /* likely underflow */ 00095 decContextDefault (&dc, DEC_INIT_DECIMAL64); /* [no traps] */ 00096 dc.round = set->round; /* use supplied rounding */ 00097 decNumberPlus (&dw, dn, &dc); /* (round and check) */ 00098 /* [this changes -0 to 0, but it will be restored below] */ 00099 status |= dc.status; /* save status */ 00100 dn = &dw; /* use the work number */ 00101 } 00102 /* [this could have pushed number to Infinity or zero, so this */ 00103 /* rounding must be done before we generate the decimal64] */ 00104 } 00105 00106 DEC_clear (d64); /* clean the target */ 00107 if (dn->bits & DECSPECIAL) 00108 { /* a special value */ 00109 uByte top; /* work */ 00110 if (dn->bits & DECINF) 00111 top = DECIMAL_Inf; 00112 else 00113 { /* sNaN or qNaN */ 00114 if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */ 00115 && (dn->digits < DECIMAL64_Pmax)) 00116 { /* coefficient fits */ 00117 decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), 0); 00118 } 00119 if (dn->bits & DECNAN) 00120 top = DECIMAL_NaN; 00121 else 00122 top = DECIMAL_sNaN; 00123 } 00124 d64->bytes[0] = top; 00125 } 00126 else if (decNumberIsZero (dn)) 00127 { /* a zero */ 00128 /* set and clamp exponent */ 00129 if (dn->exponent < -DECIMAL64_Bias) 00130 { 00131 exp = 0; 00132 status |= DEC_Clamped; 00133 } 00134 else 00135 { 00136 exp = dn->exponent + DECIMAL64_Bias; /* bias exponent */ 00137 if (exp > DECIMAL64_Ehigh) 00138 { /* top clamp */ 00139 exp = DECIMAL64_Ehigh; 00140 status |= DEC_Clamped; 00141 } 00142 } 00143 comb = (exp >> 5) & 0x18; /* combination field */ 00144 d64->bytes[0] = (uByte) (comb << 2); 00145 exp &= 0xff; /* remaining exponent bits */ 00146 decimal64SetExpCon (d64, exp); 00147 } 00148 else 00149 { /* non-zero finite number */ 00150 uInt msd; /* work */ 00151 00152 /* we have a dn that fits, but it may need to be padded */ 00153 exp = (uInt) (dn->exponent + DECIMAL64_Bias); /* bias exponent */ 00154 if (exp > DECIMAL64_Ehigh) 00155 { /* fold-down case */ 00156 pad = exp - DECIMAL64_Ehigh; 00157 exp = DECIMAL64_Ehigh; /* [to maximum] */ 00158 status |= DEC_Clamped; 00159 } 00160 00161 decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), pad); 00162 00163 /* save and clear the top digit */ 00164 msd = ((unsigned) d64->bytes[1] >> 2) & 0x0f; 00165 d64->bytes[1] &= 0x03; 00166 /* create the combination field */ 00167 if (msd >= 8) 00168 comb = 0x18 | (msd & 0x01) | ((exp >> 7) & 0x06); 00169 else 00170 comb = (msd & 0x07) | ((exp >> 5) & 0x18); 00171 d64->bytes[0] = (uByte) (comb << 2); 00172 exp &= 0xff; /* remaining exponent bits */ 00173 decimal64SetExpCon (d64, exp); 00174 } 00175 00176 if (isneg) 00177 decimal64SetSign (d64, 1); 00178 if (status != 0) 00179 decContextSetStatus (set, status); /* pass on status */ 00180 00181 /*decimal64Show(d64); */ 00182 return d64; 00183 } 00184 00185 /* ------------------------------------------------------------------ */ 00186 /* decimal64ToNumber -- convert decimal64 to decNumber */ 00187 /* d64 is the source decimal64 */ 00188 /* dn is the target number, with appropriate space */ 00189 /* No error is possible. */ 00190 /* ------------------------------------------------------------------ */ 00191 decNumber * 00192 decimal64ToNumber (const decimal64 * d64, decNumber * dn) 00193 { 00194 uInt msd; /* coefficient MSD */ 00195 decimal64 wk; /* working copy, if needed */ 00196 uInt top = d64->bytes[0] & 0x7f; /* top byte, less sign bit */ 00197 decNumberZero (dn); /* clean target */ 00198 /* set the sign if negative */ 00199 if (decimal64Sign (d64)) 00200 dn->bits = DECNEG; 00201 00202 if (top >= 0x78) 00203 { /* is a special */ 00204 if ((top & 0x7c) == (DECIMAL_Inf & 0x7c)) 00205 dn->bits |= DECINF; 00206 else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e)) 00207 dn->bits |= DECNAN; 00208 else 00209 dn->bits |= DECSNAN; 00210 msd = 0; /* no top digit */ 00211 } 00212 else 00213 { /* have a finite number */ 00214 uInt comb = top >> 2; /* combination field */ 00215 uInt exp; /* exponent */ 00216 00217 if (comb >= 0x18) 00218 { 00219 msd = 8 + (comb & 0x01); 00220 exp = (comb & 0x06) << 7; /* MSBs */ 00221 } 00222 else 00223 { 00224 msd = comb & 0x07; 00225 exp = (comb & 0x18) << 5; 00226 } 00227 dn->exponent = exp + decimal64ExpCon (d64) - DECIMAL64_Bias; /* remove bias */ 00228 } 00229 00230 /* get the coefficient, unless infinite */ 00231 if (!(dn->bits & DECINF)) 00232 { 00233 Int bunches = DECIMAL64_Pmax / 3; /* coefficient full bunches to convert */ 00234 Int odd = 0; /* assume MSD is 0 (no odd bunch) */ 00235 if (msd != 0) 00236 { /* coefficient has leading non-0 digit */ 00237 /* make a copy of the decimal64, with an extra bunch which has */ 00238 /* the top digit ready for conversion */ 00239 wk = *d64; /* take a copy */ 00240 wk.bytes[0] = 0; /* clear all but coecon */ 00241 wk.bytes[1] &= 0x03; /* .. */ 00242 wk.bytes[1] |= (msd << 2); /* and prefix MSD */ 00243 odd++; /* indicate the extra */ 00244 d64 = &wk; /* use the work copy */ 00245 } 00246 decDenseUnpackCoeff (d64->bytes, sizeof (d64->bytes), dn, bunches, odd); 00247 } 00248 return dn; 00249 } 00250 00251 /* ------------------------------------------------------------------ */ 00252 /* to-scientific-string -- conversion to numeric string */ 00253 /* to-engineering-string -- conversion to numeric string */ 00254 /* */ 00255 /* decimal64ToString(d64, string); */ 00256 /* decimal64ToEngString(d64, string); */ 00257 /* */ 00258 /* d64 is the decimal64 format number to convert */ 00259 /* string is the string where the result will be laid out */ 00260 /* */ 00261 /* string must be at least 24 characters */ 00262 /* */ 00263 /* No error is possible, and no status can be set. */ 00264 /* ------------------------------------------------------------------ */ 00265 char * 00266 decimal64ToString (const decimal64 * d64, char *string) 00267 { 00268 decNumber dn; /* work */ 00269 decimal64ToNumber (d64, &dn); 00270 decNumberToString (&dn, string); 00271 return string; 00272 } 00273 00274 char * 00275 decimal64ToEngString (const decimal64 * d64, char *string) 00276 { 00277 decNumber dn; /* work */ 00278 decimal64ToNumber (d64, &dn); 00279 decNumberToEngString (&dn, string); 00280 return string; 00281 } 00282 00283 /* ------------------------------------------------------------------ */ 00284 /* to-number -- conversion from numeric string */ 00285 /* */ 00286 /* decimal64FromString(result, string, set); */ 00287 /* */ 00288 /* result is the decimal64 format number which gets the result of */ 00289 /* the conversion */ 00290 /* *string is the character string which should contain a valid */ 00291 /* number (which may be a special value) */ 00292 /* set is the context */ 00293 /* */ 00294 /* The context is supplied to this routine is used for error handling */ 00295 /* (setting of status and traps) and for the rounding mode, only. */ 00296 /* If an error occurs, the result will be a valid decimal64 NaN. */ 00297 /* ------------------------------------------------------------------ */ 00298 decimal64 * 00299 decimal64FromString (decimal64 * result, const char *string, decContext * set) 00300 { 00301 decContext dc; /* work */ 00302 decNumber dn; /* .. */ 00303 00304 decContextDefault (&dc, DEC_INIT_DECIMAL64); /* no traps, please */ 00305 dc.round = set->round; /* use supplied rounding */ 00306 00307 decNumberFromString (&dn, string, &dc); /* will round if needed */ 00308 00309 decimal64FromNumber (result, &dn, &dc); 00310 if (dc.status != 0) 00311 { /* something happened */ 00312 decContextSetStatus (set, dc.status); /* .. pass it on */ 00313 } 00314 return result; 00315 } 00316 00317 #if DECTRACE || DECCHECK 00318 /* ------------------------------------------------------------------ */ 00319 /* decimal64Show -- display a single in hexadecimal [debug aid] */ 00320 /* d64 -- the number to show */ 00321 /* ------------------------------------------------------------------ */ 00322 /* Also shows sign/cob/expconfields extracted */ 00323 void 00324 decimal64Show (const decimal64 * d64) 00325 { 00326 char buf[DECIMAL64_Bytes * 2 + 1]; 00327 Int i, j; 00328 j = 0; 00329 for (i = 0; i < DECIMAL64_Bytes; i++) 00330 { 00331 sprintf (&buf[j], "%02x", d64->bytes[i]); 00332 j = j + 2; 00333 } 00334 printf (" D64> %s [S:%d Cb:%02x E:%d]\n", buf, 00335 decimal64Sign (d64), decimal64Comb (d64), decimal64ExpCon (d64)); 00336 } 00337 #endif
1.5.6