14e98e3e1Schristos /* Common code for fixed-size types in the decNumber C Library.
2*4559860eSchristos Copyright (C) 2007-2018 Free Software Foundation, Inc.
34e98e3e1Schristos Contributed by IBM Corporation. Author Mike Cowlishaw.
44e98e3e1Schristos
54e98e3e1Schristos This file is part of GCC.
64e98e3e1Schristos
74e98e3e1Schristos GCC is free software; you can redistribute it and/or modify it under
84e98e3e1Schristos the terms of the GNU General Public License as published by the Free
94e98e3e1Schristos Software Foundation; either version 3, or (at your option) any later
104e98e3e1Schristos version.
114e98e3e1Schristos
124e98e3e1Schristos GCC is distributed in the hope that it will be useful, but WITHOUT ANY
134e98e3e1Schristos WARRANTY; without even the implied warranty of MERCHANTABILITY or
144e98e3e1Schristos FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
154e98e3e1Schristos for more details.
164e98e3e1Schristos
174e98e3e1Schristos Under Section 7 of GPL version 3, you are granted additional
184e98e3e1Schristos permissions described in the GCC Runtime Library Exception, version
194e98e3e1Schristos 3.1, as published by the Free Software Foundation.
204e98e3e1Schristos
214e98e3e1Schristos You should have received a copy of the GNU General Public License and
224e98e3e1Schristos a copy of the GCC Runtime Library Exception along with this program;
234e98e3e1Schristos see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
244e98e3e1Schristos <http://www.gnu.org/licenses/>. */
254e98e3e1Schristos
264e98e3e1Schristos /* ------------------------------------------------------------------ */
274e98e3e1Schristos /* decCommon.c -- common code for all three fixed-size types */
284e98e3e1Schristos /* ------------------------------------------------------------------ */
294e98e3e1Schristos /* This module comprises code that is shared between all the formats */
304e98e3e1Schristos /* (decSingle, decDouble, and decQuad); it includes set and extract */
314e98e3e1Schristos /* of format components, widening, narrowing, and string conversions. */
324e98e3e1Schristos /* */
334e98e3e1Schristos /* Unlike decNumber, parameterization takes place at compile time */
344e98e3e1Schristos /* rather than at runtime. The parameters are set in the decDouble.c */
354e98e3e1Schristos /* (etc.) files, which then include this one to produce the compiled */
364e98e3e1Schristos /* code. The functions here, therefore, are code shared between */
374e98e3e1Schristos /* multiple formats. */
384e98e3e1Schristos /* ------------------------------------------------------------------ */
394e98e3e1Schristos /* Names here refer to decFloat rather than to decDouble, etc., and */
404e98e3e1Schristos /* the functions are in strict alphabetical order. */
414e98e3e1Schristos /* Constants, tables, and debug function(s) are included only for QUAD */
424e98e3e1Schristos /* (which will always be compiled if DOUBLE or SINGLE are used). */
434e98e3e1Schristos /* */
444e98e3e1Schristos /* Whenever a decContext is used, only the status may be set (using */
454e98e3e1Schristos /* OR) or the rounding mode read; all other fields are ignored and */
464e98e3e1Schristos /* untouched. */
474e98e3e1Schristos
484e98e3e1Schristos #include "decCommonSymbols.h"
494e98e3e1Schristos
504e98e3e1Schristos /* names for simpler testing and default context */
514e98e3e1Schristos #if DECPMAX==7
524e98e3e1Schristos #define SINGLE 1
534e98e3e1Schristos #define DOUBLE 0
544e98e3e1Schristos #define QUAD 0
554e98e3e1Schristos #define DEFCONTEXT DEC_INIT_DECIMAL32
564e98e3e1Schristos #elif DECPMAX==16
574e98e3e1Schristos #define SINGLE 0
584e98e3e1Schristos #define DOUBLE 1
594e98e3e1Schristos #define QUAD 0
604e98e3e1Schristos #define DEFCONTEXT DEC_INIT_DECIMAL64
614e98e3e1Schristos #elif DECPMAX==34
624e98e3e1Schristos #define SINGLE 0
634e98e3e1Schristos #define DOUBLE 0
644e98e3e1Schristos #define QUAD 1
654e98e3e1Schristos #define DEFCONTEXT DEC_INIT_DECIMAL128
664e98e3e1Schristos #else
674e98e3e1Schristos #error Unexpected DECPMAX value
684e98e3e1Schristos #endif
694e98e3e1Schristos
704e98e3e1Schristos /* Assertions */
714e98e3e1Schristos
724e98e3e1Schristos #if DECPMAX!=7 && DECPMAX!=16 && DECPMAX!=34
734e98e3e1Schristos #error Unexpected Pmax (DECPMAX) value for this module
744e98e3e1Schristos #endif
754e98e3e1Schristos
764e98e3e1Schristos /* Assert facts about digit characters, etc. */
774e98e3e1Schristos #if ('9'&0x0f)!=9
784e98e3e1Schristos #error This module assumes characters are of the form 0b....nnnn
794e98e3e1Schristos /* where .... are don't care 4 bits and nnnn is 0000 through 1001 */
804e98e3e1Schristos #endif
814e98e3e1Schristos #if ('9'&0xf0)==('.'&0xf0)
824e98e3e1Schristos #error This module assumes '.' has a different mask than a digit
834e98e3e1Schristos #endif
844e98e3e1Schristos
854e98e3e1Schristos /* Assert ToString lay-out conditions */
864e98e3e1Schristos #if DECSTRING<DECPMAX+9
874e98e3e1Schristos #error ToString needs at least 8 characters for lead-in and dot
884e98e3e1Schristos #endif
894e98e3e1Schristos #if DECPMAX+DECEMAXD+5 > DECSTRING
904e98e3e1Schristos #error Exponent form can be too long for ToString to lay out safely
914e98e3e1Schristos #endif
924e98e3e1Schristos #if DECEMAXD > 4
934e98e3e1Schristos #error Exponent form is too long for ToString to lay out
944e98e3e1Schristos /* Note: code for up to 9 digits exists in archives [decOct] */
954e98e3e1Schristos #endif
964e98e3e1Schristos
974e98e3e1Schristos /* Private functions used here and possibly in decBasic.c, etc. */
984e98e3e1Schristos static decFloat * decFinalize(decFloat *, bcdnum *, decContext *);
994e98e3e1Schristos static Flag decBiStr(const char *, const char *, const char *);
1004e98e3e1Schristos
1014e98e3e1Schristos /* Macros and private tables; those which are not format-dependent */
1024e98e3e1Schristos /* are only included if decQuad is being built. */
1034e98e3e1Schristos
1044e98e3e1Schristos /* ------------------------------------------------------------------ */
1054e98e3e1Schristos /* Combination field lookup tables (uInts to save measurable work) */
1064e98e3e1Schristos /* */
1074e98e3e1Schristos /* DECCOMBEXP - 2 most-significant-bits of exponent (00, 01, or */
1084e98e3e1Schristos /* 10), shifted left for format, or DECFLOAT_Inf/NaN */
1094e98e3e1Schristos /* DECCOMBWEXP - The same, for the next-wider format (unless QUAD) */
1104e98e3e1Schristos /* DECCOMBMSD - 4-bit most-significant-digit */
1114e98e3e1Schristos /* [0 if the index is a special (Infinity or NaN)] */
1124e98e3e1Schristos /* DECCOMBFROM - 5-bit combination field from EXP top bits and MSD */
1134e98e3e1Schristos /* (placed in uInt so no shift is needed) */
1144e98e3e1Schristos /* */
1154e98e3e1Schristos /* DECCOMBEXP, DECCOMBWEXP, and DECCOMBMSD are indexed by the sign */
1164e98e3e1Schristos /* and 5-bit combination field (0-63, the second half of the table */
1174e98e3e1Schristos /* identical to the first half) */
1184e98e3e1Schristos /* DECCOMBFROM is indexed by expTopTwoBits*16 + msd */
1194e98e3e1Schristos /* */
1204e98e3e1Schristos /* DECCOMBMSD and DECCOMBFROM are not format-dependent and so are */
1214e98e3e1Schristos /* only included once, when QUAD is being built */
1224e98e3e1Schristos /* ------------------------------------------------------------------ */
1234e98e3e1Schristos static const uInt DECCOMBEXP[64]={
1244e98e3e1Schristos 0, 0, 0, 0, 0, 0, 0, 0,
1254e98e3e1Schristos 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
1264e98e3e1Schristos 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
1274e98e3e1Schristos 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
1284e98e3e1Schristos 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
1294e98e3e1Schristos 0, 0, 1<<DECECONL, 1<<DECECONL,
1304e98e3e1Schristos 2<<DECECONL, 2<<DECECONL, DECFLOAT_Inf, DECFLOAT_NaN,
1314e98e3e1Schristos 0, 0, 0, 0, 0, 0, 0, 0,
1324e98e3e1Schristos 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
1334e98e3e1Schristos 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
1344e98e3e1Schristos 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
1354e98e3e1Schristos 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
1364e98e3e1Schristos 0, 0, 1<<DECECONL, 1<<DECECONL,
1374e98e3e1Schristos 2<<DECECONL, 2<<DECECONL, DECFLOAT_Inf, DECFLOAT_NaN};
1384e98e3e1Schristos #if !QUAD
1394e98e3e1Schristos static const uInt DECCOMBWEXP[64]={
1404e98e3e1Schristos 0, 0, 0, 0, 0, 0, 0, 0,
1414e98e3e1Schristos 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
1424e98e3e1Schristos 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
1434e98e3e1Schristos 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
1444e98e3e1Schristos 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
1454e98e3e1Schristos 0, 0, 1<<DECWECONL, 1<<DECWECONL,
1464e98e3e1Schristos 2<<DECWECONL, 2<<DECWECONL, DECFLOAT_Inf, DECFLOAT_NaN,
1474e98e3e1Schristos 0, 0, 0, 0, 0, 0, 0, 0,
1484e98e3e1Schristos 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
1494e98e3e1Schristos 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
1504e98e3e1Schristos 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
1514e98e3e1Schristos 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
1524e98e3e1Schristos 0, 0, 1<<DECWECONL, 1<<DECWECONL,
1534e98e3e1Schristos 2<<DECWECONL, 2<<DECWECONL, DECFLOAT_Inf, DECFLOAT_NaN};
1544e98e3e1Schristos #endif
1554e98e3e1Schristos
1564e98e3e1Schristos #if QUAD
1574e98e3e1Schristos const uInt DECCOMBMSD[64]={
1584e98e3e1Schristos 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
1594e98e3e1Schristos 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0,
1604e98e3e1Schristos 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
1614e98e3e1Schristos 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0};
1624e98e3e1Schristos
1634e98e3e1Schristos const uInt DECCOMBFROM[48]={
1644e98e3e1Schristos 0x00000000, 0x04000000, 0x08000000, 0x0C000000, 0x10000000, 0x14000000,
1654e98e3e1Schristos 0x18000000, 0x1C000000, 0x60000000, 0x64000000, 0x00000000, 0x00000000,
1664e98e3e1Schristos 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000, 0x24000000,
1674e98e3e1Schristos 0x28000000, 0x2C000000, 0x30000000, 0x34000000, 0x38000000, 0x3C000000,
1684e98e3e1Schristos 0x68000000, 0x6C000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
1694e98e3e1Schristos 0x00000000, 0x00000000, 0x40000000, 0x44000000, 0x48000000, 0x4C000000,
1704e98e3e1Schristos 0x50000000, 0x54000000, 0x58000000, 0x5C000000, 0x70000000, 0x74000000,
1714e98e3e1Schristos 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000};
1724e98e3e1Schristos
1734e98e3e1Schristos /* ------------------------------------------------------------------ */
1744e98e3e1Schristos /* Request and include the tables to use for conversions */
1754e98e3e1Schristos /* ------------------------------------------------------------------ */
1764e98e3e1Schristos #define DEC_BCD2DPD 1 /* 0-0x999 -> DPD */
1774e98e3e1Schristos #define DEC_BIN2DPD 1 /* 0-999 -> DPD */
1784e98e3e1Schristos #define DEC_BIN2BCD8 1 /* 0-999 -> ddd, len */
1794e98e3e1Schristos #define DEC_DPD2BCD8 1 /* DPD -> ddd, len */
1804e98e3e1Schristos #define DEC_DPD2BIN 1 /* DPD -> 0-999 */
1814e98e3e1Schristos #define DEC_DPD2BINK 1 /* DPD -> 0-999000 */
1824e98e3e1Schristos #define DEC_DPD2BINM 1 /* DPD -> 0-999000000 */
1834e98e3e1Schristos #include "decDPD.h" /* source of the lookup tables */
1844e98e3e1Schristos
1854e98e3e1Schristos #endif
1864e98e3e1Schristos
1874e98e3e1Schristos /* ----------------------------------------------------------------- */
1884e98e3e1Schristos /* decBiStr -- compare string with pairwise options */
1894e98e3e1Schristos /* */
1904e98e3e1Schristos /* targ is the string to compare */
1914e98e3e1Schristos /* str1 is one of the strings to compare against (length may be 0) */
1924e98e3e1Schristos /* str2 is the other; it must be the same length as str1 */
1934e98e3e1Schristos /* */
1944e98e3e1Schristos /* returns 1 if strings compare equal, (that is, targ is the same */
1954e98e3e1Schristos /* length as str1 and str2, and each character of targ is in one */
1964e98e3e1Schristos /* of str1 or str2 in the corresponding position), or 0 otherwise */
1974e98e3e1Schristos /* */
1984e98e3e1Schristos /* This is used for generic caseless compare, including the awkward */
1994e98e3e1Schristos /* case of the Turkish dotted and dotless Is. Use as (for example): */
2004e98e3e1Schristos /* if (decBiStr(test, "mike", "MIKE")) ... */
2014e98e3e1Schristos /* ----------------------------------------------------------------- */
decBiStr(const char * targ,const char * str1,const char * str2)2024e98e3e1Schristos static Flag decBiStr(const char *targ, const char *str1, const char *str2) {
2034e98e3e1Schristos for (;;targ++, str1++, str2++) {
2044e98e3e1Schristos if (*targ!=*str1 && *targ!=*str2) return 0;
2054e98e3e1Schristos /* *targ has a match in one (or both, if terminator) */
2064e98e3e1Schristos if (*targ=='\0') break;
2074e98e3e1Schristos } /* forever */
2084e98e3e1Schristos return 1;
2094e98e3e1Schristos } /* decBiStr */
2104e98e3e1Schristos
2114e98e3e1Schristos /* ------------------------------------------------------------------ */
2124e98e3e1Schristos /* decFinalize -- adjust and store a final result */
2134e98e3e1Schristos /* */
2144e98e3e1Schristos /* df is the decFloat format number which gets the final result */
2154e98e3e1Schristos /* num is the descriptor of the number to be checked and encoded */
2164e98e3e1Schristos /* [its values, including the coefficient, may be modified] */
2174e98e3e1Schristos /* set is the context to use */
2184e98e3e1Schristos /* returns df */
2194e98e3e1Schristos /* */
2204e98e3e1Schristos /* The num descriptor may point to a bcd8 string of any length; this */
2214e98e3e1Schristos /* string may have leading insignificant zeros. If it has more than */
2224e98e3e1Schristos /* DECPMAX digits then the final digit can be a round-for-reround */
2234e98e3e1Schristos /* digit (i.e., it may include a sticky bit residue). */
2244e98e3e1Schristos /* */
2254e98e3e1Schristos /* The exponent (q) may be one of the codes for a special value and */
2264e98e3e1Schristos /* can be up to 999999999 for conversion from string. */
2274e98e3e1Schristos /* */
2284e98e3e1Schristos /* No error is possible, but Inexact, Underflow, and/or Overflow may */
2294e98e3e1Schristos /* be set. */
2304e98e3e1Schristos /* ------------------------------------------------------------------ */
2314e98e3e1Schristos /* Constant whose size varies with format; also the check for surprises */
2324e98e3e1Schristos static uByte allnines[DECPMAX]=
2334e98e3e1Schristos #if SINGLE
2344e98e3e1Schristos {9, 9, 9, 9, 9, 9, 9};
2354e98e3e1Schristos #elif DOUBLE
2364e98e3e1Schristos {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
2374e98e3e1Schristos #elif QUAD
2384e98e3e1Schristos {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
2394e98e3e1Schristos 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
2404e98e3e1Schristos #endif
2414e98e3e1Schristos
decFinalize(decFloat * df,bcdnum * num,decContext * set)2424e98e3e1Schristos static decFloat * decFinalize(decFloat *df, bcdnum *num,
2434e98e3e1Schristos decContext *set) {
2444e98e3e1Schristos uByte *ub; /* work */
2454e98e3e1Schristos uInt dpd; /* .. */
2464e98e3e1Schristos uInt uiwork; /* for macros */
2474e98e3e1Schristos uByte *umsd=num->msd; /* local copy */
2484e98e3e1Schristos uByte *ulsd=num->lsd; /* .. */
2494e98e3e1Schristos uInt encode; /* encoding accumulator */
2504e98e3e1Schristos Int length; /* coefficient length */
2514e98e3e1Schristos
2524e98e3e1Schristos #if DECCHECK
2534e98e3e1Schristos Int clen=ulsd-umsd+1;
2544e98e3e1Schristos #if QUAD
2554e98e3e1Schristos #define COEXTRA 2 /* extra-long coefficent */
2564e98e3e1Schristos #else
2574e98e3e1Schristos #define COEXTRA 0
2584e98e3e1Schristos #endif
2594e98e3e1Schristos if (clen<1 || clen>DECPMAX*3+2+COEXTRA)
2604e98e3e1Schristos printf("decFinalize: suspect coefficient [length=%ld]\n", (LI)clen);
2614e98e3e1Schristos if (num->sign!=0 && num->sign!=DECFLOAT_Sign)
2624e98e3e1Schristos printf("decFinalize: bad sign [%08lx]\n", (LI)num->sign);
2634e98e3e1Schristos if (!EXPISSPECIAL(num->exponent)
2644e98e3e1Schristos && (num->exponent>1999999999 || num->exponent<-1999999999))
2654e98e3e1Schristos printf("decFinalize: improbable exponent [%ld]\n", (LI)num->exponent);
2664e98e3e1Schristos /* decShowNum(num, "final"); */
2674e98e3e1Schristos #endif
2684e98e3e1Schristos
2694e98e3e1Schristos /* A special will have an 'exponent' which is very positive and a */
2704e98e3e1Schristos /* coefficient < DECPMAX */
2714e98e3e1Schristos length=(uInt)(ulsd-umsd+1); /* coefficient length */
2724e98e3e1Schristos
2734e98e3e1Schristos if (!NUMISSPECIAL(num)) {
2744e98e3e1Schristos Int drop; /* digits to be dropped */
2754e98e3e1Schristos /* skip leading insignificant zeros to calculate an exact length */
2764e98e3e1Schristos /* [this is quite expensive] */
2774e98e3e1Schristos if (*umsd==0) {
2784e98e3e1Schristos for (; umsd+3<ulsd && UBTOUI(umsd)==0;) umsd+=4;
2794e98e3e1Schristos for (; *umsd==0 && umsd<ulsd;) umsd++;
2804e98e3e1Schristos length=ulsd-umsd+1; /* recalculate */
2814e98e3e1Schristos }
2824e98e3e1Schristos drop=MAXI(length-DECPMAX, DECQTINY-num->exponent);
2834e98e3e1Schristos /* drop can now be > digits for bottom-clamp (subnormal) cases */
2844e98e3e1Schristos if (drop>0) { /* rounding needed */
2854e98e3e1Schristos /* (decFloatQuantize has very similar code to this, so any */
2864e98e3e1Schristos /* changes may need to be made there, too) */
2874e98e3e1Schristos uByte *roundat; /* -> re-round digit */
2884e98e3e1Schristos uByte reround; /* reround value */
2894e98e3e1Schristos /* printf("Rounding; drop=%ld\n", (LI)drop); */
2904e98e3e1Schristos
2914e98e3e1Schristos num->exponent+=drop; /* always update exponent */
2924e98e3e1Schristos
2934e98e3e1Schristos /* Three cases here: */
2944e98e3e1Schristos /* 1. new LSD is in coefficient (almost always) */
2954e98e3e1Schristos /* 2. new LSD is digit to left of coefficient (so MSD is */
2964e98e3e1Schristos /* round-for-reround digit) */
2974e98e3e1Schristos /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */
2984e98e3e1Schristos /* [duplicate check-stickies code to save a test] */
2994e98e3e1Schristos /* [by-digit check for stickies as runs of zeros are rare] */
3004e98e3e1Schristos if (drop<length) { /* NB lengths not addresses */
3014e98e3e1Schristos roundat=umsd+length-drop;
3024e98e3e1Schristos reround=*roundat;
3034e98e3e1Schristos for (ub=roundat+1; ub<=ulsd; ub++) {
3044e98e3e1Schristos if (*ub!=0) { /* non-zero to be discarded */
3054e98e3e1Schristos reround=DECSTICKYTAB[reround]; /* apply sticky bit */
3064e98e3e1Schristos break; /* [remainder don't-care] */
3074e98e3e1Schristos }
3084e98e3e1Schristos } /* check stickies */
3094e98e3e1Schristos ulsd=roundat-1; /* new LSD */
3104e98e3e1Schristos }
3114e98e3e1Schristos else { /* edge case */
3124e98e3e1Schristos if (drop==length) {
3134e98e3e1Schristos roundat=umsd;
3144e98e3e1Schristos reround=*roundat;
3154e98e3e1Schristos }
3164e98e3e1Schristos else {
3174e98e3e1Schristos roundat=umsd-1;
3184e98e3e1Schristos reround=0;
3194e98e3e1Schristos }
3204e98e3e1Schristos for (ub=roundat+1; ub<=ulsd; ub++) {
3214e98e3e1Schristos if (*ub!=0) { /* non-zero to be discarded */
3224e98e3e1Schristos reround=DECSTICKYTAB[reround]; /* apply sticky bit */
3234e98e3e1Schristos break; /* [remainder don't-care] */
3244e98e3e1Schristos }
3254e98e3e1Schristos } /* check stickies */
3264e98e3e1Schristos *umsd=0; /* coefficient is a 0 */
3274e98e3e1Schristos ulsd=umsd; /* .. */
3284e98e3e1Schristos }
3294e98e3e1Schristos
3304e98e3e1Schristos if (reround!=0) { /* discarding non-zero */
3314e98e3e1Schristos uInt bump=0;
3324e98e3e1Schristos set->status|=DEC_Inexact;
3334e98e3e1Schristos /* if adjusted exponent [exp+digits-1] is < EMIN then num is */
3344e98e3e1Schristos /* subnormal -- so raise Underflow */
3354e98e3e1Schristos if (num->exponent<DECEMIN && (num->exponent+(ulsd-umsd+1)-1)<DECEMIN)
3364e98e3e1Schristos set->status|=DEC_Underflow;
3374e98e3e1Schristos
3384e98e3e1Schristos /* next decide whether increment of the coefficient is needed */
3394e98e3e1Schristos if (set->round==DEC_ROUND_HALF_EVEN) { /* fastpath slowest case */
3404e98e3e1Schristos if (reround>5) bump=1; /* >0.5 goes up */
3414e98e3e1Schristos else if (reround==5) /* exactly 0.5000 .. */
3424e98e3e1Schristos bump=*ulsd & 0x01; /* .. up iff [new] lsd is odd */
3434e98e3e1Schristos } /* r-h-e */
3444e98e3e1Schristos else switch (set->round) {
3454e98e3e1Schristos case DEC_ROUND_DOWN: {
3464e98e3e1Schristos /* no change */
3474e98e3e1Schristos break;} /* r-d */
3484e98e3e1Schristos case DEC_ROUND_HALF_DOWN: {
3494e98e3e1Schristos if (reround>5) bump=1;
3504e98e3e1Schristos break;} /* r-h-d */
3514e98e3e1Schristos case DEC_ROUND_HALF_UP: {
3524e98e3e1Schristos if (reround>=5) bump=1;
3534e98e3e1Schristos break;} /* r-h-u */
3544e98e3e1Schristos case DEC_ROUND_UP: {
3554e98e3e1Schristos if (reround>0) bump=1;
3564e98e3e1Schristos break;} /* r-u */
3574e98e3e1Schristos case DEC_ROUND_CEILING: {
3584e98e3e1Schristos /* same as _UP for positive numbers, and as _DOWN for negatives */
3594e98e3e1Schristos if (!num->sign && reround>0) bump=1;
3604e98e3e1Schristos break;} /* r-c */
3614e98e3e1Schristos case DEC_ROUND_FLOOR: {
3624e98e3e1Schristos /* same as _UP for negative numbers, and as _DOWN for positive */
3634e98e3e1Schristos /* [negative reround cannot occur on 0] */
3644e98e3e1Schristos if (num->sign && reround>0) bump=1;
3654e98e3e1Schristos break;} /* r-f */
3664e98e3e1Schristos case DEC_ROUND_05UP: {
3674e98e3e1Schristos if (reround>0) { /* anything out there is 'sticky' */
3684e98e3e1Schristos /* bump iff lsd=0 or 5; this cannot carry so it could be */
3694e98e3e1Schristos /* effected immediately with no bump -- but the code */
3704e98e3e1Schristos /* is clearer if this is done the same way as the others */
3714e98e3e1Schristos if (*ulsd==0 || *ulsd==5) bump=1;
3724e98e3e1Schristos }
3734e98e3e1Schristos break;} /* r-r */
3744e98e3e1Schristos default: { /* e.g., DEC_ROUND_MAX */
3754e98e3e1Schristos set->status|=DEC_Invalid_context;
3764e98e3e1Schristos #if DECCHECK
3774e98e3e1Schristos printf("Unknown rounding mode: %ld\n", (LI)set->round);
3784e98e3e1Schristos #endif
3794e98e3e1Schristos break;}
3804e98e3e1Schristos } /* switch (not r-h-e) */
3814e98e3e1Schristos /* printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump); */
3824e98e3e1Schristos
3834e98e3e1Schristos if (bump!=0) { /* need increment */
3844e98e3e1Schristos /* increment the coefficient; this might end up with 1000... */
3854e98e3e1Schristos /* (after the all nines case) */
3864e98e3e1Schristos ub=ulsd;
3874e98e3e1Schristos for(; ub-3>=umsd && UBTOUI(ub-3)==0x09090909; ub-=4) {
3884e98e3e1Schristos UBFROMUI(ub-3, 0); /* to 00000000 */
3894e98e3e1Schristos }
3904e98e3e1Schristos /* [note ub could now be to left of msd, and it is not safe */
3914e98e3e1Schristos /* to write to the the left of the msd] */
3924e98e3e1Schristos /* now at most 3 digits left to non-9 (usually just the one) */
3934e98e3e1Schristos for (; ub>=umsd; *ub=0, ub--) {
3944e98e3e1Schristos if (*ub==9) continue; /* carry */
3954e98e3e1Schristos *ub+=1;
3964e98e3e1Schristos break;
3974e98e3e1Schristos }
3984e98e3e1Schristos if (ub<umsd) { /* had all-nines */
3994e98e3e1Schristos *umsd=1; /* coefficient to 1000... */
4004e98e3e1Schristos /* usually the 1000... coefficient can be used as-is */
4014e98e3e1Schristos if ((ulsd-umsd+1)==DECPMAX) {
4024e98e3e1Schristos num->exponent++;
4034e98e3e1Schristos }
4044e98e3e1Schristos else {
4054e98e3e1Schristos /* if coefficient is shorter than Pmax then num is */
4064e98e3e1Schristos /* subnormal, so extend it; this is safe as drop>0 */
4074e98e3e1Schristos /* (or, if the coefficient was supplied above, it could */
4084e98e3e1Schristos /* not be 9); this may make the result normal. */
4094e98e3e1Schristos ulsd++;
4104e98e3e1Schristos *ulsd=0;
4114e98e3e1Schristos /* [exponent unchanged] */
4124e98e3e1Schristos #if DECCHECK
4134e98e3e1Schristos if (num->exponent!=DECQTINY) /* sanity check */
4144e98e3e1Schristos printf("decFinalize: bad all-nines extend [^%ld, %ld]\n",
4154e98e3e1Schristos (LI)num->exponent, (LI)(ulsd-umsd+1));
4164e98e3e1Schristos #endif
4174e98e3e1Schristos } /* subnormal extend */
4184e98e3e1Schristos } /* had all-nines */
4194e98e3e1Schristos } /* bump needed */
4204e98e3e1Schristos } /* inexact rounding */
4214e98e3e1Schristos
4224e98e3e1Schristos length=ulsd-umsd+1; /* recalculate (may be <DECPMAX) */
4234e98e3e1Schristos } /* need round (drop>0) */
4244e98e3e1Schristos
4254e98e3e1Schristos /* The coefficient will now fit and has final length unless overflow */
4264e98e3e1Schristos /* decShowNum(num, "rounded"); */
4274e98e3e1Schristos
4284e98e3e1Schristos /* if exponent is >=emax may have to clamp, overflow, or fold-down */
4294e98e3e1Schristos if (num->exponent>DECEMAX-(DECPMAX-1)) { /* is edge case */
4304e98e3e1Schristos /* printf("overflow checks...\n"); */
4314e98e3e1Schristos if (*ulsd==0 && ulsd==umsd) { /* have zero */
4324e98e3e1Schristos num->exponent=DECEMAX-(DECPMAX-1); /* clamp to max */
4334e98e3e1Schristos }
4344e98e3e1Schristos else if ((num->exponent+length-1)>DECEMAX) { /* > Nmax */
4354e98e3e1Schristos /* Overflow -- these could go straight to encoding, here, but */
4364e98e3e1Schristos /* instead num is adjusted to keep the code cleaner */
4374e98e3e1Schristos Flag needmax=0; /* 1 for finite result */
4384e98e3e1Schristos set->status|=(DEC_Overflow | DEC_Inexact);
4394e98e3e1Schristos switch (set->round) {
4404e98e3e1Schristos case DEC_ROUND_DOWN: {
4414e98e3e1Schristos needmax=1; /* never Infinity */
4424e98e3e1Schristos break;} /* r-d */
4434e98e3e1Schristos case DEC_ROUND_05UP: {
4444e98e3e1Schristos needmax=1; /* never Infinity */
4454e98e3e1Schristos break;} /* r-05 */
4464e98e3e1Schristos case DEC_ROUND_CEILING: {
4474e98e3e1Schristos if (num->sign) needmax=1; /* Infinity iff non-negative */
4484e98e3e1Schristos break;} /* r-c */
4494e98e3e1Schristos case DEC_ROUND_FLOOR: {
4504e98e3e1Schristos if (!num->sign) needmax=1; /* Infinity iff negative */
4514e98e3e1Schristos break;} /* r-f */
4524e98e3e1Schristos default: break; /* Infinity in all other cases */
4534e98e3e1Schristos }
4544e98e3e1Schristos if (!needmax) { /* easy .. set Infinity */
4554e98e3e1Schristos num->exponent=DECFLOAT_Inf;
4564e98e3e1Schristos *umsd=0; /* be clean: coefficient to 0 */
4574e98e3e1Schristos ulsd=umsd; /* .. */
4584e98e3e1Schristos }
4594e98e3e1Schristos else { /* return Nmax */
4604e98e3e1Schristos umsd=allnines; /* use constant array */
4614e98e3e1Schristos ulsd=allnines+DECPMAX-1;
4624e98e3e1Schristos num->exponent=DECEMAX-(DECPMAX-1);
4634e98e3e1Schristos }
4644e98e3e1Schristos }
4654e98e3e1Schristos else { /* no overflow but non-zero and may have to fold-down */
4664e98e3e1Schristos Int shift=num->exponent-(DECEMAX-(DECPMAX-1));
4674e98e3e1Schristos if (shift>0) { /* fold-down needed */
4684e98e3e1Schristos /* fold down needed; must copy to buffer in order to pad */
4694e98e3e1Schristos /* with zeros safely; fortunately this is not the worst case */
4704e98e3e1Schristos /* path because cannot have had a round */
4714e98e3e1Schristos uByte buffer[ROUNDUP(DECPMAX+3, 4)]; /* [+3 allows uInt padding] */
4724e98e3e1Schristos uByte *s=umsd; /* source */
4734e98e3e1Schristos uByte *t=buffer; /* safe target */
4744e98e3e1Schristos uByte *tlsd=buffer+(ulsd-umsd)+shift; /* target LSD */
4754e98e3e1Schristos /* printf("folddown shift=%ld\n", (LI)shift); */
4764e98e3e1Schristos for (; s<=ulsd; s+=4, t+=4) UBFROMUI(t, UBTOUI(s));
4774e98e3e1Schristos for (t=tlsd-shift+1; t<=tlsd; t+=4) UBFROMUI(t, 0); /* pad 0s */
4784e98e3e1Schristos num->exponent-=shift;
4794e98e3e1Schristos umsd=buffer;
4804e98e3e1Schristos ulsd=tlsd;
4814e98e3e1Schristos }
4824e98e3e1Schristos } /* fold-down? */
4834e98e3e1Schristos length=ulsd-umsd+1; /* recalculate length */
4844e98e3e1Schristos } /* high-end edge case */
4854e98e3e1Schristos } /* finite number */
4864e98e3e1Schristos
4874e98e3e1Schristos /*------------------------------------------------------------------*/
4884e98e3e1Schristos /* At this point the result will properly fit the decFloat */
4894e98e3e1Schristos /* encoding, and it can be encoded with no possibility of error */
4904e98e3e1Schristos /*------------------------------------------------------------------*/
4914e98e3e1Schristos /* Following code does not alter coefficient (could be allnines array) */
4924e98e3e1Schristos
4934e98e3e1Schristos /* fast path possible when DECPMAX digits */
4944e98e3e1Schristos if (length==DECPMAX) {
4954e98e3e1Schristos return decFloatFromBCD(df, num->exponent, umsd, num->sign);
4964e98e3e1Schristos } /* full-length */
4974e98e3e1Schristos
4984e98e3e1Schristos /* slower path when not a full-length number; must care about length */
4994e98e3e1Schristos /* [coefficient length here will be < DECPMAX] */
5004e98e3e1Schristos if (!NUMISSPECIAL(num)) { /* is still finite */
5014e98e3e1Schristos /* encode the combination field and exponent continuation */
5024e98e3e1Schristos uInt uexp=(uInt)(num->exponent+DECBIAS); /* biased exponent */
5034e98e3e1Schristos uInt code=(uexp>>DECECONL)<<4; /* top two bits of exp */
5044e98e3e1Schristos /* [msd==0] */
5054e98e3e1Schristos /* look up the combination field and make high word */
5064e98e3e1Schristos encode=DECCOMBFROM[code]; /* indexed by (0-2)*16+msd */
5074e98e3e1Schristos encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; /* exponent continuation */
5084e98e3e1Schristos }
5094e98e3e1Schristos else encode=num->exponent; /* special [already in word] */
5104e98e3e1Schristos encode|=num->sign; /* add sign */
5114e98e3e1Schristos
5124e98e3e1Schristos /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */
5134e98e3e1Schristos /* refers to the declet from the least significant three digits) */
5144e98e3e1Schristos /* and put the corresponding DPD code into dpd. Access to umsd and */
5154e98e3e1Schristos /* ulsd (pointers to the most and least significant digit of the */
5164e98e3e1Schristos /* variable-length coefficient) is assumed, along with use of a */
5174e98e3e1Schristos /* working pointer, uInt *ub. */
5184e98e3e1Schristos /* As not full-length then chances are there are many leading zeros */
5194e98e3e1Schristos /* [and there may be a partial triad] */
5204e98e3e1Schristos #define getDPDt(dpd, n) ub=ulsd-(3*(n))-2; \
5214e98e3e1Schristos if (ub<umsd-2) dpd=0; \
5224e98e3e1Schristos else if (ub>=umsd) dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)]; \
5234e98e3e1Schristos else {dpd=*(ub+2); if (ub+1==umsd) dpd+=*(ub+1)*16; dpd=BCD2DPD[dpd];}
5244e98e3e1Schristos
5254e98e3e1Schristos /* place the declets in the encoding words and copy to result (df), */
5264e98e3e1Schristos /* according to endianness; in all cases complete the sign word */
5274e98e3e1Schristos /* first */
5284e98e3e1Schristos #if DECPMAX==7
5294e98e3e1Schristos getDPDt(dpd, 1);
5304e98e3e1Schristos encode|=dpd<<10;
5314e98e3e1Schristos getDPDt(dpd, 0);
5324e98e3e1Schristos encode|=dpd;
5334e98e3e1Schristos DFWORD(df, 0)=encode; /* just the one word */
5344e98e3e1Schristos
5354e98e3e1Schristos #elif DECPMAX==16
5364e98e3e1Schristos getDPDt(dpd, 4); encode|=dpd<<8;
5374e98e3e1Schristos getDPDt(dpd, 3); encode|=dpd>>2;
5384e98e3e1Schristos DFWORD(df, 0)=encode;
5394e98e3e1Schristos encode=dpd<<30;
5404e98e3e1Schristos getDPDt(dpd, 2); encode|=dpd<<20;
5414e98e3e1Schristos getDPDt(dpd, 1); encode|=dpd<<10;
5424e98e3e1Schristos getDPDt(dpd, 0); encode|=dpd;
5434e98e3e1Schristos DFWORD(df, 1)=encode;
5444e98e3e1Schristos
5454e98e3e1Schristos #elif DECPMAX==34
5464e98e3e1Schristos getDPDt(dpd,10); encode|=dpd<<4;
5474e98e3e1Schristos getDPDt(dpd, 9); encode|=dpd>>6;
5484e98e3e1Schristos DFWORD(df, 0)=encode;
5494e98e3e1Schristos
5504e98e3e1Schristos encode=dpd<<26;
5514e98e3e1Schristos getDPDt(dpd, 8); encode|=dpd<<16;
5524e98e3e1Schristos getDPDt(dpd, 7); encode|=dpd<<6;
5534e98e3e1Schristos getDPDt(dpd, 6); encode|=dpd>>4;
5544e98e3e1Schristos DFWORD(df, 1)=encode;
5554e98e3e1Schristos
5564e98e3e1Schristos encode=dpd<<28;
5574e98e3e1Schristos getDPDt(dpd, 5); encode|=dpd<<18;
5584e98e3e1Schristos getDPDt(dpd, 4); encode|=dpd<<8;
5594e98e3e1Schristos getDPDt(dpd, 3); encode|=dpd>>2;
5604e98e3e1Schristos DFWORD(df, 2)=encode;
5614e98e3e1Schristos
5624e98e3e1Schristos encode=dpd<<30;
5634e98e3e1Schristos getDPDt(dpd, 2); encode|=dpd<<20;
5644e98e3e1Schristos getDPDt(dpd, 1); encode|=dpd<<10;
5654e98e3e1Schristos getDPDt(dpd, 0); encode|=dpd;
5664e98e3e1Schristos DFWORD(df, 3)=encode;
5674e98e3e1Schristos #endif
5684e98e3e1Schristos
5694e98e3e1Schristos /* printf("Status: %08lx\n", (LI)set->status); */
5704e98e3e1Schristos /* decFloatShow(df, "final2"); */
5714e98e3e1Schristos return df;
5724e98e3e1Schristos } /* decFinalize */
5734e98e3e1Schristos
5744e98e3e1Schristos /* ------------------------------------------------------------------ */
5754e98e3e1Schristos /* decFloatFromBCD -- set decFloat from exponent, BCD8, and sign */
5764e98e3e1Schristos /* */
5774e98e3e1Schristos /* df is the target decFloat */
5784e98e3e1Schristos /* exp is the in-range unbiased exponent, q, or a special value in */
5794e98e3e1Schristos /* the form returned by decFloatGetExponent */
5804e98e3e1Schristos /* bcdar holds DECPMAX digits to set the coefficient from, one */
5814e98e3e1Schristos /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
5824e98e3e1Schristos /* if df is a NaN; all are ignored if df is infinite. */
5834e98e3e1Schristos /* All bytes must be in 0-9; results are undefined otherwise. */
5844e98e3e1Schristos /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
5854e98e3e1Schristos /* returns df, which will be canonical */
5864e98e3e1Schristos /* */
5874e98e3e1Schristos /* No error is possible, and no status will be set. */
5884e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatFromBCD(decFloat * df,Int exp,const uByte * bcdar,Int sig)5894e98e3e1Schristos decFloat * decFloatFromBCD(decFloat *df, Int exp, const uByte *bcdar,
5904e98e3e1Schristos Int sig) {
5914e98e3e1Schristos uInt encode, dpd; /* work */
5924e98e3e1Schristos const uByte *ub; /* .. */
5934e98e3e1Schristos
5944e98e3e1Schristos if (EXPISSPECIAL(exp)) encode=exp|sig;/* specials already encoded */
5954e98e3e1Schristos else { /* is finite */
5964e98e3e1Schristos /* encode the combination field and exponent continuation */
5974e98e3e1Schristos uInt uexp=(uInt)(exp+DECBIAS); /* biased exponent */
5984e98e3e1Schristos uInt code=(uexp>>DECECONL)<<4; /* top two bits of exp */
5994e98e3e1Schristos code+=bcdar[0]; /* add msd */
6004e98e3e1Schristos /* look up the combination field and make high word */
6014e98e3e1Schristos encode=DECCOMBFROM[code]|sig; /* indexed by (0-2)*16+msd */
6024e98e3e1Schristos encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; /* exponent continuation */
6034e98e3e1Schristos }
6044e98e3e1Schristos
6054e98e3e1Schristos /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */
6064e98e3e1Schristos /* refers to the declet from the least significant three digits) */
6074e98e3e1Schristos /* and put the corresponding DPD code into dpd. */
6084e98e3e1Schristos /* Use of a working pointer, uInt *ub, is assumed. */
6094e98e3e1Schristos
6104e98e3e1Schristos #define getDPDb(dpd, n) ub=bcdar+DECPMAX-1-(3*(n))-2; \
6114e98e3e1Schristos dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)];
6124e98e3e1Schristos
6134e98e3e1Schristos /* place the declets in the encoding words and copy to result (df), */
6144e98e3e1Schristos /* according to endianness; in all cases complete the sign word */
6154e98e3e1Schristos /* first */
6164e98e3e1Schristos #if DECPMAX==7
6174e98e3e1Schristos getDPDb(dpd, 1);
6184e98e3e1Schristos encode|=dpd<<10;
6194e98e3e1Schristos getDPDb(dpd, 0);
6204e98e3e1Schristos encode|=dpd;
6214e98e3e1Schristos DFWORD(df, 0)=encode; /* just the one word */
6224e98e3e1Schristos
6234e98e3e1Schristos #elif DECPMAX==16
6244e98e3e1Schristos getDPDb(dpd, 4); encode|=dpd<<8;
6254e98e3e1Schristos getDPDb(dpd, 3); encode|=dpd>>2;
6264e98e3e1Schristos DFWORD(df, 0)=encode;
6274e98e3e1Schristos encode=dpd<<30;
6284e98e3e1Schristos getDPDb(dpd, 2); encode|=dpd<<20;
6294e98e3e1Schristos getDPDb(dpd, 1); encode|=dpd<<10;
6304e98e3e1Schristos getDPDb(dpd, 0); encode|=dpd;
6314e98e3e1Schristos DFWORD(df, 1)=encode;
6324e98e3e1Schristos
6334e98e3e1Schristos #elif DECPMAX==34
6344e98e3e1Schristos getDPDb(dpd,10); encode|=dpd<<4;
6354e98e3e1Schristos getDPDb(dpd, 9); encode|=dpd>>6;
6364e98e3e1Schristos DFWORD(df, 0)=encode;
6374e98e3e1Schristos
6384e98e3e1Schristos encode=dpd<<26;
6394e98e3e1Schristos getDPDb(dpd, 8); encode|=dpd<<16;
6404e98e3e1Schristos getDPDb(dpd, 7); encode|=dpd<<6;
6414e98e3e1Schristos getDPDb(dpd, 6); encode|=dpd>>4;
6424e98e3e1Schristos DFWORD(df, 1)=encode;
6434e98e3e1Schristos
6444e98e3e1Schristos encode=dpd<<28;
6454e98e3e1Schristos getDPDb(dpd, 5); encode|=dpd<<18;
6464e98e3e1Schristos getDPDb(dpd, 4); encode|=dpd<<8;
6474e98e3e1Schristos getDPDb(dpd, 3); encode|=dpd>>2;
6484e98e3e1Schristos DFWORD(df, 2)=encode;
6494e98e3e1Schristos
6504e98e3e1Schristos encode=dpd<<30;
6514e98e3e1Schristos getDPDb(dpd, 2); encode|=dpd<<20;
6524e98e3e1Schristos getDPDb(dpd, 1); encode|=dpd<<10;
6534e98e3e1Schristos getDPDb(dpd, 0); encode|=dpd;
6544e98e3e1Schristos DFWORD(df, 3)=encode;
6554e98e3e1Schristos #endif
6564e98e3e1Schristos /* decFloatShow(df, "fromB"); */
6574e98e3e1Schristos return df;
6584e98e3e1Schristos } /* decFloatFromBCD */
6594e98e3e1Schristos
6604e98e3e1Schristos /* ------------------------------------------------------------------ */
6614e98e3e1Schristos /* decFloatFromPacked -- set decFloat from exponent and packed BCD */
6624e98e3e1Schristos /* */
6634e98e3e1Schristos /* df is the target decFloat */
6644e98e3e1Schristos /* exp is the in-range unbiased exponent, q, or a special value in */
6654e98e3e1Schristos /* the form returned by decFloatGetExponent */
6664e98e3e1Schristos /* packed holds DECPMAX packed decimal digits plus a sign nibble */
6674e98e3e1Schristos /* (all 6 codes are OK); the first (MSD) is ignored if df is a NaN */
6684e98e3e1Schristos /* and all except sign are ignored if df is infinite. For DOUBLE */
6694e98e3e1Schristos /* and QUAD the first (pad) nibble is also ignored in all cases. */
6704e98e3e1Schristos /* All coefficient nibbles must be in 0-9 and sign in A-F; results */
6714e98e3e1Schristos /* are undefined otherwise. */
6724e98e3e1Schristos /* returns df, which will be canonical */
6734e98e3e1Schristos /* */
6744e98e3e1Schristos /* No error is possible, and no status will be set. */
6754e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatFromPacked(decFloat * df,Int exp,const uByte * packed)6764e98e3e1Schristos decFloat * decFloatFromPacked(decFloat *df, Int exp, const uByte *packed) {
6774e98e3e1Schristos uByte bcdar[DECPMAX+2]; /* work [+1 for pad, +1 for sign] */
6784e98e3e1Schristos const uByte *ip; /* .. */
6794e98e3e1Schristos uByte *op; /* .. */
6804e98e3e1Schristos Int sig=0; /* sign */
6814e98e3e1Schristos
6824e98e3e1Schristos /* expand coefficient and sign to BCDAR */
6834e98e3e1Schristos #if SINGLE
6844e98e3e1Schristos op=bcdar+1; /* no pad digit */
6854e98e3e1Schristos #else
6864e98e3e1Schristos op=bcdar; /* first (pad) digit ignored */
6874e98e3e1Schristos #endif
6884e98e3e1Schristos for (ip=packed; ip<packed+((DECPMAX+2)/2); ip++) {
6894e98e3e1Schristos *op++=*ip>>4;
6904e98e3e1Schristos *op++=(uByte)(*ip&0x0f); /* [final nibble is sign] */
6914e98e3e1Schristos }
6924e98e3e1Schristos op--; /* -> sign byte */
6934e98e3e1Schristos if (*op==DECPMINUS || *op==DECPMINUSALT) sig=DECFLOAT_Sign;
6944e98e3e1Schristos
6954e98e3e1Schristos if (EXPISSPECIAL(exp)) { /* Infinity or NaN */
6964e98e3e1Schristos if (!EXPISINF(exp)) bcdar[1]=0; /* a NaN: ignore MSD */
6974e98e3e1Schristos else memset(bcdar+1, 0, DECPMAX); /* Infinite: coefficient to 0 */
6984e98e3e1Schristos }
6994e98e3e1Schristos return decFloatFromBCD(df, exp, bcdar+1, sig);
7004e98e3e1Schristos } /* decFloatFromPacked */
7014e98e3e1Schristos
7024e98e3e1Schristos /* ------------------------------------------------------------------ */
7034e98e3e1Schristos /* decFloatFromPackedChecked -- set from exponent and packed; checked */
7044e98e3e1Schristos /* */
7054e98e3e1Schristos /* df is the target decFloat */
7064e98e3e1Schristos /* exp is the in-range unbiased exponent, q, or a special value in */
7074e98e3e1Schristos /* the form returned by decFloatGetExponent */
7084e98e3e1Schristos /* packed holds DECPMAX packed decimal digits plus a sign nibble */
7094e98e3e1Schristos /* (all 6 codes are OK); the first (MSD) must be 0 if df is a NaN */
7104e98e3e1Schristos /* and all digits must be 0 if df is infinite. For DOUBLE and */
7114e98e3e1Schristos /* QUAD the first (pad) nibble must be 0. */
7124e98e3e1Schristos /* All coefficient nibbles must be in 0-9 and sign in A-F. */
7134e98e3e1Schristos /* returns df, which will be canonical or NULL if any of the */
7144e98e3e1Schristos /* requirements are not met (if this case df is unchanged); that */
7154e98e3e1Schristos /* is, the input data must be as returned by decFloatToPacked, */
716*4559860eSchristos /* except that all six sign codes are accepted. */
7174e98e3e1Schristos /* */
7184e98e3e1Schristos /* No status will be set. */
7194e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatFromPackedChecked(decFloat * df,Int exp,const uByte * packed)7204e98e3e1Schristos decFloat * decFloatFromPackedChecked(decFloat *df, Int exp,
7214e98e3e1Schristos const uByte *packed) {
7224e98e3e1Schristos uByte bcdar[DECPMAX+2]; /* work [+1 for pad, +1 for sign] */
7234e98e3e1Schristos const uByte *ip; /* .. */
7244e98e3e1Schristos uByte *op; /* .. */
7254e98e3e1Schristos Int sig=0; /* sign */
7264e98e3e1Schristos
7274e98e3e1Schristos /* expand coefficient and sign to BCDAR */
7284e98e3e1Schristos #if SINGLE
7294e98e3e1Schristos op=bcdar+1; /* no pad digit */
7304e98e3e1Schristos #else
7314e98e3e1Schristos op=bcdar; /* first (pad) digit here */
7324e98e3e1Schristos #endif
7334e98e3e1Schristos for (ip=packed; ip<packed+((DECPMAX+2)/2); ip++) {
7344e98e3e1Schristos *op=*ip>>4;
7354e98e3e1Schristos if (*op>9) return NULL;
7364e98e3e1Schristos op++;
7374e98e3e1Schristos *op=(uByte)(*ip&0x0f); /* [final nibble is sign] */
7384e98e3e1Schristos if (*op>9 && ip<packed+((DECPMAX+2)/2)-1) return NULL;
7394e98e3e1Schristos op++;
7404e98e3e1Schristos }
7414e98e3e1Schristos op--; /* -> sign byte */
7424e98e3e1Schristos if (*op<=9) return NULL; /* bad sign */
7434e98e3e1Schristos if (*op==DECPMINUS || *op==DECPMINUSALT) sig=DECFLOAT_Sign;
7444e98e3e1Schristos
7454e98e3e1Schristos #if !SINGLE
7464e98e3e1Schristos if (bcdar[0]!=0) return NULL; /* bad pad nibble */
7474e98e3e1Schristos #endif
7484e98e3e1Schristos
7494e98e3e1Schristos if (EXPISNAN(exp)) { /* a NaN */
7504e98e3e1Schristos if (bcdar[1]!=0) return NULL; /* bad msd */
7514e98e3e1Schristos } /* NaN */
7524e98e3e1Schristos else if (EXPISINF(exp)) { /* is infinite */
7534e98e3e1Schristos Int i;
7544e98e3e1Schristos for (i=0; i<DECPMAX; i++) {
7554e98e3e1Schristos if (bcdar[i+1]!=0) return NULL; /* should be all zeros */
7564e98e3e1Schristos }
7574e98e3e1Schristos } /* infinity */
7584e98e3e1Schristos else { /* finite */
7594e98e3e1Schristos /* check the exponent is in range */
7604e98e3e1Schristos if (exp>DECEMAX-DECPMAX+1) return NULL;
7614e98e3e1Schristos if (exp<DECEMIN-DECPMAX+1) return NULL;
7624e98e3e1Schristos }
7634e98e3e1Schristos return decFloatFromBCD(df, exp, bcdar+1, sig);
7644e98e3e1Schristos } /* decFloatFromPacked */
7654e98e3e1Schristos
7664e98e3e1Schristos /* ------------------------------------------------------------------ */
7674e98e3e1Schristos /* decFloatFromString -- conversion from numeric string */
7684e98e3e1Schristos /* */
7694e98e3e1Schristos /* result is the decFloat format number which gets the result of */
7704e98e3e1Schristos /* the conversion */
7714e98e3e1Schristos /* *string is the character string which should contain a valid */
7724e98e3e1Schristos /* number (which may be a special value), \0-terminated */
7734e98e3e1Schristos /* If there are too many significant digits in the */
7744e98e3e1Schristos /* coefficient it will be rounded. */
7754e98e3e1Schristos /* set is the context */
7764e98e3e1Schristos /* returns result */
7774e98e3e1Schristos /* */
7784e98e3e1Schristos /* The length of the coefficient and the size of the exponent are */
7794e98e3e1Schristos /* checked by this routine, so the correct error (Underflow or */
7804e98e3e1Schristos /* Overflow) can be reported or rounding applied, as necessary. */
7814e98e3e1Schristos /* */
7824e98e3e1Schristos /* There is no limit to the coefficient length for finite inputs; */
7834e98e3e1Schristos /* NaN payloads must be integers with no more than DECPMAX-1 digits. */
7844e98e3e1Schristos /* Exponents may have up to nine significant digits. */
7854e98e3e1Schristos /* */
7864e98e3e1Schristos /* If bad syntax is detected, the result will be a quiet NaN. */
7874e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatFromString(decFloat * result,const char * string,decContext * set)7884e98e3e1Schristos decFloat * decFloatFromString(decFloat *result, const char *string,
7894e98e3e1Schristos decContext *set) {
7904e98e3e1Schristos Int digits; /* count of digits in coefficient */
7914e98e3e1Schristos const char *dotchar=NULL; /* where dot was found [NULL if none] */
7924e98e3e1Schristos const char *cfirst=string; /* -> first character of decimal part */
7934e98e3e1Schristos const char *c; /* work */
7944e98e3e1Schristos uByte *ub; /* .. */
7954e98e3e1Schristos uInt uiwork; /* for macros */
7964e98e3e1Schristos bcdnum num; /* collects data for finishing */
7974e98e3e1Schristos uInt error=DEC_Conversion_syntax; /* assume the worst */
7984e98e3e1Schristos uByte buffer[ROUNDUP(DECSTRING+11, 8)]; /* room for most coefficents, */
7994e98e3e1Schristos /* some common rounding, +3, & pad */
8004e98e3e1Schristos #if DECTRACE
8014e98e3e1Schristos /* printf("FromString %s ...\n", string); */
8024e98e3e1Schristos #endif
8034e98e3e1Schristos
8044e98e3e1Schristos for(;;) { /* once-only 'loop' */
8054e98e3e1Schristos num.sign=0; /* assume non-negative */
8064e98e3e1Schristos num.msd=buffer; /* MSD is here always */
8074e98e3e1Schristos
8084e98e3e1Schristos /* detect and validate the coefficient, including any leading, */
8094e98e3e1Schristos /* trailing, or embedded '.' */
8104e98e3e1Schristos /* [could test four-at-a-time here (saving 10% for decQuads), */
8114e98e3e1Schristos /* but that risks storage violation because the position of the */
8124e98e3e1Schristos /* terminator is unknown] */
8134e98e3e1Schristos for (c=string;; c++) { /* -> input character */
8144e98e3e1Schristos if (((unsigned)(*c-'0'))<=9) continue; /* '0' through '9' is good */
8154e98e3e1Schristos if (*c=='\0') break; /* most common non-digit */
8164e98e3e1Schristos if (*c=='.') {
8174e98e3e1Schristos if (dotchar!=NULL) break; /* not first '.' */
8184e98e3e1Schristos dotchar=c; /* record offset into decimal part */
8194e98e3e1Schristos continue;}
8204e98e3e1Schristos if (c==string) { /* first in string... */
8214e98e3e1Schristos if (*c=='-') { /* valid - sign */
8224e98e3e1Schristos cfirst++;
8234e98e3e1Schristos num.sign=DECFLOAT_Sign;
8244e98e3e1Schristos continue;}
8254e98e3e1Schristos if (*c=='+') { /* valid + sign */
8264e98e3e1Schristos cfirst++;
8274e98e3e1Schristos continue;}
8284e98e3e1Schristos }
8294e98e3e1Schristos /* *c is not a digit, terminator, or a valid +, -, or '.' */
8304e98e3e1Schristos break;
8314e98e3e1Schristos } /* c loop */
8324e98e3e1Schristos
8334e98e3e1Schristos digits=(uInt)(c-cfirst); /* digits (+1 if a dot) */
8344e98e3e1Schristos
8354e98e3e1Schristos if (digits>0) { /* had digits and/or dot */
8364e98e3e1Schristos const char *clast=c-1; /* note last coefficient char position */
8374e98e3e1Schristos Int exp=0; /* exponent accumulator */
8384e98e3e1Schristos if (*c!='\0') { /* something follows the coefficient */
8394e98e3e1Schristos uInt edig; /* unsigned work */
8404e98e3e1Schristos /* had some digits and more to come; expect E[+|-]nnn now */
8414e98e3e1Schristos const char *firstexp; /* exponent first non-zero */
8424e98e3e1Schristos if (*c!='E' && *c!='e') break;
8434e98e3e1Schristos c++; /* to (optional) sign */
8444e98e3e1Schristos if (*c=='-' || *c=='+') c++; /* step over sign (c=clast+2) */
8454e98e3e1Schristos if (*c=='\0') break; /* no digits! (e.g., '1.2E') */
8464e98e3e1Schristos for (; *c=='0';) c++; /* skip leading zeros [even last] */
8474e98e3e1Schristos firstexp=c; /* remember start [maybe '\0'] */
8484e98e3e1Schristos /* gather exponent digits */
8494e98e3e1Schristos edig=(uInt)*c-(uInt)'0';
8504e98e3e1Schristos if (edig<=9) { /* [check not bad or terminator] */
8514e98e3e1Schristos exp+=edig; /* avoid initial X10 */
8524e98e3e1Schristos c++;
8534e98e3e1Schristos for (;; c++) {
8544e98e3e1Schristos edig=(uInt)*c-(uInt)'0';
8554e98e3e1Schristos if (edig>9) break;
8564e98e3e1Schristos exp=exp*10+edig;
8574e98e3e1Schristos }
8584e98e3e1Schristos }
8594e98e3e1Schristos /* if not now on the '\0', *c must not be a digit */
8604e98e3e1Schristos if (*c!='\0') break;
8614e98e3e1Schristos
8624e98e3e1Schristos /* (this next test must be after the syntax checks) */
8634e98e3e1Schristos /* if definitely more than the possible digits for format then */
8644e98e3e1Schristos /* the exponent may have wrapped, so simply set it to a certain */
8654e98e3e1Schristos /* over/underflow value */
8664e98e3e1Schristos if (c>firstexp+DECEMAXD) exp=DECEMAX*2;
8674e98e3e1Schristos if (*(clast+2)=='-') exp=-exp; /* was negative */
8684e98e3e1Schristos } /* digits>0 */
8694e98e3e1Schristos
8704e98e3e1Schristos if (dotchar!=NULL) { /* had a '.' */
8714e98e3e1Schristos digits--; /* remove from digits count */
8724e98e3e1Schristos if (digits==0) break; /* was dot alone: bad syntax */
8734e98e3e1Schristos exp-=(Int)(clast-dotchar); /* adjust exponent */
8744e98e3e1Schristos /* [the '.' can now be ignored] */
8754e98e3e1Schristos }
8764e98e3e1Schristos num.exponent=exp; /* exponent is good; store it */
8774e98e3e1Schristos
8784e98e3e1Schristos /* Here when whole string has been inspected and syntax is good */
8794e98e3e1Schristos /* cfirst->first digit or dot, clast->last digit or dot */
8804e98e3e1Schristos error=0; /* no error possible now */
8814e98e3e1Schristos
8824e98e3e1Schristos /* if the number of digits in the coefficient will fit in buffer */
8834e98e3e1Schristos /* then it can simply be converted to bcd8 and copied -- decFinalize */
8844e98e3e1Schristos /* will take care of leading zeros and rounding; the buffer is big */
8854e98e3e1Schristos /* enough for all canonical coefficients, including 0.00000nn... */
8864e98e3e1Schristos ub=buffer;
8874e98e3e1Schristos if (digits<=(Int)(sizeof(buffer)-3)) { /* [-3 allows by-4s copy] */
8884e98e3e1Schristos c=cfirst;
8894e98e3e1Schristos if (dotchar!=NULL) { /* a dot to worry about */
8904e98e3e1Schristos if (*(c+1)=='.') { /* common canonical case */
8914e98e3e1Schristos *ub++=(uByte)(*c-'0'); /* copy leading digit */
8924e98e3e1Schristos c+=2; /* prepare to handle rest */
8934e98e3e1Schristos }
8944e98e3e1Schristos else for (; c<=clast;) { /* '.' could be anywhere */
8954e98e3e1Schristos /* as usual, go by fours when safe; NB it has been asserted */
8964e98e3e1Schristos /* that a '.' does not have the same mask as a digit */
8974e98e3e1Schristos if (c<=clast-3 /* safe for four */
8984e98e3e1Schristos && (UBTOUI(c)&0xf0f0f0f0)==CHARMASK) { /* test four */
8994e98e3e1Schristos UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f); /* to BCD8 */
9004e98e3e1Schristos ub+=4;
9014e98e3e1Schristos c+=4;
9024e98e3e1Schristos continue;
9034e98e3e1Schristos }
9044e98e3e1Schristos if (*c=='.') { /* found the dot */
9054e98e3e1Schristos c++; /* step over it .. */
9064e98e3e1Schristos break; /* .. and handle the rest */
9074e98e3e1Schristos }
9084e98e3e1Schristos *ub++=(uByte)(*c++-'0');
9094e98e3e1Schristos }
9104e98e3e1Schristos } /* had dot */
9114e98e3e1Schristos /* Now no dot; do this by fours (where safe) */
9124e98e3e1Schristos for (; c<=clast-3; c+=4, ub+=4) UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f);
9134e98e3e1Schristos for (; c<=clast; c++, ub++) *ub=(uByte)(*c-'0');
9144e98e3e1Schristos num.lsd=buffer+digits-1; /* record new LSD */
9154e98e3e1Schristos } /* fits */
9164e98e3e1Schristos
9174e98e3e1Schristos else { /* too long for buffer */
9184e98e3e1Schristos /* [This is a rare and unusual case; arbitrary-length input] */
9194e98e3e1Schristos /* strip leading zeros [but leave final 0 if all 0's] */
9204e98e3e1Schristos if (*cfirst=='.') cfirst++; /* step past dot at start */
9214e98e3e1Schristos if (*cfirst=='0') { /* [cfirst always -> digit] */
9224e98e3e1Schristos for (; cfirst<clast; cfirst++) {
9234e98e3e1Schristos if (*cfirst!='0') { /* non-zero found */
9244e98e3e1Schristos if (*cfirst=='.') continue; /* [ignore] */
9254e98e3e1Schristos break; /* done */
9264e98e3e1Schristos }
9274e98e3e1Schristos digits--; /* 0 stripped */
9284e98e3e1Schristos } /* cfirst */
9294e98e3e1Schristos } /* at least one leading 0 */
9304e98e3e1Schristos
9314e98e3e1Schristos /* the coefficient is now as short as possible, but may still */
9324e98e3e1Schristos /* be too long; copy up to Pmax+1 digits to the buffer, then */
9334e98e3e1Schristos /* just record any non-zeros (set round-for-reround digit) */
9344e98e3e1Schristos for (c=cfirst; c<=clast && ub<=buffer+DECPMAX; c++) {
9354e98e3e1Schristos /* (see commentary just above) */
9364e98e3e1Schristos if (c<=clast-3 /* safe for four */
9374e98e3e1Schristos && (UBTOUI(c)&0xf0f0f0f0)==CHARMASK) { /* four digits */
9384e98e3e1Schristos UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f); /* to BCD8 */
9394e98e3e1Schristos ub+=4;
9404e98e3e1Schristos c+=3; /* [will become 4] */
9414e98e3e1Schristos continue;
9424e98e3e1Schristos }
9434e98e3e1Schristos if (*c=='.') continue; /* [ignore] */
9444e98e3e1Schristos *ub++=(uByte)(*c-'0');
9454e98e3e1Schristos }
9464e98e3e1Schristos ub--; /* -> LSD */
9474e98e3e1Schristos for (; c<=clast; c++) { /* inspect remaining chars */
9484e98e3e1Schristos if (*c!='0') { /* sticky bit needed */
9494e98e3e1Schristos if (*c=='.') continue; /* [ignore] */
9504e98e3e1Schristos *ub=DECSTICKYTAB[*ub]; /* update round-for-reround */
9514e98e3e1Schristos break; /* no need to look at more */
9524e98e3e1Schristos }
9534e98e3e1Schristos }
9544e98e3e1Schristos num.lsd=ub; /* record LSD */
9554e98e3e1Schristos /* adjust exponent for dropped digits */
9564e98e3e1Schristos num.exponent+=digits-(Int)(ub-buffer+1);
9574e98e3e1Schristos } /* too long for buffer */
9584e98e3e1Schristos } /* digits or dot */
9594e98e3e1Schristos
9604e98e3e1Schristos else { /* no digits or dot were found */
9614e98e3e1Schristos if (*c=='\0') break; /* nothing to come is bad */
9624e98e3e1Schristos /* only Infinities and NaNs are allowed, here */
9634e98e3e1Schristos buffer[0]=0; /* default a coefficient of 0 */
9644e98e3e1Schristos num.lsd=buffer; /* .. */
9654e98e3e1Schristos if (decBiStr(c, "infinity", "INFINITY")
9664e98e3e1Schristos || decBiStr(c, "inf", "INF")) num.exponent=DECFLOAT_Inf;
9674e98e3e1Schristos else { /* should be a NaN */
9684e98e3e1Schristos num.exponent=DECFLOAT_qNaN; /* assume quiet NaN */
9694e98e3e1Schristos if (*c=='s' || *c=='S') { /* probably an sNaN */
9704e98e3e1Schristos c++;
9714e98e3e1Schristos num.exponent=DECFLOAT_sNaN; /* assume is in fact sNaN */
9724e98e3e1Schristos }
9734e98e3e1Schristos if (*c!='N' && *c!='n') break; /* check caseless "NaN" */
9744e98e3e1Schristos c++;
9754e98e3e1Schristos if (*c!='a' && *c!='A') break; /* .. */
9764e98e3e1Schristos c++;
9774e98e3e1Schristos if (*c!='N' && *c!='n') break; /* .. */
9784e98e3e1Schristos c++;
9794e98e3e1Schristos /* now either nothing, or nnnn payload (no dots), expected */
9804e98e3e1Schristos /* -> start of integer, and skip leading 0s [including plain 0] */
9814e98e3e1Schristos for (cfirst=c; *cfirst=='0';) cfirst++;
9824e98e3e1Schristos if (*cfirst!='\0') { /* not empty or all-0, payload */
9834e98e3e1Schristos /* payload found; check all valid digits and copy to buffer as bcd8 */
9844e98e3e1Schristos ub=buffer;
9854e98e3e1Schristos for (c=cfirst;; c++, ub++) {
9864e98e3e1Schristos if ((unsigned)(*c-'0')>9) break; /* quit if not 0-9 */
9874e98e3e1Schristos if (c-cfirst==DECPMAX-1) break; /* too many digits */
9884e98e3e1Schristos *ub=(uByte)(*c-'0'); /* good bcd8 */
9894e98e3e1Schristos }
9904e98e3e1Schristos if (*c!='\0') break; /* not all digits, or too many */
9914e98e3e1Schristos num.lsd=ub-1; /* record new LSD */
9924e98e3e1Schristos }
9934e98e3e1Schristos } /* NaN or sNaN */
9944e98e3e1Schristos error=0; /* syntax is OK */
9954e98e3e1Schristos break; /* done with specials */
9964e98e3e1Schristos } /* digits=0 (special expected) */
9974e98e3e1Schristos break;
9984e98e3e1Schristos } /* [for(;;) break] */
9994e98e3e1Schristos
10004e98e3e1Schristos /* decShowNum(&num, "fromStr"); */
10014e98e3e1Schristos
10024e98e3e1Schristos if (error!=0) {
10034e98e3e1Schristos set->status|=error;
10044e98e3e1Schristos num.exponent=DECFLOAT_qNaN; /* set up quiet NaN */
10054e98e3e1Schristos num.sign=0; /* .. with 0 sign */
10064e98e3e1Schristos buffer[0]=0; /* .. and coefficient */
10074e98e3e1Schristos num.lsd=buffer; /* .. */
10084e98e3e1Schristos /* decShowNum(&num, "oops"); */
10094e98e3e1Schristos }
10104e98e3e1Schristos
10114e98e3e1Schristos /* decShowNum(&num, "dffs"); */
10124e98e3e1Schristos decFinalize(result, &num, set); /* round, check, and lay out */
10134e98e3e1Schristos /* decFloatShow(result, "fromString"); */
10144e98e3e1Schristos return result;
10154e98e3e1Schristos } /* decFloatFromString */
10164e98e3e1Schristos
10174e98e3e1Schristos /* ------------------------------------------------------------------ */
10184e98e3e1Schristos /* decFloatFromWider -- conversion from next-wider format */
10194e98e3e1Schristos /* */
10204e98e3e1Schristos /* result is the decFloat format number which gets the result of */
10214e98e3e1Schristos /* the conversion */
10224e98e3e1Schristos /* wider is the decFloatWider format number which will be narrowed */
10234e98e3e1Schristos /* set is the context */
10244e98e3e1Schristos /* returns result */
10254e98e3e1Schristos /* */
10264e98e3e1Schristos /* Narrowing can cause rounding, overflow, etc., but not Invalid */
10274e98e3e1Schristos /* operation (sNaNs are copied and do not signal). */
10284e98e3e1Schristos /* ------------------------------------------------------------------ */
10294e98e3e1Schristos /* narrow-to is not possible for decQuad format numbers; simply omit */
10304e98e3e1Schristos #if !QUAD
decFloatFromWider(decFloat * result,const decFloatWider * wider,decContext * set)10314e98e3e1Schristos decFloat * decFloatFromWider(decFloat *result, const decFloatWider *wider,
10324e98e3e1Schristos decContext *set) {
10334e98e3e1Schristos bcdnum num; /* collects data for finishing */
10344e98e3e1Schristos uByte bcdar[DECWPMAX]; /* room for wider coefficient */
10354e98e3e1Schristos uInt widerhi=DFWWORD(wider, 0); /* top word */
10364e98e3e1Schristos Int exp;
10374e98e3e1Schristos
10384e98e3e1Schristos GETWCOEFF(wider, bcdar);
10394e98e3e1Schristos
10404e98e3e1Schristos num.msd=bcdar; /* MSD is here always */
10414e98e3e1Schristos num.lsd=bcdar+DECWPMAX-1; /* LSD is here always */
10424e98e3e1Schristos num.sign=widerhi&0x80000000; /* extract sign [DECFLOAT_Sign=Neg] */
10434e98e3e1Schristos
10444e98e3e1Schristos /* decode the wider combination field to exponent */
10454e98e3e1Schristos exp=DECCOMBWEXP[widerhi>>26]; /* decode from wider combination field */
10464e98e3e1Schristos /* if it is a special there's nothing to do unless sNaN; if it's */
10474e98e3e1Schristos /* finite then add the (wider) exponent continuation and unbias */
10484e98e3e1Schristos if (EXPISSPECIAL(exp)) exp=widerhi&0x7e000000; /* include sNaN selector */
10494e98e3e1Schristos else exp+=GETWECON(wider)-DECWBIAS;
10504e98e3e1Schristos num.exponent=exp;
10514e98e3e1Schristos
10524e98e3e1Schristos /* decShowNum(&num, "dffw"); */
10534e98e3e1Schristos return decFinalize(result, &num, set);/* round, check, and lay out */
10544e98e3e1Schristos } /* decFloatFromWider */
10554e98e3e1Schristos #endif
10564e98e3e1Schristos
10574e98e3e1Schristos /* ------------------------------------------------------------------ */
10584e98e3e1Schristos /* decFloatGetCoefficient -- get coefficient as BCD8 */
10594e98e3e1Schristos /* */
10604e98e3e1Schristos /* df is the decFloat from which to extract the coefficient */
10614e98e3e1Schristos /* bcdar is where DECPMAX bytes will be written, one BCD digit in */
10624e98e3e1Schristos /* each byte (BCD8 encoding); if df is a NaN the first byte will */
10634e98e3e1Schristos /* be zero, and if it is infinite they will all be zero */
10644e98e3e1Schristos /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
10654e98e3e1Schristos /* 0 otherwise) */
10664e98e3e1Schristos /* */
10674e98e3e1Schristos /* No error is possible, and no status will be set. If df is a */
10684e98e3e1Schristos /* special value the array is set to zeros (for Infinity) or to the */
10694e98e3e1Schristos /* payload of a qNaN or sNaN. */
10704e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatGetCoefficient(const decFloat * df,uByte * bcdar)10714e98e3e1Schristos Int decFloatGetCoefficient(const decFloat *df, uByte *bcdar) {
10724e98e3e1Schristos if (DFISINF(df)) memset(bcdar, 0, DECPMAX);
10734e98e3e1Schristos else {
10744e98e3e1Schristos GETCOEFF(df, bcdar); /* use macro */
10754e98e3e1Schristos if (DFISNAN(df)) bcdar[0]=0; /* MSD needs correcting */
10764e98e3e1Schristos }
10774e98e3e1Schristos return DFISSIGNED(df);
10784e98e3e1Schristos } /* decFloatGetCoefficient */
10794e98e3e1Schristos
10804e98e3e1Schristos /* ------------------------------------------------------------------ */
10814e98e3e1Schristos /* decFloatGetExponent -- get unbiased exponent */
10824e98e3e1Schristos /* */
10834e98e3e1Schristos /* df is the decFloat from which to extract the exponent */
10844e98e3e1Schristos /* returns the exponent, q. */
10854e98e3e1Schristos /* */
10864e98e3e1Schristos /* No error is possible, and no status will be set. If df is a */
10874e98e3e1Schristos /* special value the first seven bits of the decFloat are returned, */
10884e98e3e1Schristos /* left adjusted and with the first (sign) bit set to 0 (followed by */
10894e98e3e1Schristos /* 25 0 bits). e.g., -sNaN would return 0x7e000000 (DECFLOAT_sNaN). */
10904e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatGetExponent(const decFloat * df)10914e98e3e1Schristos Int decFloatGetExponent(const decFloat *df) {
10924e98e3e1Schristos if (DFISSPECIAL(df)) return DFWORD(df, 0)&0x7e000000;
10934e98e3e1Schristos return GETEXPUN(df);
10944e98e3e1Schristos } /* decFloatGetExponent */
10954e98e3e1Schristos
10964e98e3e1Schristos /* ------------------------------------------------------------------ */
10974e98e3e1Schristos /* decFloatSetCoefficient -- set coefficient from BCD8 */
10984e98e3e1Schristos /* */
10994e98e3e1Schristos /* df is the target decFloat (and source of exponent/special value) */
11004e98e3e1Schristos /* bcdar holds DECPMAX digits to set the coefficient from, one */
11014e98e3e1Schristos /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
11024e98e3e1Schristos /* if df is a NaN; all are ignored if df is infinite. */
11034e98e3e1Schristos /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
11044e98e3e1Schristos /* returns df, which will be canonical */
11054e98e3e1Schristos /* */
11064e98e3e1Schristos /* No error is possible, and no status will be set. */
11074e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatSetCoefficient(decFloat * df,const uByte * bcdar,Int sig)11084e98e3e1Schristos decFloat * decFloatSetCoefficient(decFloat *df, const uByte *bcdar,
11094e98e3e1Schristos Int sig) {
11104e98e3e1Schristos uInt exp; /* for exponent */
11114e98e3e1Schristos uByte bcdzero[DECPMAX]; /* for infinities */
11124e98e3e1Schristos
11134e98e3e1Schristos /* Exponent/special code is extracted from df */
11144e98e3e1Schristos if (DFISSPECIAL(df)) {
11154e98e3e1Schristos exp=DFWORD(df, 0)&0x7e000000;
11164e98e3e1Schristos if (DFISINF(df)) {
11174e98e3e1Schristos memset(bcdzero, 0, DECPMAX);
11184e98e3e1Schristos return decFloatFromBCD(df, exp, bcdzero, sig);
11194e98e3e1Schristos }
11204e98e3e1Schristos }
11214e98e3e1Schristos else exp=GETEXPUN(df);
11224e98e3e1Schristos return decFloatFromBCD(df, exp, bcdar, sig);
11234e98e3e1Schristos } /* decFloatSetCoefficient */
11244e98e3e1Schristos
11254e98e3e1Schristos /* ------------------------------------------------------------------ */
11264e98e3e1Schristos /* decFloatSetExponent -- set exponent or special value */
11274e98e3e1Schristos /* */
11284e98e3e1Schristos /* df is the target decFloat (and source of coefficient/payload) */
11294e98e3e1Schristos /* set is the context for reporting status */
11304e98e3e1Schristos /* exp is the unbiased exponent, q, or a special value in the form */
11314e98e3e1Schristos /* returned by decFloatGetExponent */
11324e98e3e1Schristos /* returns df, which will be canonical */
11334e98e3e1Schristos /* */
11344e98e3e1Schristos /* No error is possible, but Overflow or Underflow might occur. */
11354e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatSetExponent(decFloat * df,decContext * set,Int exp)11364e98e3e1Schristos decFloat * decFloatSetExponent(decFloat *df, decContext *set, Int exp) {
11374e98e3e1Schristos uByte bcdcopy[DECPMAX]; /* for coefficient */
11384e98e3e1Schristos bcdnum num; /* work */
11394e98e3e1Schristos num.exponent=exp;
11404e98e3e1Schristos num.sign=decFloatGetCoefficient(df, bcdcopy); /* extract coefficient */
11414e98e3e1Schristos if (DFISSPECIAL(df)) { /* MSD or more needs correcting */
11424e98e3e1Schristos if (DFISINF(df)) memset(bcdcopy, 0, DECPMAX);
11434e98e3e1Schristos bcdcopy[0]=0;
11444e98e3e1Schristos }
11454e98e3e1Schristos num.msd=bcdcopy;
11464e98e3e1Schristos num.lsd=bcdcopy+DECPMAX-1;
11474e98e3e1Schristos return decFinalize(df, &num, set);
11484e98e3e1Schristos } /* decFloatSetExponent */
11494e98e3e1Schristos
11504e98e3e1Schristos /* ------------------------------------------------------------------ */
11514e98e3e1Schristos /* decFloatRadix -- returns the base (10) */
11524e98e3e1Schristos /* */
11534e98e3e1Schristos /* df is any decFloat of this format */
11544e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatRadix(const decFloat * df)11554e98e3e1Schristos uInt decFloatRadix(const decFloat *df) {
11564e98e3e1Schristos if (df) return 10; /* to placate compiler */
11574e98e3e1Schristos return 10;
11584e98e3e1Schristos } /* decFloatRadix */
11594e98e3e1Schristos
11604e98e3e1Schristos #if (DECCHECK || DECTRACE)
11614e98e3e1Schristos /* ------------------------------------------------------------------ */
11624e98e3e1Schristos /* decFloatShow -- printf a decFloat in hexadecimal and decimal */
11634e98e3e1Schristos /* df is the decFloat to show */
11644e98e3e1Schristos /* tag is a tag string displayed with the number */
11654e98e3e1Schristos /* */
11664e98e3e1Schristos /* This is a debug aid; the precise format of the string may change. */
11674e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatShow(const decFloat * df,const char * tag)11684e98e3e1Schristos void decFloatShow(const decFloat *df, const char *tag) {
11694e98e3e1Schristos char hexbuf[DECBYTES*2+DECBYTES/4+1]; /* NB blank after every fourth */
11704e98e3e1Schristos char buff[DECSTRING]; /* for value in decimal */
11714e98e3e1Schristos Int i, j=0;
11724e98e3e1Schristos
11734e98e3e1Schristos for (i=0; i<DECBYTES; i++) {
11744e98e3e1Schristos #if DECLITEND
11754e98e3e1Schristos sprintf(&hexbuf[j], "%02x", df->bytes[DECBYTES-1-i]);
11764e98e3e1Schristos #else
11774e98e3e1Schristos sprintf(&hexbuf[j], "%02x", df->bytes[i]);
11784e98e3e1Schristos #endif
11794e98e3e1Schristos j+=2;
11804e98e3e1Schristos /* the next line adds blank (and terminator) after final pair, too */
11814e98e3e1Schristos if ((i+1)%4==0) {strcpy(&hexbuf[j], " "); j++;}
11824e98e3e1Schristos }
11834e98e3e1Schristos decFloatToString(df, buff);
11844e98e3e1Schristos printf(">%s> %s [big-endian] %s\n", tag, hexbuf, buff);
11854e98e3e1Schristos return;
11864e98e3e1Schristos } /* decFloatShow */
11874e98e3e1Schristos #endif
11884e98e3e1Schristos
11894e98e3e1Schristos /* ------------------------------------------------------------------ */
11904e98e3e1Schristos /* decFloatToBCD -- get sign, exponent, and BCD8 from a decFloat */
11914e98e3e1Schristos /* */
11924e98e3e1Schristos /* df is the source decFloat */
11934e98e3e1Schristos /* exp will be set to the unbiased exponent, q, or to a special */
11944e98e3e1Schristos /* value in the form returned by decFloatGetExponent */
11954e98e3e1Schristos /* bcdar is where DECPMAX bytes will be written, one BCD digit in */
11964e98e3e1Schristos /* each byte (BCD8 encoding); if df is a NaN the first byte will */
11974e98e3e1Schristos /* be zero, and if it is infinite they will all be zero */
11984e98e3e1Schristos /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
11994e98e3e1Schristos /* 0 otherwise) */
12004e98e3e1Schristos /* */
12014e98e3e1Schristos /* No error is possible, and no status will be set. */
12024e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatToBCD(const decFloat * df,Int * exp,uByte * bcdar)12034e98e3e1Schristos Int decFloatToBCD(const decFloat *df, Int *exp, uByte *bcdar) {
12044e98e3e1Schristos if (DFISINF(df)) {
12054e98e3e1Schristos memset(bcdar, 0, DECPMAX);
12064e98e3e1Schristos *exp=DFWORD(df, 0)&0x7e000000;
12074e98e3e1Schristos }
12084e98e3e1Schristos else {
12094e98e3e1Schristos GETCOEFF(df, bcdar); /* use macro */
12104e98e3e1Schristos if (DFISNAN(df)) {
12114e98e3e1Schristos bcdar[0]=0; /* MSD needs correcting */
12124e98e3e1Schristos *exp=DFWORD(df, 0)&0x7e000000;
12134e98e3e1Schristos }
12144e98e3e1Schristos else { /* finite */
12154e98e3e1Schristos *exp=GETEXPUN(df);
12164e98e3e1Schristos }
12174e98e3e1Schristos }
12184e98e3e1Schristos return DFISSIGNED(df);
12194e98e3e1Schristos } /* decFloatToBCD */
12204e98e3e1Schristos
12214e98e3e1Schristos /* ------------------------------------------------------------------ */
12224e98e3e1Schristos /* decFloatToEngString -- conversion to numeric string, engineering */
12234e98e3e1Schristos /* */
12244e98e3e1Schristos /* df is the decFloat format number to convert */
12254e98e3e1Schristos /* string is the string where the result will be laid out */
12264e98e3e1Schristos /* */
12274e98e3e1Schristos /* string must be at least DECPMAX+9 characters (the worst case is */
12284e98e3e1Schristos /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
12294e98e3e1Schristos /* DECEMAXD<=4); this condition is asserted above */
12304e98e3e1Schristos /* */
12314e98e3e1Schristos /* No error is possible, and no status will be set */
12324e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatToEngString(const decFloat * df,char * string)12334e98e3e1Schristos char * decFloatToEngString(const decFloat *df, char *string){
12344e98e3e1Schristos uInt msd; /* coefficient MSD */
12354e98e3e1Schristos Int exp; /* exponent top two bits or full */
12364e98e3e1Schristos uInt comb; /* combination field */
12374e98e3e1Schristos char *cstart; /* coefficient start */
12384e98e3e1Schristos char *c; /* output pointer in string */
12394e98e3e1Schristos char *s, *t; /* .. (source, target) */
12404e98e3e1Schristos Int pre, e; /* work */
12414e98e3e1Schristos const uByte *u; /* .. */
12424e98e3e1Schristos uInt uiwork; /* for macros [one compiler needs */
12434e98e3e1Schristos /* volatile here to avoid bug, but */
12444e98e3e1Schristos /* that doubles execution time] */
12454e98e3e1Schristos
12464e98e3e1Schristos /* Source words; macro handles endianness */
12474e98e3e1Schristos uInt sourhi=DFWORD(df, 0); /* word with sign */
12484e98e3e1Schristos #if DECPMAX==16
12494e98e3e1Schristos uInt sourlo=DFWORD(df, 1);
12504e98e3e1Schristos #elif DECPMAX==34
12514e98e3e1Schristos uInt sourmh=DFWORD(df, 1);
12524e98e3e1Schristos uInt sourml=DFWORD(df, 2);
12534e98e3e1Schristos uInt sourlo=DFWORD(df, 3);
12544e98e3e1Schristos #endif
12554e98e3e1Schristos
12564e98e3e1Schristos c=string; /* where result will go */
12574e98e3e1Schristos if (((Int)sourhi)<0) *c++='-'; /* handle sign */
12584e98e3e1Schristos comb=sourhi>>26; /* sign+combination field */
12594e98e3e1Schristos msd=DECCOMBMSD[comb]; /* decode the combination field */
12604e98e3e1Schristos exp=DECCOMBEXP[comb]; /* .. */
12614e98e3e1Schristos
12624e98e3e1Schristos if (EXPISSPECIAL(exp)) { /* special */
12634e98e3e1Schristos if (exp==DECFLOAT_Inf) { /* infinity */
12644e98e3e1Schristos strcpy(c, "Inf");
12654e98e3e1Schristos strcpy(c+3, "inity");
12664e98e3e1Schristos return string; /* easy */
12674e98e3e1Schristos }
12684e98e3e1Schristos if (sourhi&0x02000000) *c++='s'; /* sNaN */
12694e98e3e1Schristos strcpy(c, "NaN"); /* complete word */
12704e98e3e1Schristos c+=3; /* step past */
12714e98e3e1Schristos /* quick exit if the payload is zero */
12724e98e3e1Schristos #if DECPMAX==7
12734e98e3e1Schristos if ((sourhi&0x000fffff)==0) return string;
12744e98e3e1Schristos #elif DECPMAX==16
12754e98e3e1Schristos if (sourlo==0 && (sourhi&0x0003ffff)==0) return string;
12764e98e3e1Schristos #elif DECPMAX==34
12774e98e3e1Schristos if (sourlo==0 && sourml==0 && sourmh==0
12784e98e3e1Schristos && (sourhi&0x00003fff)==0) return string;
12794e98e3e1Schristos #endif
12804e98e3e1Schristos /* otherwise drop through to add integer; set correct exp etc. */
12814e98e3e1Schristos exp=0; msd=0; /* setup for following code */
12824e98e3e1Schristos }
12834e98e3e1Schristos else { /* complete exponent; top two bits are in place */
12844e98e3e1Schristos exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */
12854e98e3e1Schristos }
12864e98e3e1Schristos
12874e98e3e1Schristos /* convert the digits of the significand to characters */
12884e98e3e1Schristos cstart=c; /* save start of coefficient */
12894e98e3e1Schristos if (msd) *c++=(char)('0'+(char)msd); /* non-zero most significant digit */
12904e98e3e1Schristos
12914e98e3e1Schristos /* Decode the declets. After extracting each declet, it is */
12924e98e3e1Schristos /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
12934e98e3e1Schristos /* are the three encoded BCD8 digits followed by a 1-byte length */
12944e98e3e1Schristos /* (significant digits, except that 000 has length 0). This allows */
12954e98e3e1Schristos /* us to left-align the first declet with non-zero content, then */
12964e98e3e1Schristos /* the remaining ones are full 3-char length. Fixed-length copies */
12974e98e3e1Schristos /* are used because variable-length memcpy causes a subroutine call */
12984e98e3e1Schristos /* in at least two compilers. (The copies are length 4 for speed */
12994e98e3e1Schristos /* and are safe because the last item in the array is of length */
13004e98e3e1Schristos /* three and has the length byte following.) */
13014e98e3e1Schristos #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
13024e98e3e1Schristos if (c!=cstart) {UBFROMUI(c, UBTOUI(u)|CHARMASK); c+=3;} \
13034e98e3e1Schristos else if (*(u+3)) { \
13044e98e3e1Schristos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); c+=*(u+3);}
13054e98e3e1Schristos
13064e98e3e1Schristos #if DECPMAX==7
13074e98e3e1Schristos dpd2char(sourhi>>10); /* declet 1 */
13084e98e3e1Schristos dpd2char(sourhi); /* declet 2 */
13094e98e3e1Schristos
13104e98e3e1Schristos #elif DECPMAX==16
13114e98e3e1Schristos dpd2char(sourhi>>8); /* declet 1 */
13124e98e3e1Schristos dpd2char((sourhi<<2) | (sourlo>>30)); /* declet 2 */
13134e98e3e1Schristos dpd2char(sourlo>>20); /* declet 3 */
13144e98e3e1Schristos dpd2char(sourlo>>10); /* declet 4 */
13154e98e3e1Schristos dpd2char(sourlo); /* declet 5 */
13164e98e3e1Schristos
13174e98e3e1Schristos #elif DECPMAX==34
13184e98e3e1Schristos dpd2char(sourhi>>4); /* declet 1 */
13194e98e3e1Schristos dpd2char((sourhi<<6) | (sourmh>>26)); /* declet 2 */
13204e98e3e1Schristos dpd2char(sourmh>>16); /* declet 3 */
13214e98e3e1Schristos dpd2char(sourmh>>6); /* declet 4 */
13224e98e3e1Schristos dpd2char((sourmh<<4) | (sourml>>28)); /* declet 5 */
13234e98e3e1Schristos dpd2char(sourml>>18); /* declet 6 */
13244e98e3e1Schristos dpd2char(sourml>>8); /* declet 7 */
13254e98e3e1Schristos dpd2char((sourml<<2) | (sourlo>>30)); /* declet 8 */
13264e98e3e1Schristos dpd2char(sourlo>>20); /* declet 9 */
13274e98e3e1Schristos dpd2char(sourlo>>10); /* declet 10 */
13284e98e3e1Schristos dpd2char(sourlo); /* declet 11 */
13294e98e3e1Schristos #endif
13304e98e3e1Schristos
13314e98e3e1Schristos if (c==cstart) *c++='0'; /* all zeros, empty -- make "0" */
13324e98e3e1Schristos
13334e98e3e1Schristos if (exp==0) { /* integer or NaN case -- easy */
13344e98e3e1Schristos *c='\0'; /* terminate */
13354e98e3e1Schristos return string;
13364e98e3e1Schristos }
13374e98e3e1Schristos /* non-0 exponent */
13384e98e3e1Schristos
13394e98e3e1Schristos e=0; /* assume no E */
13404e98e3e1Schristos pre=(Int)(c-cstart)+exp; /* length+exp [c->LSD+1] */
13414e98e3e1Schristos /* [here, pre-exp is the digits count (==1 for zero)] */
13424e98e3e1Schristos
13434e98e3e1Schristos if (exp>0 || pre<-5) { /* need exponential form */
13444e98e3e1Schristos e=pre-1; /* calculate E value */
13454e98e3e1Schristos pre=1; /* assume one digit before '.' */
13464e98e3e1Schristos if (e!=0) { /* engineering: may need to adjust */
13474e98e3e1Schristos Int adj; /* adjustment */
13484e98e3e1Schristos /* The C remainder operator is undefined for negative numbers, so */
13494e98e3e1Schristos /* a positive remainder calculation must be used here */
13504e98e3e1Schristos if (e<0) {
13514e98e3e1Schristos adj=(-e)%3;
13524e98e3e1Schristos if (adj!=0) adj=3-adj;
13534e98e3e1Schristos }
13544e98e3e1Schristos else { /* e>0 */
13554e98e3e1Schristos adj=e%3;
13564e98e3e1Schristos }
13574e98e3e1Schristos e=e-adj;
13584e98e3e1Schristos /* if dealing with zero still produce an exponent which is a */
13594e98e3e1Schristos /* multiple of three, as expected, but there will only be the */
13604e98e3e1Schristos /* one zero before the E, still. Otherwise note the padding. */
13614e98e3e1Schristos if (!DFISZERO(df)) pre+=adj;
13624e98e3e1Schristos else { /* is zero */
13634e98e3e1Schristos if (adj!=0) { /* 0.00Esnn needed */
13644e98e3e1Schristos e=e+3;
13654e98e3e1Schristos pre=-(2-adj);
13664e98e3e1Schristos }
13674e98e3e1Schristos } /* zero */
13684e98e3e1Schristos } /* engineering adjustment */
13694e98e3e1Schristos } /* exponential form */
13704e98e3e1Schristos /* printf("e=%ld pre=%ld exp=%ld\n", (LI)e, (LI)pre, (LI)exp); */
13714e98e3e1Schristos
13724e98e3e1Schristos /* modify the coefficient, adding 0s, '.', and E+nn as needed */
13734e98e3e1Schristos if (pre>0) { /* ddd.ddd (plain), perhaps with E */
13744e98e3e1Schristos /* or dd00 padding for engineering */
13754e98e3e1Schristos char *dotat=cstart+pre;
13764e98e3e1Schristos if (dotat<c) { /* if embedded dot needed... */
13774e98e3e1Schristos /* move by fours; there must be space for junk at the end */
13784e98e3e1Schristos /* because there is still space for exponent */
13794e98e3e1Schristos s=dotat+ROUNDDOWN4(c-dotat); /* source */
13804e98e3e1Schristos t=s+1; /* target */
13814e98e3e1Schristos /* open the gap [cannot use memcpy] */
13824e98e3e1Schristos for (; s>=dotat; s-=4, t-=4) UBFROMUI(t, UBTOUI(s));
13834e98e3e1Schristos *dotat='.';
13844e98e3e1Schristos c++; /* length increased by one */
13854e98e3e1Schristos } /* need dot? */
13864e98e3e1Schristos else for (; c<dotat; c++) *c='0'; /* pad for engineering */
13874e98e3e1Schristos } /* pre>0 */
13884e98e3e1Schristos else {
13894e98e3e1Schristos /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (may have
13904e98e3e1Schristos E, but only for 0.00E+3 kind of case -- with plenty of spare
13914e98e3e1Schristos space in this case */
13924e98e3e1Schristos pre=-pre+2; /* gap width, including "0." */
13934e98e3e1Schristos t=cstart+ROUNDDOWN4(c-cstart)+pre; /* preferred first target point */
13944e98e3e1Schristos /* backoff if too far to the right */
13954e98e3e1Schristos if (t>string+DECSTRING-5) t=string+DECSTRING-5; /* adjust to fit */
13964e98e3e1Schristos /* now shift the entire coefficient to the right, being careful not */
13974e98e3e1Schristos /* to access to the left of string [cannot use memcpy] */
13984e98e3e1Schristos for (s=t-pre; s>=string; s-=4, t-=4) UBFROMUI(t, UBTOUI(s));
13994e98e3e1Schristos /* for Quads and Singles there may be a character or two left... */
14004e98e3e1Schristos s+=3; /* where next would come from */
14014e98e3e1Schristos for(; s>=cstart; s--, t--) *(t+3)=*(s);
14024e98e3e1Schristos /* now have fill 0. through 0.00000; use overlaps to avoid tests */
14034e98e3e1Schristos if (pre>=4) {
14044e98e3e1Schristos memcpy(cstart+pre-4, "0000", 4);
14054e98e3e1Schristos memcpy(cstart, "0.00", 4);
14064e98e3e1Schristos }
14074e98e3e1Schristos else { /* 2 or 3 */
14084e98e3e1Schristos *(cstart+pre-1)='0';
14094e98e3e1Schristos memcpy(cstart, "0.", 2);
14104e98e3e1Schristos }
14114e98e3e1Schristos c+=pre; /* to end */
14124e98e3e1Schristos }
14134e98e3e1Schristos
14144e98e3e1Schristos /* finally add the E-part, if needed; it will never be 0, and has */
14154e98e3e1Schristos /* a maximum length of 3 or 4 digits (asserted above) */
14164e98e3e1Schristos if (e!=0) {
14174e98e3e1Schristos memcpy(c, "E+", 2); /* starts with E, assume + */
14184e98e3e1Schristos c++;
14194e98e3e1Schristos if (e<0) {
14204e98e3e1Schristos *c='-'; /* oops, need '-' */
14214e98e3e1Schristos e=-e; /* uInt, please */
14224e98e3e1Schristos }
14234e98e3e1Schristos c++;
14244e98e3e1Schristos /* Three-character exponents are easy; 4-character a little trickier */
14254e98e3e1Schristos #if DECEMAXD<=3
14264e98e3e1Schristos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
14274e98e3e1Schristos /* copy fixed 4 characters [is safe], starting at non-zero */
14284e98e3e1Schristos /* and with character mask to convert BCD to char */
14294e98e3e1Schristos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK);
14304e98e3e1Schristos c+=*(u+3); /* bump pointer appropriately */
14314e98e3e1Schristos #elif DECEMAXD==4
14324e98e3e1Schristos if (e<1000) { /* 3 (or fewer) digits case */
14334e98e3e1Schristos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
14344e98e3e1Schristos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); /* [as above] */
14354e98e3e1Schristos c+=*(u+3); /* bump pointer appropriately */
14364e98e3e1Schristos }
14374e98e3e1Schristos else { /* 4-digits */
14384e98e3e1Schristos Int thou=((e>>3)*1049)>>17; /* e/1000 */
14394e98e3e1Schristos Int rem=e-(1000*thou); /* e%1000 */
14404e98e3e1Schristos *c++=(char)('0'+(char)thou); /* the thousands digit */
14414e98e3e1Schristos u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
14424e98e3e1Schristos UBFROMUI(c, UBTOUI(u)|CHARMASK);/* copy fixed 3+1 characters [is safe] */
14434e98e3e1Schristos c+=3; /* bump pointer, always 3 digits */
14444e98e3e1Schristos }
14454e98e3e1Schristos #endif
14464e98e3e1Schristos }
14474e98e3e1Schristos *c='\0'; /* terminate */
14484e98e3e1Schristos /*printf("res %s\n", string); */
14494e98e3e1Schristos return string;
14504e98e3e1Schristos } /* decFloatToEngString */
14514e98e3e1Schristos
14524e98e3e1Schristos /* ------------------------------------------------------------------ */
14534e98e3e1Schristos /* decFloatToPacked -- convert decFloat to Packed decimal + exponent */
14544e98e3e1Schristos /* */
14554e98e3e1Schristos /* df is the source decFloat */
14564e98e3e1Schristos /* exp will be set to the unbiased exponent, q, or to a special */
14574e98e3e1Schristos /* value in the form returned by decFloatGetExponent */
14584e98e3e1Schristos /* packed is where DECPMAX nibbles will be written with the sign as */
14594e98e3e1Schristos /* final nibble (0x0c for +, 0x0d for -); a NaN has a first nibble */
14604e98e3e1Schristos /* of zero, and an infinity is all zeros. decDouble and decQuad */
14614e98e3e1Schristos /* have a additional leading zero nibble, leading to result */
14624e98e3e1Schristos /* lengths of 4, 9, and 18 bytes. */
14634e98e3e1Schristos /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
14644e98e3e1Schristos /* 0 otherwise) */
14654e98e3e1Schristos /* */
14664e98e3e1Schristos /* No error is possible, and no status will be set. */
14674e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatToPacked(const decFloat * df,Int * exp,uByte * packed)14684e98e3e1Schristos Int decFloatToPacked(const decFloat *df, Int *exp, uByte *packed) {
14694e98e3e1Schristos uByte bcdar[DECPMAX+2]; /* work buffer */
14704e98e3e1Schristos uByte *ip=bcdar, *op=packed; /* work pointers */
14714e98e3e1Schristos if (DFISINF(df)) {
14724e98e3e1Schristos memset(bcdar, 0, DECPMAX+2);
14734e98e3e1Schristos *exp=DECFLOAT_Inf;
14744e98e3e1Schristos }
14754e98e3e1Schristos else {
14764e98e3e1Schristos GETCOEFF(df, bcdar+1); /* use macro */
14774e98e3e1Schristos if (DFISNAN(df)) {
14784e98e3e1Schristos bcdar[1]=0; /* MSD needs clearing */
14794e98e3e1Schristos *exp=DFWORD(df, 0)&0x7e000000;
14804e98e3e1Schristos }
14814e98e3e1Schristos else { /* finite */
14824e98e3e1Schristos *exp=GETEXPUN(df);
14834e98e3e1Schristos }
14844e98e3e1Schristos }
14854e98e3e1Schristos /* now pack; coefficient currently at bcdar+1 */
14864e98e3e1Schristos #if SINGLE
14874e98e3e1Schristos ip++; /* ignore first byte */
14884e98e3e1Schristos #else
14894e98e3e1Schristos *ip=0; /* need leading zero */
14904e98e3e1Schristos #endif
14914e98e3e1Schristos /* set final byte to Packed BCD sign value */
14924e98e3e1Schristos bcdar[DECPMAX+1]=(DFISSIGNED(df) ? DECPMINUS : DECPPLUS);
14934e98e3e1Schristos /* pack an even number of bytes... */
14944e98e3e1Schristos for (; op<packed+((DECPMAX+2)/2); op++, ip+=2) {
14954e98e3e1Schristos *op=(uByte)((*ip<<4)+*(ip+1));
14964e98e3e1Schristos }
14974e98e3e1Schristos return (bcdar[DECPMAX+1]==DECPMINUS ? DECFLOAT_Sign : 0);
14984e98e3e1Schristos } /* decFloatToPacked */
14994e98e3e1Schristos
15004e98e3e1Schristos /* ------------------------------------------------------------------ */
15014e98e3e1Schristos /* decFloatToString -- conversion to numeric string */
15024e98e3e1Schristos /* */
15034e98e3e1Schristos /* df is the decFloat format number to convert */
15044e98e3e1Schristos /* string is the string where the result will be laid out */
15054e98e3e1Schristos /* */
15064e98e3e1Schristos /* string must be at least DECPMAX+9 characters (the worst case is */
15074e98e3e1Schristos /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
15084e98e3e1Schristos /* DECEMAXD<=4); this condition is asserted above */
15094e98e3e1Schristos /* */
15104e98e3e1Schristos /* No error is possible, and no status will be set */
15114e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatToString(const decFloat * df,char * string)15124e98e3e1Schristos char * decFloatToString(const decFloat *df, char *string){
15134e98e3e1Schristos uInt msd; /* coefficient MSD */
15144e98e3e1Schristos Int exp; /* exponent top two bits or full */
15154e98e3e1Schristos uInt comb; /* combination field */
15164e98e3e1Schristos char *cstart; /* coefficient start */
15174e98e3e1Schristos char *c; /* output pointer in string */
15184e98e3e1Schristos char *s, *t; /* .. (source, target) */
15194e98e3e1Schristos Int pre, e; /* work */
15204e98e3e1Schristos const uByte *u; /* .. */
15214e98e3e1Schristos uInt uiwork; /* for macros [one compiler needs */
15224e98e3e1Schristos /* volatile here to avoid bug, but */
15234e98e3e1Schristos /* that doubles execution time] */
15244e98e3e1Schristos
15254e98e3e1Schristos /* Source words; macro handles endianness */
15264e98e3e1Schristos uInt sourhi=DFWORD(df, 0); /* word with sign */
15274e98e3e1Schristos #if DECPMAX==16
15284e98e3e1Schristos uInt sourlo=DFWORD(df, 1);
15294e98e3e1Schristos #elif DECPMAX==34
15304e98e3e1Schristos uInt sourmh=DFWORD(df, 1);
15314e98e3e1Schristos uInt sourml=DFWORD(df, 2);
15324e98e3e1Schristos uInt sourlo=DFWORD(df, 3);
15334e98e3e1Schristos #endif
15344e98e3e1Schristos
15354e98e3e1Schristos c=string; /* where result will go */
15364e98e3e1Schristos if (((Int)sourhi)<0) *c++='-'; /* handle sign */
15374e98e3e1Schristos comb=sourhi>>26; /* sign+combination field */
15384e98e3e1Schristos msd=DECCOMBMSD[comb]; /* decode the combination field */
15394e98e3e1Schristos exp=DECCOMBEXP[comb]; /* .. */
15404e98e3e1Schristos
15414e98e3e1Schristos if (!EXPISSPECIAL(exp)) { /* finite */
15424e98e3e1Schristos /* complete exponent; top two bits are in place */
15434e98e3e1Schristos exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */
15444e98e3e1Schristos }
15454e98e3e1Schristos else { /* IS special */
15464e98e3e1Schristos if (exp==DECFLOAT_Inf) { /* infinity */
15474e98e3e1Schristos strcpy(c, "Infinity");
15484e98e3e1Schristos return string; /* easy */
15494e98e3e1Schristos }
15504e98e3e1Schristos if (sourhi&0x02000000) *c++='s'; /* sNaN */
15514e98e3e1Schristos strcpy(c, "NaN"); /* complete word */
15524e98e3e1Schristos c+=3; /* step past */
15534e98e3e1Schristos /* quick exit if the payload is zero */
15544e98e3e1Schristos #if DECPMAX==7
15554e98e3e1Schristos if ((sourhi&0x000fffff)==0) return string;
15564e98e3e1Schristos #elif DECPMAX==16
15574e98e3e1Schristos if (sourlo==0 && (sourhi&0x0003ffff)==0) return string;
15584e98e3e1Schristos #elif DECPMAX==34
15594e98e3e1Schristos if (sourlo==0 && sourml==0 && sourmh==0
15604e98e3e1Schristos && (sourhi&0x00003fff)==0) return string;
15614e98e3e1Schristos #endif
15624e98e3e1Schristos /* otherwise drop through to add integer; set correct exp etc. */
15634e98e3e1Schristos exp=0; msd=0; /* setup for following code */
15644e98e3e1Schristos }
15654e98e3e1Schristos
15664e98e3e1Schristos /* convert the digits of the significand to characters */
15674e98e3e1Schristos cstart=c; /* save start of coefficient */
15684e98e3e1Schristos if (msd) *c++=(char)('0'+(char)msd); /* non-zero most significant digit */
15694e98e3e1Schristos
15704e98e3e1Schristos /* Decode the declets. After extracting each declet, it is */
15714e98e3e1Schristos /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
15724e98e3e1Schristos /* are the three encoded BCD8 digits followed by a 1-byte length */
15734e98e3e1Schristos /* (significant digits, except that 000 has length 0). This allows */
15744e98e3e1Schristos /* us to left-align the first declet with non-zero content, then */
15754e98e3e1Schristos /* the remaining ones are full 3-char length. Fixed-length copies */
15764e98e3e1Schristos /* are used because variable-length memcpy causes a subroutine call */
15774e98e3e1Schristos /* in at least two compilers. (The copies are length 4 for speed */
15784e98e3e1Schristos /* and are safe because the last item in the array is of length */
15794e98e3e1Schristos /* three and has the length byte following.) */
15804e98e3e1Schristos #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
15814e98e3e1Schristos if (c!=cstart) {UBFROMUI(c, UBTOUI(u)|CHARMASK); c+=3;} \
15824e98e3e1Schristos else if (*(u+3)) { \
15834e98e3e1Schristos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); c+=*(u+3);}
15844e98e3e1Schristos
15854e98e3e1Schristos #if DECPMAX==7
15864e98e3e1Schristos dpd2char(sourhi>>10); /* declet 1 */
15874e98e3e1Schristos dpd2char(sourhi); /* declet 2 */
15884e98e3e1Schristos
15894e98e3e1Schristos #elif DECPMAX==16
15904e98e3e1Schristos dpd2char(sourhi>>8); /* declet 1 */
15914e98e3e1Schristos dpd2char((sourhi<<2) | (sourlo>>30)); /* declet 2 */
15924e98e3e1Schristos dpd2char(sourlo>>20); /* declet 3 */
15934e98e3e1Schristos dpd2char(sourlo>>10); /* declet 4 */
15944e98e3e1Schristos dpd2char(sourlo); /* declet 5 */
15954e98e3e1Schristos
15964e98e3e1Schristos #elif DECPMAX==34
15974e98e3e1Schristos dpd2char(sourhi>>4); /* declet 1 */
15984e98e3e1Schristos dpd2char((sourhi<<6) | (sourmh>>26)); /* declet 2 */
15994e98e3e1Schristos dpd2char(sourmh>>16); /* declet 3 */
16004e98e3e1Schristos dpd2char(sourmh>>6); /* declet 4 */
16014e98e3e1Schristos dpd2char((sourmh<<4) | (sourml>>28)); /* declet 5 */
16024e98e3e1Schristos dpd2char(sourml>>18); /* declet 6 */
16034e98e3e1Schristos dpd2char(sourml>>8); /* declet 7 */
16044e98e3e1Schristos dpd2char((sourml<<2) | (sourlo>>30)); /* declet 8 */
16054e98e3e1Schristos dpd2char(sourlo>>20); /* declet 9 */
16064e98e3e1Schristos dpd2char(sourlo>>10); /* declet 10 */
16074e98e3e1Schristos dpd2char(sourlo); /* declet 11 */
16084e98e3e1Schristos #endif
16094e98e3e1Schristos
16104e98e3e1Schristos if (c==cstart) *c++='0'; /* all zeros, empty -- make "0" */
16114e98e3e1Schristos
16124e98e3e1Schristos /*[This fast path is valid but adds 3-5 cycles to worst case length] */
16134e98e3e1Schristos /*if (exp==0) { // integer or NaN case -- easy */
16144e98e3e1Schristos /* *c='\0'; // terminate */
16154e98e3e1Schristos /* return string; */
16164e98e3e1Schristos /* } */
16174e98e3e1Schristos
16184e98e3e1Schristos e=0; /* assume no E */
16194e98e3e1Schristos pre=(Int)(c-cstart)+exp; /* length+exp [c->LSD+1] */
16204e98e3e1Schristos /* [here, pre-exp is the digits count (==1 for zero)] */
16214e98e3e1Schristos
16224e98e3e1Schristos if (exp>0 || pre<-5) { /* need exponential form */
16234e98e3e1Schristos e=pre-1; /* calculate E value */
16244e98e3e1Schristos pre=1; /* assume one digit before '.' */
16254e98e3e1Schristos } /* exponential form */
16264e98e3e1Schristos
16274e98e3e1Schristos /* modify the coefficient, adding 0s, '.', and E+nn as needed */
16284e98e3e1Schristos if (pre>0) { /* ddd.ddd (plain), perhaps with E */
16294e98e3e1Schristos char *dotat=cstart+pre;
16304e98e3e1Schristos if (dotat<c) { /* if embedded dot needed... */
16314e98e3e1Schristos /* [memmove is a disaster, here] */
16324e98e3e1Schristos /* move by fours; there must be space for junk at the end */
16334e98e3e1Schristos /* because exponent is still possible */
16344e98e3e1Schristos s=dotat+ROUNDDOWN4(c-dotat); /* source */
16354e98e3e1Schristos t=s+1; /* target */
16364e98e3e1Schristos /* open the gap [cannot use memcpy] */
16374e98e3e1Schristos for (; s>=dotat; s-=4, t-=4) UBFROMUI(t, UBTOUI(s));
16384e98e3e1Schristos *dotat='.';
16394e98e3e1Schristos c++; /* length increased by one */
16404e98e3e1Schristos } /* need dot? */
16414e98e3e1Schristos
16424e98e3e1Schristos /* finally add the E-part, if needed; it will never be 0, and has */
16434e98e3e1Schristos /* a maximum length of 3 or 4 digits (asserted above) */
16444e98e3e1Schristos if (e!=0) {
16454e98e3e1Schristos memcpy(c, "E+", 2); /* starts with E, assume + */
16464e98e3e1Schristos c++;
16474e98e3e1Schristos if (e<0) {
16484e98e3e1Schristos *c='-'; /* oops, need '-' */
16494e98e3e1Schristos e=-e; /* uInt, please */
16504e98e3e1Schristos }
16514e98e3e1Schristos c++;
16524e98e3e1Schristos /* Three-character exponents are easy; 4-character a little trickier */
16534e98e3e1Schristos #if DECEMAXD<=3
16544e98e3e1Schristos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
16554e98e3e1Schristos /* copy fixed 4 characters [is safe], starting at non-zero */
16564e98e3e1Schristos /* and with character mask to convert BCD to char */
16574e98e3e1Schristos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK);
16584e98e3e1Schristos c+=*(u+3); /* bump pointer appropriately */
16594e98e3e1Schristos #elif DECEMAXD==4
16604e98e3e1Schristos if (e<1000) { /* 3 (or fewer) digits case */
16614e98e3e1Schristos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
16624e98e3e1Schristos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); /* [as above] */
16634e98e3e1Schristos c+=*(u+3); /* bump pointer appropriately */
16644e98e3e1Schristos }
16654e98e3e1Schristos else { /* 4-digits */
16664e98e3e1Schristos Int thou=((e>>3)*1049)>>17; /* e/1000 */
16674e98e3e1Schristos Int rem=e-(1000*thou); /* e%1000 */
16684e98e3e1Schristos *c++=(char)('0'+(char)thou); /* the thousands digit */
16694e98e3e1Schristos u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
16704e98e3e1Schristos UBFROMUI(c, UBTOUI(u)|CHARMASK); /* copy fixed 3+1 characters [is safe] */
16714e98e3e1Schristos c+=3; /* bump pointer, always 3 digits */
16724e98e3e1Schristos }
16734e98e3e1Schristos #endif
16744e98e3e1Schristos }
16754e98e3e1Schristos *c='\0'; /* add terminator */
16764e98e3e1Schristos /*printf("res %s\n", string); */
16774e98e3e1Schristos return string;
16784e98e3e1Schristos } /* pre>0 */
16794e98e3e1Schristos
16804e98e3e1Schristos /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
16814e98e3e1Schristos /* Surprisingly, this is close to being the worst-case path, so the */
16824e98e3e1Schristos /* shift is done by fours; this is a little tricky because the */
16834e98e3e1Schristos /* rightmost character to be written must not be beyond where the */
16844e98e3e1Schristos /* rightmost terminator could be -- so backoff to not touch */
16854e98e3e1Schristos /* terminator position if need be (this can make exact alignments */
16864e98e3e1Schristos /* for full Doubles, but in some cases needs care not to access too */
16874e98e3e1Schristos /* far to the left) */
16884e98e3e1Schristos
16894e98e3e1Schristos pre=-pre+2; /* gap width, including "0." */
16904e98e3e1Schristos t=cstart+ROUNDDOWN4(c-cstart)+pre; /* preferred first target point */
16914e98e3e1Schristos /* backoff if too far to the right */
16924e98e3e1Schristos if (t>string+DECSTRING-5) t=string+DECSTRING-5; /* adjust to fit */
16934e98e3e1Schristos /* now shift the entire coefficient to the right, being careful not */
16944e98e3e1Schristos /* to access to the left of string [cannot use memcpy] */
16954e98e3e1Schristos for (s=t-pre; s>=string; s-=4, t-=4) UBFROMUI(t, UBTOUI(s));
16964e98e3e1Schristos /* for Quads and Singles there may be a character or two left... */
16974e98e3e1Schristos s+=3; /* where next would come from */
16984e98e3e1Schristos for(; s>=cstart; s--, t--) *(t+3)=*(s);
16994e98e3e1Schristos /* now have fill 0. through 0.00000; use overlaps to avoid tests */
17004e98e3e1Schristos if (pre>=4) {
17014e98e3e1Schristos memcpy(cstart+pre-4, "0000", 4);
17024e98e3e1Schristos memcpy(cstart, "0.00", 4);
17034e98e3e1Schristos }
17044e98e3e1Schristos else { /* 2 or 3 */
17054e98e3e1Schristos *(cstart+pre-1)='0';
17064e98e3e1Schristos memcpy(cstart, "0.", 2);
17074e98e3e1Schristos }
17084e98e3e1Schristos *(c+pre)='\0'; /* terminate */
17094e98e3e1Schristos return string;
17104e98e3e1Schristos } /* decFloatToString */
17114e98e3e1Schristos
17124e98e3e1Schristos /* ------------------------------------------------------------------ */
17134e98e3e1Schristos /* decFloatToWider -- conversion to next-wider format */
17144e98e3e1Schristos /* */
17154e98e3e1Schristos /* source is the decFloat format number which gets the result of */
17164e98e3e1Schristos /* the conversion */
17174e98e3e1Schristos /* wider is the decFloatWider format number which will be narrowed */
17184e98e3e1Schristos /* returns wider */
17194e98e3e1Schristos /* */
17204e98e3e1Schristos /* Widening is always exact; no status is set (sNaNs are copied and */
17214e98e3e1Schristos /* do not signal). The result will be canonical if the source is, */
17224e98e3e1Schristos /* and may or may not be if the source is not. */
17234e98e3e1Schristos /* ------------------------------------------------------------------ */
17244e98e3e1Schristos /* widening is not possible for decQuad format numbers; simply omit */
17254e98e3e1Schristos #if !QUAD
decFloatToWider(const decFloat * source,decFloatWider * wider)17264e98e3e1Schristos decFloatWider * decFloatToWider(const decFloat *source, decFloatWider *wider) {
17274e98e3e1Schristos uInt msd;
17284e98e3e1Schristos
17294e98e3e1Schristos /* Construct and copy the sign word */
17304e98e3e1Schristos if (DFISSPECIAL(source)) {
17314e98e3e1Schristos /* copy sign, combination, and first bit of exponent (sNaN selector) */
17324e98e3e1Schristos DFWWORD(wider, 0)=DFWORD(source, 0)&0xfe000000;
17334e98e3e1Schristos msd=0;
17344e98e3e1Schristos }
17354e98e3e1Schristos else { /* is finite number */
17364e98e3e1Schristos uInt exp=GETEXPUN(source)+DECWBIAS; /* get unbiased exponent and rebias */
17374e98e3e1Schristos uInt code=(exp>>DECWECONL)<<29; /* set two bits of exp [msd=0] */
17384e98e3e1Schristos code|=(exp<<(32-6-DECWECONL)) & 0x03ffffff; /* add exponent continuation */
17394e98e3e1Schristos code|=DFWORD(source, 0)&0x80000000; /* add sign */
17404e98e3e1Schristos DFWWORD(wider, 0)=code; /* .. and place top word in wider */
17414e98e3e1Schristos msd=GETMSD(source); /* get source coefficient MSD [0-9] */
17424e98e3e1Schristos }
17434e98e3e1Schristos /* Copy the coefficient and clear any 'unused' words to left */
17444e98e3e1Schristos #if SINGLE
17454e98e3e1Schristos DFWWORD(wider, 1)=(DFWORD(source, 0)&0x000fffff)|(msd<<20);
17464e98e3e1Schristos #elif DOUBLE
17474e98e3e1Schristos DFWWORD(wider, 2)=(DFWORD(source, 0)&0x0003ffff)|(msd<<18);
17484e98e3e1Schristos DFWWORD(wider, 3)=DFWORD(source, 1);
17494e98e3e1Schristos DFWWORD(wider, 1)=0;
17504e98e3e1Schristos #endif
17514e98e3e1Schristos return wider;
17524e98e3e1Schristos } /* decFloatToWider */
17534e98e3e1Schristos #endif
17544e98e3e1Schristos
17554e98e3e1Schristos /* ------------------------------------------------------------------ */
17564e98e3e1Schristos /* decFloatVersion -- return package version string */
17574e98e3e1Schristos /* */
17584e98e3e1Schristos /* returns a constant string describing this package */
17594e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatVersion(void)17604e98e3e1Schristos const char *decFloatVersion(void) {
17614e98e3e1Schristos return DECVERSION;
17624e98e3e1Schristos } /* decFloatVersion */
17634e98e3e1Schristos
17644e98e3e1Schristos /* ------------------------------------------------------------------ */
17654e98e3e1Schristos /* decFloatZero -- set to canonical (integer) zero */
17664e98e3e1Schristos /* */
17674e98e3e1Schristos /* df is the decFloat format number to integer +0 (q=0, c=+0) */
17684e98e3e1Schristos /* returns df */
17694e98e3e1Schristos /* */
17704e98e3e1Schristos /* No error is possible, and no status can be set. */
17714e98e3e1Schristos /* ------------------------------------------------------------------ */
decFloatZero(decFloat * df)17724e98e3e1Schristos decFloat * decFloatZero(decFloat *df){
17734e98e3e1Schristos DFWORD(df, 0)=ZEROWORD; /* set appropriate top word */
17744e98e3e1Schristos #if DOUBLE || QUAD
17754e98e3e1Schristos DFWORD(df, 1)=0;
17764e98e3e1Schristos #if QUAD
17774e98e3e1Schristos DFWORD(df, 2)=0;
17784e98e3e1Schristos DFWORD(df, 3)=0;
17794e98e3e1Schristos #endif
17804e98e3e1Schristos #endif
17814e98e3e1Schristos /* decFloatShow(df, "zero"); */
17824e98e3e1Schristos return df;
17834e98e3e1Schristos } /* decFloatZero */
17844e98e3e1Schristos
17854e98e3e1Schristos /* ------------------------------------------------------------------ */
17864e98e3e1Schristos /* Private generic function (not format-specific) for development use */
17874e98e3e1Schristos /* ------------------------------------------------------------------ */
17884e98e3e1Schristos /* This is included once only, for all to use */
17894e98e3e1Schristos #if QUAD && (DECCHECK || DECTRACE)
17904e98e3e1Schristos /* ---------------------------------------------------------------- */
17914e98e3e1Schristos /* decShowNum -- display bcd8 number in debug form */
17924e98e3e1Schristos /* */
17934e98e3e1Schristos /* num is the bcdnum to display */
17944e98e3e1Schristos /* tag is a string to label the display */
17954e98e3e1Schristos /* ---------------------------------------------------------------- */
decShowNum(const bcdnum * num,const char * tag)17964e98e3e1Schristos void decShowNum(const bcdnum *num, const char *tag) {
17974e98e3e1Schristos const char *csign="+"; /* sign character */
17984e98e3e1Schristos uByte *ub; /* work */
17994e98e3e1Schristos uInt uiwork; /* for macros */
18004e98e3e1Schristos if (num->sign==DECFLOAT_Sign) csign="-";
18014e98e3e1Schristos
18024e98e3e1Schristos printf(">%s> ", tag);
18034e98e3e1Schristos if (num->exponent==DECFLOAT_Inf) printf("%sInfinity", csign);
18044e98e3e1Schristos else if (num->exponent==DECFLOAT_qNaN) printf("%sqNaN", csign);
18054e98e3e1Schristos else if (num->exponent==DECFLOAT_sNaN) printf("%ssNaN", csign);
18064e98e3e1Schristos else { /* finite */
18074e98e3e1Schristos char qbuf[10]; /* for right-aligned q */
18084e98e3e1Schristos char *c; /* work */
18094e98e3e1Schristos const uByte *u; /* .. */
18104e98e3e1Schristos Int e=num->exponent; /* .. exponent */
18114e98e3e1Schristos strcpy(qbuf, "q=");
18124e98e3e1Schristos c=&qbuf[2]; /* where exponent will go */
18134e98e3e1Schristos /* lay out the exponent */
18144e98e3e1Schristos if (e<0) {
18154e98e3e1Schristos *c++='-'; /* add '-' */
18164e98e3e1Schristos e=-e; /* uInt, please */
18174e98e3e1Schristos }
18184e98e3e1Schristos #if DECEMAXD>4
18194e98e3e1Schristos #error Exponent form is too long for ShowNum to lay out
18204e98e3e1Schristos #endif
18214e98e3e1Schristos if (e==0) *c++='0'; /* 0-length case */
18224e98e3e1Schristos else if (e<1000) { /* 3 (or fewer) digits case */
18234e98e3e1Schristos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
18244e98e3e1Schristos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); /* [as above] */
18254e98e3e1Schristos c+=*(u+3); /* bump pointer appropriately */
18264e98e3e1Schristos }
18274e98e3e1Schristos else { /* 4-digits */
18284e98e3e1Schristos Int thou=((e>>3)*1049)>>17; /* e/1000 */
18294e98e3e1Schristos Int rem=e-(1000*thou); /* e%1000 */
18304e98e3e1Schristos *c++=(char)('0'+(char)thou); /* the thousands digit */
18314e98e3e1Schristos u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
18324e98e3e1Schristos UBFROMUI(c, UBTOUI(u)|CHARMASK); /* copy fixed 3+1 characters [is safe] */
18334e98e3e1Schristos c+=3; /* bump pointer, always 3 digits */
18344e98e3e1Schristos }
18354e98e3e1Schristos *c='\0'; /* add terminator */
18364e98e3e1Schristos printf("%7s c=%s", qbuf, csign);
18374e98e3e1Schristos }
18384e98e3e1Schristos
18394e98e3e1Schristos if (!EXPISSPECIAL(num->exponent) || num->msd!=num->lsd || *num->lsd!=0) {
18404e98e3e1Schristos for (ub=num->msd; ub<=num->lsd; ub++) { /* coefficient... */
18414e98e3e1Schristos printf("%1x", *ub);
18424e98e3e1Schristos if ((num->lsd-ub)%3==0 && ub!=num->lsd) printf(" "); /* 4-space */
18434e98e3e1Schristos }
18444e98e3e1Schristos }
18454e98e3e1Schristos printf("\n");
18464e98e3e1Schristos } /* decShowNum */
18474e98e3e1Schristos #endif
1848