1169695Skan/* Decimal 64-bit format module for the decNumber C Library 2169695Skan Copyright (C) 2005 Free Software Foundation, Inc. 3169695Skan Contributed by IBM Corporation. Author Mike Cowlishaw. 4169695Skan 5169695Skan This file is part of GCC. 6169695Skan 7169695Skan GCC is free software; you can redistribute it and/or modify it under 8169695Skan the terms of the GNU General Public License as published by the Free 9169695Skan Software Foundation; either version 2, or (at your option) any later 10169695Skan version. 11169695Skan 12169695Skan In addition to the permissions in the GNU General Public License, 13169695Skan the Free Software Foundation gives you unlimited permission to link 14169695Skan the compiled version of this file into combinations with other 15169695Skan programs, and to distribute those combinations without any 16169695Skan restriction coming from the use of this file. (The General Public 17169695Skan License restrictions do apply in other respects; for example, they 18169695Skan cover modification of the file, and distribution when not linked 19169695Skan into a combine executable.) 20169695Skan 21169695Skan GCC is distributed in the hope that it will be useful, but WITHOUT ANY 22169695Skan WARRANTY; without even the implied warranty of MERCHANTABILITY or 23169695Skan FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 24169695Skan for more details. 25169695Skan 26169695Skan You should have received a copy of the GNU General Public License 27169695Skan along with GCC; see the file COPYING. If not, write to the Free 28169695Skan Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 29169695Skan 02110-1301, USA. */ 30169695Skan 31169695Skan/* ------------------------------------------------------------------ */ 32169695Skan/* This module comprises the routines for decimal64 format numbers. */ 33169695Skan/* Conversions are supplied to and from decNumber and String. */ 34169695Skan/* */ 35169695Skan/* No arithmetic routines are included; decNumber provides these. */ 36169695Skan/* */ 37169695Skan/* Error handling is the same as decNumber (qv.). */ 38169695Skan/* ------------------------------------------------------------------ */ 39169695Skan#include <string.h> /* [for memset/memcpy] */ 40169695Skan#include <stdio.h> /* [for printf] */ 41169695Skan 42169695Skan#define DECNUMDIGITS 16 /* we need decNumbers with space for 16 */ 43169695Skan#include "config.h" 44169695Skan#include "decNumber.h" /* base number library */ 45169695Skan#include "decNumberLocal.h" /* decNumber local types, etc. */ 46169695Skan#include "decimal64.h" /* our primary include */ 47169695Skan#include "decUtility.h" /* utility routines */ 48169695Skan 49169695Skan#if DECTRACE || DECCHECK 50169695Skanvoid decimal64Show (const decimal64 *); /* for debug */ 51169695Skanvoid decNumberShow (const decNumber *); /* .. */ 52169695Skan#endif 53169695Skan 54169695Skan/* Useful macro */ 55169695Skan/* Clear a structure (e.g., a decNumber) */ 56169695Skan#define DEC_clear(d) memset(d, 0, sizeof(*d)) 57169695Skan 58169695Skan/* ------------------------------------------------------------------ */ 59169695Skan/* decimal64FromNumber -- convert decNumber to decimal64 */ 60169695Skan/* */ 61169695Skan/* ds is the target decimal64 */ 62169695Skan/* dn is the source number (assumed valid) */ 63169695Skan/* set is the context, used only for reporting errors */ 64169695Skan/* */ 65169695Skan/* The set argument is used only for status reporting and for the */ 66169695Skan/* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */ 67169695Skan/* digits or an overflow is detected). If the exponent is out of the */ 68169695Skan/* valid range then Overflow or Underflow will be raised. */ 69169695Skan/* After Underflow a subnormal result is possible. */ 70169695Skan/* */ 71169695Skan/* DEC_Clamped is set if the number has to be 'folded down' to fit, */ 72169695Skan/* by reducing its exponent and multiplying the coefficient by a */ 73169695Skan/* power of ten, or if the exponent on a zero had to be clamped. */ 74169695Skan/* ------------------------------------------------------------------ */ 75169695Skandecimal64 * 76169695Skandecimal64FromNumber (decimal64 * d64, const decNumber * dn, decContext * set) 77169695Skan{ 78169695Skan uInt status = 0; /* status accumulator */ 79169695Skan Int pad = 0; /* coefficient pad digits */ 80169695Skan decNumber dw; /* work */ 81169695Skan decContext dc; /* .. */ 82169695Skan uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */ 83169695Skan uInt comb, exp; /* work */ 84169695Skan 85169695Skan /* If the number is finite, and has too many digits, or the exponent */ 86169695Skan /* could be out of range then we reduce the number under the */ 87169695Skan /* appropriate constraints */ 88169695Skan if (!(dn->bits & DECSPECIAL)) 89169695Skan { /* not a special value */ 90169695Skan Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */ 91169695Skan if (dn->digits > DECIMAL64_Pmax /* too many digits */ 92169695Skan || ae > DECIMAL64_Emax /* likely overflow */ 93169695Skan || ae < DECIMAL64_Emin) 94169695Skan { /* likely underflow */ 95169695Skan decContextDefault (&dc, DEC_INIT_DECIMAL64); /* [no traps] */ 96169695Skan dc.round = set->round; /* use supplied rounding */ 97169695Skan decNumberPlus (&dw, dn, &dc); /* (round and check) */ 98169695Skan /* [this changes -0 to 0, but it will be restored below] */ 99169695Skan status |= dc.status; /* save status */ 100169695Skan dn = &dw; /* use the work number */ 101169695Skan } 102169695Skan /* [this could have pushed number to Infinity or zero, so this */ 103169695Skan /* rounding must be done before we generate the decimal64] */ 104169695Skan } 105169695Skan 106169695Skan DEC_clear (d64); /* clean the target */ 107169695Skan if (dn->bits & DECSPECIAL) 108169695Skan { /* a special value */ 109169695Skan uByte top; /* work */ 110169695Skan if (dn->bits & DECINF) 111169695Skan top = DECIMAL_Inf; 112169695Skan else 113169695Skan { /* sNaN or qNaN */ 114169695Skan if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */ 115169695Skan && (dn->digits < DECIMAL64_Pmax)) 116169695Skan { /* coefficient fits */ 117169695Skan decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), 0); 118169695Skan } 119169695Skan if (dn->bits & DECNAN) 120169695Skan top = DECIMAL_NaN; 121169695Skan else 122169695Skan top = DECIMAL_sNaN; 123169695Skan } 124169695Skan d64->bytes[0] = top; 125169695Skan } 126169695Skan else if (decNumberIsZero (dn)) 127169695Skan { /* a zero */ 128169695Skan /* set and clamp exponent */ 129169695Skan if (dn->exponent < -DECIMAL64_Bias) 130169695Skan { 131169695Skan exp = 0; 132169695Skan status |= DEC_Clamped; 133169695Skan } 134169695Skan else 135169695Skan { 136169695Skan exp = dn->exponent + DECIMAL64_Bias; /* bias exponent */ 137169695Skan if (exp > DECIMAL64_Ehigh) 138169695Skan { /* top clamp */ 139169695Skan exp = DECIMAL64_Ehigh; 140169695Skan status |= DEC_Clamped; 141169695Skan } 142169695Skan } 143169695Skan comb = (exp >> 5) & 0x18; /* combination field */ 144169695Skan d64->bytes[0] = (uByte) (comb << 2); 145169695Skan exp &= 0xff; /* remaining exponent bits */ 146169695Skan decimal64SetExpCon (d64, exp); 147169695Skan } 148169695Skan else 149169695Skan { /* non-zero finite number */ 150169695Skan uInt msd; /* work */ 151169695Skan 152169695Skan /* we have a dn that fits, but it may need to be padded */ 153169695Skan exp = (uInt) (dn->exponent + DECIMAL64_Bias); /* bias exponent */ 154169695Skan if (exp > DECIMAL64_Ehigh) 155169695Skan { /* fold-down case */ 156169695Skan pad = exp - DECIMAL64_Ehigh; 157169695Skan exp = DECIMAL64_Ehigh; /* [to maximum] */ 158169695Skan status |= DEC_Clamped; 159169695Skan } 160169695Skan 161169695Skan decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), pad); 162169695Skan 163169695Skan /* save and clear the top digit */ 164169695Skan msd = ((unsigned) d64->bytes[1] >> 2) & 0x0f; 165169695Skan d64->bytes[1] &= 0x03; 166169695Skan /* create the combination field */ 167169695Skan if (msd >= 8) 168169695Skan comb = 0x18 | (msd & 0x01) | ((exp >> 7) & 0x06); 169169695Skan else 170169695Skan comb = (msd & 0x07) | ((exp >> 5) & 0x18); 171169695Skan d64->bytes[0] = (uByte) (comb << 2); 172169695Skan exp &= 0xff; /* remaining exponent bits */ 173169695Skan decimal64SetExpCon (d64, exp); 174169695Skan } 175169695Skan 176169695Skan if (isneg) 177169695Skan decimal64SetSign (d64, 1); 178169695Skan if (status != 0) 179169695Skan decContextSetStatus (set, status); /* pass on status */ 180169695Skan 181169695Skan /*decimal64Show(d64); */ 182169695Skan return d64; 183169695Skan} 184169695Skan 185169695Skan/* ------------------------------------------------------------------ */ 186169695Skan/* decimal64ToNumber -- convert decimal64 to decNumber */ 187169695Skan/* d64 is the source decimal64 */ 188169695Skan/* dn is the target number, with appropriate space */ 189169695Skan/* No error is possible. */ 190169695Skan/* ------------------------------------------------------------------ */ 191169695SkandecNumber * 192169695Skandecimal64ToNumber (const decimal64 * d64, decNumber * dn) 193169695Skan{ 194169695Skan uInt msd; /* coefficient MSD */ 195169695Skan decimal64 wk; /* working copy, if needed */ 196169695Skan uInt top = d64->bytes[0] & 0x7f; /* top byte, less sign bit */ 197169695Skan decNumberZero (dn); /* clean target */ 198169695Skan /* set the sign if negative */ 199169695Skan if (decimal64Sign (d64)) 200169695Skan dn->bits = DECNEG; 201169695Skan 202169695Skan if (top >= 0x78) 203169695Skan { /* is a special */ 204169695Skan if ((top & 0x7c) == (DECIMAL_Inf & 0x7c)) 205169695Skan dn->bits |= DECINF; 206169695Skan else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e)) 207169695Skan dn->bits |= DECNAN; 208169695Skan else 209169695Skan dn->bits |= DECSNAN; 210169695Skan msd = 0; /* no top digit */ 211169695Skan } 212169695Skan else 213169695Skan { /* have a finite number */ 214169695Skan uInt comb = top >> 2; /* combination field */ 215169695Skan uInt exp; /* exponent */ 216169695Skan 217169695Skan if (comb >= 0x18) 218169695Skan { 219169695Skan msd = 8 + (comb & 0x01); 220169695Skan exp = (comb & 0x06) << 7; /* MSBs */ 221169695Skan } 222169695Skan else 223169695Skan { 224169695Skan msd = comb & 0x07; 225169695Skan exp = (comb & 0x18) << 5; 226169695Skan } 227169695Skan dn->exponent = exp + decimal64ExpCon (d64) - DECIMAL64_Bias; /* remove bias */ 228169695Skan } 229169695Skan 230169695Skan /* get the coefficient, unless infinite */ 231169695Skan if (!(dn->bits & DECINF)) 232169695Skan { 233169695Skan Int bunches = DECIMAL64_Pmax / 3; /* coefficient full bunches to convert */ 234169695Skan Int odd = 0; /* assume MSD is 0 (no odd bunch) */ 235169695Skan if (msd != 0) 236169695Skan { /* coefficient has leading non-0 digit */ 237169695Skan /* make a copy of the decimal64, with an extra bunch which has */ 238169695Skan /* the top digit ready for conversion */ 239169695Skan wk = *d64; /* take a copy */ 240169695Skan wk.bytes[0] = 0; /* clear all but coecon */ 241169695Skan wk.bytes[1] &= 0x03; /* .. */ 242169695Skan wk.bytes[1] |= (msd << 2); /* and prefix MSD */ 243169695Skan odd++; /* indicate the extra */ 244169695Skan d64 = &wk; /* use the work copy */ 245169695Skan } 246169695Skan decDenseUnpackCoeff (d64->bytes, sizeof (d64->bytes), dn, bunches, odd); 247169695Skan } 248169695Skan return dn; 249169695Skan} 250169695Skan 251169695Skan/* ------------------------------------------------------------------ */ 252169695Skan/* to-scientific-string -- conversion to numeric string */ 253169695Skan/* to-engineering-string -- conversion to numeric string */ 254169695Skan/* */ 255169695Skan/* decimal64ToString(d64, string); */ 256169695Skan/* decimal64ToEngString(d64, string); */ 257169695Skan/* */ 258169695Skan/* d64 is the decimal64 format number to convert */ 259169695Skan/* string is the string where the result will be laid out */ 260169695Skan/* */ 261169695Skan/* string must be at least 24 characters */ 262169695Skan/* */ 263169695Skan/* No error is possible, and no status can be set. */ 264169695Skan/* ------------------------------------------------------------------ */ 265169695Skanchar * 266169695Skandecimal64ToString (const decimal64 * d64, char *string) 267169695Skan{ 268169695Skan decNumber dn; /* work */ 269169695Skan decimal64ToNumber (d64, &dn); 270169695Skan decNumberToString (&dn, string); 271169695Skan return string; 272169695Skan} 273169695Skan 274169695Skanchar * 275169695Skandecimal64ToEngString (const decimal64 * d64, char *string) 276169695Skan{ 277169695Skan decNumber dn; /* work */ 278169695Skan decimal64ToNumber (d64, &dn); 279169695Skan decNumberToEngString (&dn, string); 280169695Skan return string; 281169695Skan} 282169695Skan 283169695Skan/* ------------------------------------------------------------------ */ 284169695Skan/* to-number -- conversion from numeric string */ 285169695Skan/* */ 286169695Skan/* decimal64FromString(result, string, set); */ 287169695Skan/* */ 288169695Skan/* result is the decimal64 format number which gets the result of */ 289169695Skan/* the conversion */ 290169695Skan/* *string is the character string which should contain a valid */ 291169695Skan/* number (which may be a special value) */ 292169695Skan/* set is the context */ 293169695Skan/* */ 294169695Skan/* The context is supplied to this routine is used for error handling */ 295169695Skan/* (setting of status and traps) and for the rounding mode, only. */ 296169695Skan/* If an error occurs, the result will be a valid decimal64 NaN. */ 297169695Skan/* ------------------------------------------------------------------ */ 298169695Skandecimal64 * 299169695Skandecimal64FromString (decimal64 * result, const char *string, decContext * set) 300169695Skan{ 301169695Skan decContext dc; /* work */ 302169695Skan decNumber dn; /* .. */ 303169695Skan 304169695Skan decContextDefault (&dc, DEC_INIT_DECIMAL64); /* no traps, please */ 305169695Skan dc.round = set->round; /* use supplied rounding */ 306169695Skan 307169695Skan decNumberFromString (&dn, string, &dc); /* will round if needed */ 308169695Skan 309169695Skan decimal64FromNumber (result, &dn, &dc); 310169695Skan if (dc.status != 0) 311169695Skan { /* something happened */ 312169695Skan decContextSetStatus (set, dc.status); /* .. pass it on */ 313169695Skan } 314169695Skan return result; 315169695Skan} 316169695Skan 317169695Skan#if DECTRACE || DECCHECK 318169695Skan/* ------------------------------------------------------------------ */ 319169695Skan/* decimal64Show -- display a single in hexadecimal [debug aid] */ 320169695Skan/* d64 -- the number to show */ 321169695Skan/* ------------------------------------------------------------------ */ 322169695Skan/* Also shows sign/cob/expconfields extracted */ 323169695Skanvoid 324169695Skandecimal64Show (const decimal64 * d64) 325169695Skan{ 326169695Skan char buf[DECIMAL64_Bytes * 2 + 1]; 327169695Skan Int i, j; 328169695Skan j = 0; 329169695Skan for (i = 0; i < DECIMAL64_Bytes; i++) 330169695Skan { 331169695Skan sprintf (&buf[j], "%02x", d64->bytes[i]); 332169695Skan j = j + 2; 333169695Skan } 334169695Skan printf (" D64> %s [S:%d Cb:%02x E:%d]\n", buf, 335169695Skan decimal64Sign (d64), decimal64Comb (d64), decimal64ExpCon (d64)); 336169695Skan} 337169695Skan#endif 338