decNumber.c (727385c4e13e1a5a985124a20a2370855141111d) decNumber.c (21d7826fdbf13bc3180f8f23f3f87967604fdf7e)
1/* Decimal number arithmetic module for the decNumber C Library.
2 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
3 Contributed by IBM Corporation. Author Mike Cowlishaw.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free

--- 250 unchanged lines hidden (view full) ---

259static Int decShiftToLeast(Unit *, Int, Int);
260static Int decShiftToMost(Unit *, Int, Int);
261static void decStatus(decNumber *, uInt, decContext *);
262static void decToString(const decNumber *, char[], Flag);
263static decNumber * decTrim(decNumber *, decContext *, Flag, Int *);
264static Int decUnitAddSub(const Unit *, Int, const Unit *, Int, Int,
265 Unit *, Int);
266static Int decUnitCompare(const Unit *, Int, const Unit *, Int, Int);
1/* Decimal number arithmetic module for the decNumber C Library.
2 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
3 Contributed by IBM Corporation. Author Mike Cowlishaw.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free

--- 250 unchanged lines hidden (view full) ---

259static Int decShiftToLeast(Unit *, Int, Int);
260static Int decShiftToMost(Unit *, Int, Int);
261static void decStatus(decNumber *, uInt, decContext *);
262static void decToString(const decNumber *, char[], Flag);
263static decNumber * decTrim(decNumber *, decContext *, Flag, Int *);
264static Int decUnitAddSub(const Unit *, Int, const Unit *, Int, Int,
265 Unit *, Int);
266static Int decUnitCompare(const Unit *, Int, const Unit *, Int, Int);
267static bool mulUInt128ByPowOf10(uLong *, uLong *, uInt);
267
268#if !DECSUBSET
269/* decFinish == decFinalize when no subset arithmetic needed */
270#define decFinish(a,b,c,d) decFinalize(a,b,c,d)
271#else
272static void decFinish(decNumber *, decContext *, Int *, uInt *);
273static decNumber * decRoundOperand(const decNumber *, decContext *, uInt *);
274#endif

--- 262 unchanged lines hidden (view full) ---

537 return (decNumberIsNegative(dn)) ? -((int64_t)hi) : (int64_t)hi;
538 }
539
540Invalid:
541 decContextSetStatus(set, DEC_Invalid_operation);
542 return 0;
543} /* decNumberIntegralToInt64 */
544
268
269#if !DECSUBSET
270/* decFinish == decFinalize when no subset arithmetic needed */
271#define decFinish(a,b,c,d) decFinalize(a,b,c,d)
272#else
273static void decFinish(decNumber *, decContext *, Int *, uInt *);
274static decNumber * decRoundOperand(const decNumber *, decContext *, uInt *);
275#endif

--- 262 unchanged lines hidden (view full) ---

538 return (decNumberIsNegative(dn)) ? -((int64_t)hi) : (int64_t)hi;
539 }
540
541Invalid:
542 decContextSetStatus(set, DEC_Invalid_operation);
543 return 0;
544} /* decNumberIntegralToInt64 */
545
546/* ------------------------------------------------------------------ */
547/* decNumberIntegralToInt128 -- conversion to int128 */
548/* */
549/* dn is the decNumber to convert. dn is assumed to have been */
550/* rounded to a floating point integer value. */
551/* set is the context for reporting errors */
552/* returns the converted decNumber via plow and phigh */
553/* */
554/* Invalid is set if the decNumber is a NaN, Infinite or is out of */
555/* range for a signed 128 bit integer. */
556/* ------------------------------------------------------------------ */
545
557
558void decNumberIntegralToInt128(const decNumber *dn, decContext *set,
559 uint64_t *plow, uint64_t *phigh)
560{
561 int d; /* work */
562 const Unit *up; /* .. */
563 uint64_t lo = 0, hi = 0;
564
565 if (decNumberIsSpecial(dn) || (dn->exponent < 0) ||
566 (dn->digits + dn->exponent > 39)) {
567 goto Invalid;
568 }
569
570 up = dn->lsu; /* -> lsu */
571
572 for (d = (dn->digits - 1) / DECDPUN; d >= 0; d--) {
573 if (mulu128(&lo, &hi, DECDPUNMAX + 1)) {
574 /* overflow */
575 goto Invalid;
576 }
577 if (uadd64_overflow(lo, up[d], &lo)) {
578 if (uadd64_overflow(hi, 1, &hi)) {
579 /* overflow */
580 goto Invalid;
581 }
582 }
583 }
584
585 if (mulUInt128ByPowOf10(&lo, &hi, dn->exponent)) {
586 /* overflow */
587 goto Invalid;
588 }
589
590 if (decNumberIsNegative(dn)) {
591 if (lo == 0) {
592 *phigh = -hi;
593 *plow = 0;
594 } else {
595 *phigh = ~hi;
596 *plow = -lo;
597 }
598 } else {
599 *plow = lo;
600 *phigh = hi;
601 }
602
603 return;
604
605Invalid:
606 decContextSetStatus(set, DEC_Invalid_operation);
607} /* decNumberIntegralToInt128 */
608
546/* ------------------------------------------------------------------ */
547/* to-scientific-string -- conversion to numeric string */
548/* to-engineering-string -- conversion to numeric string */
549/* */
550/* decNumberToString(dn, string); */
551/* decNumberToEngString(dn, string); */
552/* */
553/* dn is the decNumber to convert */

