DEC_TYPE INTERNAL_FUNCTION_NAME (DEC_TYPE x) { decContext context; decNumber dn_result; DEC_TYPE result; decNumber dn_x; decNumber dn_tmp; decNumber dn_log10; decNumber dn_one; decNumber dn_cmp; enum rounding round; FUNC_CONVERT_TO_DN (&x, &dn_x); if (decNumberIsNaN (&dn_x)) return x+x; if (decNumberIsInfinite (&dn_x)) /* +-Inf: Inf */ return DEC_INFINITY; if (decNumberIsZero (&dn_x)) /* Pole Error if x==0 */ { DFP_ERRNO (ERANGE); DFP_EXCEPT (FE_DIVBYZERO); return -DFP_HUGE_VAL; } if (decNumberIsInfinite (&dn_x) && decNumberIsNegative (&dn_x)) return -x; decContextDefault (&context, DEFAULT_CONTEXT); decNumberAbs (&dn_tmp, &dn_x, &context); /* For DFP, we use radix 10 instead of whatever FLT_RADIX happens to be */ decNumberLog10 (&dn_log10, &dn_tmp, &context); /* Capture the case where truncation will return the wrong result, by rounding up if -1.0 < x < 1.0 */ round = DEC_ROUND_DOWN; decNumberFromInt32 (&dn_one, 1); decNumberCompare (&dn_cmp, &dn_x, &dn_one, &context); if (-decNumberIsNegative(&dn_cmp)) { decNumberFromInt32 (&dn_one, -1); decNumberCompare (&dn_cmp, &dn_x, &dn_one, &context); if (!decNumberIsNegative(&dn_cmp) && !decNumberIsZero(&dn_cmp)) round = DEC_ROUND_UP; } context.round = round; decNumberToIntegralValue (&dn_result, &dn_log10, &context); FUNC_CONVERT_FROM_DN (&dn_result, &result, &context); return result; }
static DEC_TYPE IEEE_FUNCTION_NAME (DEC_TYPE x) { decContext context; decNumber dn_result; DEC_TYPE result; decNumber dn_x; decNumber dn_two; DEC_TYPE two = DFP_CONSTANT(2.0); FUNC_CONVERT_TO_DN (&two, &dn_two); FUNC_CONVERT_TO_DN (&x, &dn_x); if (decNumberIsNaN (&dn_x)) return x+x; if (decNumberIsInfinite (&dn_x) ) return decNumberIsNegative (&dn_x) ? DFP_CONSTANT(0.0) : x; decContextDefault (&context, DEFAULT_CONTEXT); /* decNumberPow (&dn_result, &dn_two, &dn_x, &context); */ decNumberPower (&dn_result, &dn_two, &dn_x, &context); FUNC_CONVERT_FROM_DN (&dn_result, &result, &context); if(context.status & DEC_Overflow) DFP_EXCEPT (FE_OVERFLOW); return result; }
_Decimal128 __quantumd128 (_Decimal128 x) { decNumber dn_x; decNumber dn_result; decContext context; _Decimal128 result; FUNC_CONVERT_TO_DN (&x, &dn_x); if (decNumberIsNaN (&dn_x) || decNumberIsZero (&dn_x)) return x; if (decNumberIsInfinite (&dn_x)) return DEC_INFINITY; /* The quantum of a finite number is defined as 1 x 10^exponent, so first get input absolute value and then sets its coefficient to 1. */ decContextDefault (&context, DEFAULT_CONTEXT); decNumberAbs (&dn_result, &dn_x, &context); dn_result.digits = 1; dn_result.lsu[0] = 1; FUNC_CONVERT_FROM_DN (&dn_result, &result, &context); return result; }
static DEC_TYPE IEEE_FUNCTION_NAME (DEC_TYPE x) { decContext context; decNumber dn_result; DEC_TYPE result; decNumber dn_x; FUNC_CONVERT_TO_DN(&x, &dn_x); if (decNumberIsNaN (&dn_x)) return x+x; if (decNumberIsZero (&dn_x)) /* If x == 0: Pole Error */ { DFP_EXCEPT (FE_DIVBYZERO); return -DFP_HUGE_VAL; } if (decNumberIsNegative (&dn_x)) /* If x < 0,: Domain Error */ { DFP_EXCEPT (FE_INVALID); return DFP_NAN; } if (decNumberIsInfinite (&dn_x)) return x; decContextDefault (&context, DEFAULT_CONTEXT); decNumberLn(&dn_result, &dn_x, &context); FUNC_CONVERT_FROM_DN(&dn_result, &result, &context); return result; }
DEC_TYPE INTERNAL_FUNCTION_NAME (DEC_TYPE x) { decContext context; decNumber dn_result; DEC_TYPE result, one; decNumber dn_x, dn_one; one = DFP_CONSTANT(1.0); FUNC_CONVERT_TO_DN (&one, &dn_one); FUNC_CONVERT_TO_DN (&x, &dn_x); if (decNumberIsNaN (&dn_x) || decNumberIsZero (&dn_x) || decNumberIsInfinite (&dn_x)) { return x + x; } decContextDefault (&context, DEFAULT_CONTEXT); /* using trig identity: acosh(x) = log(x+sqrt(x*x-1)) */ decNumberMultiply (&dn_result, &dn_x, &dn_x, &context); decNumberAdd (&dn_result, &dn_result, &dn_one, &context); decNumberSquareRoot (&dn_result, &dn_result, &context); decNumberAdd (&dn_result, &dn_result, &dn_x, &context); decNumberLn (&dn_result, &dn_result, &context); FUNC_CONVERT_FROM_DN (&dn_result, &result, &context); return result; }
DEC_TYPE INTERNAL_FUNCTION_NAME (DEC_TYPE x) { decContext context; decNumber dn_result; DEC_TYPE result; decNumber dn_x; FUNC_CONVERT_TO_DN (&x, &dn_x); decContextDefault (&context, DEFAULT_CONTEXT); if (decNumberIsInfinite (&dn_x)) { if (decNumberIsNegative (&dn_x)) result = -M_PI_2dl; else result = M_PI_2dl; } else { decNumberAtan (&dn_result, &dn_x, &context); FUNC_CONVERT_FROM_DN (&dn_result, &result, &context); } return result; }
_RETURN_TYPE INTERNAL_FUNCTION_NAME (DEC_TYPE x) { DEC_TYPE result; decContext context; decNumber dn_result; decNumber dn_x; decNumber dn_absx; decNumber dn_logx; decNumber dn_one; decNumber dn_cmp; enum rounding round; FUNC_CONVERT_TO_DN (&x, &dn_x); if (decNumberIsZero (&dn_x)) { DFP_EXCEPT (FE_INVALID); DFP_ERRNO (EDOM); return _FBLOG0; } if (decNumberIsInfinite (&dn_x)) { DFP_EXCEPT (FE_INVALID); DFP_ERRNO (EDOM); return decNumberIsNegative (&dn_x) ? _MIN_VALUE : _MAX_VALUE; } if (decNumberIsNaN (&dn_x)) { DFP_EXCEPT (FE_INVALID); DFP_ERRNO (EDOM); return _FBLOGNAN; } decContextDefault (&context, DEFAULT_CONTEXT); decNumberAbs (&dn_absx, &dn_x, &context); /* For DFP, we use radix 10 instead of whatever FLT_RADIX happens to be */ decNumberLog10 (&dn_logx, &dn_absx, &context); /* Capture the case where truncation will return the wrong result, by rounding up if -1.0 < x < 1.0 */ round = DEC_ROUND_DOWN; decNumberFromInt32 (&dn_one, 1); decNumberCompare (&dn_cmp, &dn_x, &dn_one, &context); if (-decNumberIsNegative(&dn_cmp)) { decNumberFromInt32 (&dn_one, -1); decNumberCompare (&dn_cmp, &dn_x, &dn_one, &context); if (!decNumberIsNegative(&dn_cmp) && !decNumberIsZero(&dn_cmp)) round = DEC_ROUND_UP; } context.round = round; decNumberToIntegralValue (&dn_result, &dn_logx, &context); FUNC_CONVERT_FROM_DN (&dn_result, &result, &context); /* Use _Decimal* to int casting. */ return (_RETURN_TYPE) result; }
static DEC_TYPE IEEE_FUNCTION_NAME (DEC_TYPE x) { decContext context; decNumber dn_result; DEC_TYPE result; decNumber dn_x; decNumber dn_one; decNumber dn_exponent; DEC_TYPE one = DFP_CONSTANT(1.0); FUNC_CONVERT_TO_DN(&x, &dn_x); FUNC_CONVERT_TO_DN(&one, &dn_one); if (decNumberIsNaN (&dn_x)) return x+x; if (decNumberIsInfinite (&dn_x)) return decNumberIsNegative (&dn_x) ? DFP_CONSTANT(-1.0) : x; decContextDefault(&context, DEFAULT_CONTEXT); decNumberExp(&dn_exponent, &dn_x, &context); decNumberSubtract(&dn_result, &dn_exponent, &dn_one, &context); FUNC_CONVERT_FROM_DN(&dn_result, &result, &context); if (context.status & DEC_Overflow) DFP_EXCEPT (FE_OVERFLOW); return result; }
static __ROUND_RETURN_TYPE IEEE_FUNCTION_NAME (DEC_TYPE x) { DEC_TYPE result; decContext context; decNumber dn_result; decNumber dn_x; FUNC_CONVERT_TO_DN (&x, &dn_x); if (decNumberIsNaN (&dn_x) || decNumberIsInfinite (&dn_x) || x > __MAX_VALUE || x < __MIN_VALUE) { DFP_EXCEPT (FE_INVALID); return (__ROUND_RETURN_TYPE) x; } decContextDefault (&context, DEFAULT_CONTEXT); context.round = __ROUND_MODE; decNumberToIntegralValue (&dn_result,&dn_x,&context); FUNC_CONVERT_FROM_DN(&dn_result, &result, &context); /* Use _Decimal* to __ROUND_RETURN_TYPE casting. */ return (__ROUND_RETURN_TYPE)result; /* return (__ROUND_RETURN_TYPE)decNumberToInteger (&dn_result); */ }
int __fpclassifyd64 (_Decimal64 x) { decNumber dn_x; decContext context; FUNC_CONVERT_TO_DN (&x, &dn_x); if (decNumberIsNaN (&dn_x)) return FP_NAN; else if (decNumberIsInfinite (&dn_x)) return FP_INFINITE; else if (decNumberIsZero (&dn_x)) return FP_ZERO; /* Since DFP value are not normalized, checking the exponent for normal/subnormal is not suffice. For instance, the value 10e-96 will result in a expoenent below the minimum, however it is still a FP_NORMAL number due implicit normalization. TO avoid such traps the check relies on runtime comparisons. */ decContextDefault (&context, DEC_INIT_DECIMAL64); if (decNumberIsSubnormal (&dn_x, &context)) return FP_SUBNORMAL; return FP_NORMAL; }
static void decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context) { memset (r, 0, sizeof (REAL_VALUE_TYPE)); r->cl = rvc_normal; if (decNumberIsZero (dn)) r->cl = rvc_zero; if (decNumberIsNaN (dn)) r->cl = rvc_nan; if (decNumberIsInfinite (dn)) r->cl = rvc_inf; if (context->status & DEC_Overflow) r->cl = rvc_inf; if (decNumberIsNegative (dn)) r->sign = 1; r->decimal = 1; if (r->cl != rvc_normal) return; decContextDefault (context, DEC_INIT_DECIMAL128); context->traps = 0; decimal128FromNumber ((decimal128 *) r->sig, dn, context); }
int isinfd32 (_Decimal32 arg) { decNumber dn; decimal32 d32; __host_to_ieee_32 (arg, &d32); decimal32ToNumber (&d32, &dn); return (decNumberIsInfinite (&dn)); }
int isinfd64 (_Decimal64 arg) { decNumber dn; decimal64 d64; __host_to_ieee_64 (arg, &d64); decimal64ToNumber (&d64, &dn); return (decNumberIsInfinite (&dn)); }
int isinfd128 (_Decimal128 arg) { decNumber dn; decimal128 d128; __host_to_ieee_128 (arg, &d128); decimal128ToNumber (&d128, &dn); return (decNumberIsInfinite (&dn)); }
const DecimalDecNumber &DecimalDecNumber::operator /=(const DecimalDecNumber &rhs) { if (decNumberIsNaN(&m_value) || decNumberIsNaN(&rhs.m_value)) { // FTHROW(InvalidStateException, "Performing arithmetic on uninitialised decimal [Nan]"); throw("Performing arithmetic on uninitialised decimal [Nan]"); } if (decNumberIsZero(&rhs.m_value)) { // FTHROW(LogicError, "Division by zero"); throw("Division by zero"); } if (decNumberIsInfinite(&m_value) || decNumberIsInfinite(&rhs.m_value)) { throw("Cannot divide infinity by infinity"); } decNumberDivide(&m_value, &m_value, &rhs.m_value, &m_context); return *this; }
/* Compute a factorial. * Currently, only for positive integer arguments. Needs to be extended * to a full gamma function. */ decNumber *decNumberFactorial(decNumber *r, const decNumber *x, decContext *ctx) { decNumber y, const_1; int_to_dn(&const_1, 1, ctx); decNumberCopy(&y, x); if (!decNumberIsNegative(x) || decNumberIsZero(x)) { decNumberCopy(r, &const_1); for (;;) { if (decNumberIsZero(&y)) break; if (decNumberIsInfinite(r)) break; decNumberMultiply(r, r, &y, ctx); decNumberSubtract(&y, &y, &const_1, ctx); } } return r; }
static DEC_TYPE IEEE_FUNCTION_NAME (DEC_TYPE x) { decContext context; decNumber dn_result; DEC_TYPE result; decNumber dn_x; decNumber dn_sum; decNumber dn_one; DEC_TYPE one = DFP_CONSTANT(1.0); FUNC_CONVERT_TO_DN (&x, &dn_x); FUNC_CONVERT_TO_DN (&one, &dn_one); /* For NaN, 0, or +Inf, just return x */ if (decNumberIsNaN (&dn_x) || decNumberIsZero (&dn_x) || (decNumberIsInfinite (&dn_x) && !decNumberIsNegative (&dn_x))) return x+x; decContextDefault(&context, DEFAULT_CONTEXT); decNumberAdd(&dn_sum, &dn_x, &dn_one, &context); if (decNumberIsZero(&dn_sum)) /* Pole Error if x was -1 */ { DFP_EXCEPT (FE_DIVBYZERO); return -DFP_HUGE_VAL; } if (decNumberIsNegative(&dn_sum)) /* Domain Error if x < -1 */ { DFP_EXCEPT (FE_INVALID); return DFP_NAN; } decNumberLn(&dn_result, &dn_sum, &context); FUNC_CONVERT_FROM_DN(&dn_result, &result, &context); return result; }
DEC_TYPE INTERNAL_FUNCTION_NAME (DEC_TYPE x) { decContext context; decNumber dn_result; DEC_TYPE result; decNumber dn_x; FUNC_CONVERT_TO_DN (&x, &dn_x); if (decNumberIsNaN (&dn_x) || decNumberIsInfinite (&dn_x) || decNumberIsZero (&dn_x)) return x+x; decContextDefault (&context, DEFAULT_CONTEXT); context.round = __ROUND_MODE; decNumberToIntegralValue (&dn_result, &dn_x, &context); FUNC_CONVERT_FROM_DN (&dn_result, &result, &context); if (context.status & DEC_Overflow) DFP_EXCEPT (FE_OVERFLOW); return result; }
int DecimalDecNumber::isInfinity() const { if (decNumberIsInfinite(&m_value)) return isNegative() ? -1 : 1; return 0; }
uint32_t decSingleIsInfinite (const decSingle* _0) noexcept { decNumber _0num; decSingleToNumber (_0, &_0num); return decNumberIsInfinite (&_0num); }
bool DecimalDecNumber::isValid() const { return !decNumberIsNaN(&m_value) && !decNumberIsInfinite(&m_value); }
static DEC_TYPE IEEE_FUNCTION_NAME (DEC_TYPE x, DEC_TYPE y) { decContext context; decNumber dn_result; DEC_TYPE result; DEC_TYPE absx; decNumber dn_x; decNumber dn_absx; decNumber dn_y; decNumber dn_one; decNumber dn_two; decNumber dn_temp; decNumber dn_temp2; decNumber dn_temp3; int y_is_int; int y_is_oddint=0; int abs_x_vs_1; DEC_TYPE one = DFP_CONSTANT(1.0); DEC_TYPE two = DFP_CONSTANT(2.0); FUNC_CONVERT_TO_DN (&x, &dn_x); FUNC_CONVERT_TO_DN (&y, &dn_y); FUNC_CONVERT_TO_DN (&one, &dn_one); decContextDefault (&context, DEFAULT_CONTEXT); if (decNumberIsZero (&dn_y)) return one; if (decNumberIsNaN (&dn_x)) return x+x; decNumberAbs (&dn_absx, &dn_x, &context); FUNC_CONVERT_FROM_DN (&dn_absx, &absx, &context); if(absx<one) abs_x_vs_1 = -1; else if (absx==one) abs_x_vs_1 = 0; else abs_x_vs_1 = 1; /* abs_x_vs_1 = decCompare(&dn_absx, &dn_one); */ if(abs_x_vs_1 == 0 && !decNumberIsNegative (&dn_x)) /* If x == +1 */ return one; if (decNumberIsNaN (&dn_y)) return y+y; /* Detect if y is odd/an integer */ decNumberToIntegralValue (&dn_temp, &dn_y, &context); decNumberSubtract (&dn_temp2, &dn_temp, &dn_y, &context); y_is_int = decNumberIsZero (&dn_temp2); if (y_is_int) { FUNC_CONVERT_TO_DN (&two, &dn_two); decNumberDivide (&dn_temp, &dn_y, &dn_two, &context); decNumberToIntegralValue (&dn_temp2, &dn_temp, &context); decNumberSubtract (&dn_temp3, &dn_temp2, &dn_temp, &context); y_is_oddint = !decNumberIsZero (&dn_temp3); } /* Handle all special cases for which x = +-0 */ if (decNumberIsZero (&dn_x)) { if(decNumberIsNegative (&dn_y)) { if (decNumberIsInfinite (&dn_y)) /* +-0^-Inf = +Inf */ return -y; /* Pole Error for x = +-0, y < 0 */ DFP_EXCEPT (FE_DIVBYZERO); return decNumberIsNegative(&dn_x) && y_is_oddint ? -DFP_HUGE_VAL : DFP_HUGE_VAL; } return decNumberIsNegative(&dn_x) && y_is_oddint ? -DFP_CONSTANT(0.0) : DFP_CONSTANT(0.0); } /* Handle remaining special cases for x = +-Inf or y = +-Inf */ if (decNumberIsInfinite (&dn_x) || decNumberIsInfinite (&dn_y)) { if (abs_x_vs_1 == 0) /* If (-1)^(+-Inf) */ return one; if (abs_x_vs_1 < 0) /* x^(+-Inf), where 0<x<1 */ return decNumberIsNegative (&dn_y) ? DFP_HUGE_VAL : DFP_CONSTANT(0.0); if (decNumberIsNegative (&dn_y)) result = DFP_CONSTANT(0.0); else result = (DEC_TYPE)DEC_INFINITY; if (y_is_oddint && decNumberIsNegative(&dn_x)) result = -result; return result; } /* Domain Error: x < 0 && y is a finite non-int */ if (decNumberIsNegative (&dn_x) && !y_is_int) { DFP_EXCEPT (FE_INVALID); return DFP_NAN; } decNumberPower (&dn_result, &dn_x, &dn_y, &context); FUNC_CONVERT_FROM_DN (&dn_result, &result, &context); if (context.status & DEC_Overflow) DFP_EXCEPT (FE_OVERFLOW); if (context.status & DEC_Underflow) DFP_EXCEPT (FE_UNDERFLOW); return result; }