--- 7326 unchanged lines hidden (view full) ---

7880 #endif
7881 #endif
7882 #endif
7883 break;
7884 } /* up */
7885 return digits;
7886 } /* decGetDigits */
7887
609/* ------------------------------------------------------------------ */
610/* to-scientific-string -- conversion to numeric string */
611/* to-engineering-string -- conversion to numeric string */
612/* */
613/* decNumberToString(dn, string); */
614/* decNumberToEngString(dn, string); */
615/* */
616/* dn is the decNumber to convert */

--- 7326 unchanged lines hidden (view full) ---

7943 #endif
7944 #endif
7945 #endif
7946 break;
7947 } /* up */
7948 return digits;
7949 } /* decGetDigits */
7950
7951/* ------------------------------------------------------------------ */
7952/* mulUInt128ByPowOf10 -- multiply a 128-bit unsigned integer by a */
7953/* power of 10. */
7954/* */
7955/* The 128-bit factor composed of plow and phigh is multiplied */
7956/* by 10^exp. */
7957/* */
7958/* plow pointer to the low 64 bits of the first factor */
7959/* phigh pointer to the high 64 bits of the first factor */
7960/* exp the exponent of the power of 10 of the second factor */
7961/* */
7962/* If the result fits in 128 bits, returns false and the */
7963/* multiplication result through plow and phigh. */
7964/* Otherwise, returns true. */
7965/* ------------------------------------------------------------------ */
7966static bool mulUInt128ByPowOf10(uLong *plow, uLong *phigh, uInt pow10)
7967{
7968 while (pow10 >= ARRAY_SIZE(powers)) {
7969 if (mulu128(plow, phigh, powers[ARRAY_SIZE(powers) - 1])) {
7970 /* Overflow */
7971 return true;
7972 }
7973 pow10 -= ARRAY_SIZE(powers) - 1;
7974 }
7975
7976 if (pow10 > 0) {
7977 return mulu128(plow, phigh, powers[pow10]);
7978 } else {
7979 return false;
7980 }
7981}
7982
7888#if DECTRACE | DECCHECK
7889/* ------------------------------------------------------------------ */
7890/* decNumberShow -- display a number [debug aid] */
7891/* dn is the number to show */
7892/* */
7893/* Shows: sign, exponent, coefficient (msu first), digits */
7894/* or: sign, special-value */
7895/* ------------------------------------------------------------------ */

--- 333 unchanged lines hidden ---
7983#if DECTRACE | DECCHECK
7984/* ------------------------------------------------------------------ */
7985/* decNumberShow -- display a number [debug aid] */
7986/* dn is the number to show */
7987/* */
7988/* Shows: sign, exponent, coefficient (msu first), digits */
7989/* or: sign, special-value */
7990/* ------------------------------------------------------------------ */

--- 333 unchanged lines hidden ---