Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* This is a software decimal floating point library.
  2.    Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3.  
  4. This file is part of GCC.
  5.  
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10.  
  11. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. Under Section 7 of GPL version 3, you are granted additional
  17. permissions described in the GCC Runtime Library Exception, version
  18. 3.1, as published by the Free Software Foundation.
  19.  
  20. You should have received a copy of the GNU General Public License and
  21. a copy of the GCC Runtime Library Exception along with this program;
  22. see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
  23. <http://www.gnu.org/licenses/>.  */
  24.  
  25. /* This implements IEEE 754 decimal floating point arithmetic, but
  26.    does not provide a mechanism for setting the rounding mode, or for
  27.    generating or handling exceptions.  Conversions between decimal
  28.    floating point types and other types depend on C library functions.
  29.  
  30.    Contributed by Ben Elliston  <bje@au.ibm.com>.  */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. /* FIXME: compile with -std=gnu99 to get these from stdlib.h */
  35. extern float strtof (const char *, char **);
  36. extern long double strtold (const char *, char **);
  37. #include <string.h>
  38. #include <limits.h>
  39.  
  40. #include "dfp-bit.h"
  41.  
  42. /* Forward declarations.  */
  43. #if WIDTH == 32 || WIDTH_TO == 32
  44. void __host_to_ieee_32 (_Decimal32 in, decimal32 *out);
  45. void __ieee_to_host_32 (decimal32 in, _Decimal32 *out);
  46. #endif
  47. #if WIDTH == 64 || WIDTH_TO == 64
  48. void __host_to_ieee_64 (_Decimal64 in, decimal64 *out);
  49. void __ieee_to_host_64 (decimal64 in, _Decimal64 *out);
  50. #endif
  51. #if WIDTH == 128 || WIDTH_TO == 128
  52. void __host_to_ieee_128 (_Decimal128 in, decimal128 *out);
  53. void __ieee_to_host_128 (decimal128 in, _Decimal128 *out);
  54. #endif
  55.  
  56. /* A pointer to a binary decFloat operation.  */
  57. typedef decFloat* (*dfp_binary_func)
  58.      (decFloat *, const decFloat *, const decFloat *, decContext *);
  59. /* Binary operations.  */
  60.  
  61. /* Use a decFloat (decDouble or decQuad) function to perform a DFP
  62.    binary operation.  */
  63. static inline decFloat
  64. dfp_binary_op (dfp_binary_func op, decFloat arg_a, decFloat arg_b)
  65. {
  66.   decFloat result;
  67.   decContext context;
  68.  
  69.   decContextDefault (&context, CONTEXT_INIT);
  70.   DFP_INIT_ROUNDMODE (context.round);
  71.  
  72.   /* Perform the operation.  */
  73.   op (&result, &arg_a, &arg_b, &context);
  74.  
  75.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  76.     {
  77.       /* decNumber exception flags we care about here.  */
  78.       int ieee_flags;
  79.       int dec_flags = DEC_IEEE_854_Division_by_zero | DEC_IEEE_854_Inexact
  80.                       | DEC_IEEE_854_Invalid_operation | DEC_IEEE_854_Overflow
  81.                       | DEC_IEEE_854_Underflow;
  82.       dec_flags &= context.status;
  83.       ieee_flags = DFP_IEEE_FLAGS (dec_flags);
  84.       if (ieee_flags != 0)
  85.         DFP_HANDLE_EXCEPTIONS (ieee_flags);
  86.     }
  87.  
  88.   return result;
  89. }
  90.  
  91. #if WIDTH == 32
  92. /* The decNumber package doesn't provide arithmetic for decSingle (32 bits);
  93.    convert to decDouble, use the operation for that, and convert back.  */
  94. static inline _Decimal32
  95. d32_binary_op (dfp_binary_func op, _Decimal32 arg_a, _Decimal32 arg_b)
  96. {
  97.   union { _Decimal32 c; decSingle f; } a32, b32, res32;
  98.   decDouble a, b, res;
  99.   decContext context;
  100.  
  101.   /* Widen the operands and perform the operation.  */
  102.   a32.c = arg_a;
  103.   b32.c = arg_b;
  104.   decSingleToWider (&a32.f, &a);
  105.   decSingleToWider (&b32.f, &b);
  106.   res = dfp_binary_op (op, a, b);
  107.  
  108.   /* Narrow the result, which might result in an underflow or overflow.  */
  109.   decContextDefault (&context, CONTEXT_INIT);
  110.   DFP_INIT_ROUNDMODE (context.round);
  111.   decSingleFromWider (&res32.f, &res, &context);
  112.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  113.     {
  114.       /* decNumber exception flags we care about here.  */
  115.       int ieee_flags;
  116.       int dec_flags = DEC_IEEE_854_Inexact | DEC_IEEE_854_Overflow
  117.                       | DEC_IEEE_854_Underflow;
  118.       dec_flags &= context.status;
  119.       ieee_flags = DFP_IEEE_FLAGS (dec_flags);
  120.       if (ieee_flags != 0)
  121.         DFP_HANDLE_EXCEPTIONS (ieee_flags);
  122.     }
  123.  
  124.   return res32.c;
  125. }
  126. #else
  127. /* decFloat operations are supported for decDouble (64 bits) and
  128.    decQuad (128 bits).  The bit patterns for the types are the same.  */
  129. static inline DFP_C_TYPE
  130. dnn_binary_op (dfp_binary_func op, DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  131. {
  132.   union { DFP_C_TYPE c; decFloat f; } a, b, result;
  133.  
  134.   a.c = arg_a;
  135.   b.c = arg_b;
  136.   result.f = dfp_binary_op (op, a.f, b.f);
  137.   return result.c;
  138. }
  139. #endif
  140.  
  141. /* Comparison operations.  */
  142.  
  143. /* Use a decFloat (decDouble or decQuad) function to perform a DFP
  144.    comparison.  */
  145. static inline CMPtype
  146. dfp_compare_op (dfp_binary_func op, decFloat arg_a, decFloat arg_b)
  147. {
  148.   decContext context;
  149.   decFloat res;
  150.   int result;
  151.  
  152.   decContextDefault (&context, CONTEXT_INIT);
  153.   DFP_INIT_ROUNDMODE (context.round);
  154.  
  155.   /* Perform the comparison.  */
  156.   op (&res, &arg_a, &arg_b, &context);
  157.  
  158.   if (DEC_FLOAT_IS_SIGNED (&res))
  159.     result = -1;
  160.   else if (DEC_FLOAT_IS_ZERO (&res))
  161.     result = 0;
  162.   else if (DEC_FLOAT_IS_NAN (&res))
  163.     result = -2;
  164.   else
  165.     result = 1;
  166.  
  167.   return (CMPtype) result;
  168. }
  169.  
  170. #if WIDTH == 32
  171. /* The decNumber package doesn't provide comparisons for decSingle (32 bits);
  172.    convert to decDouble, use the operation for that, and convert back.  */
  173. static inline CMPtype
  174. d32_compare_op (dfp_binary_func op, _Decimal32 arg_a, _Decimal32 arg_b)
  175. {
  176.   union { _Decimal32 c; decSingle f; } a32, b32;
  177.   decDouble a, b;
  178.  
  179.   a32.c = arg_a;
  180.   b32.c = arg_b;
  181.   decSingleToWider (&a32.f, &a);
  182.   decSingleToWider (&b32.f, &b);
  183.   return dfp_compare_op (op, a, b);  
  184. }
  185. #else
  186. /* decFloat comparisons are supported for decDouble (64 bits) and
  187.    decQuad (128 bits).  The bit patterns for the types are the same.  */
  188. static inline CMPtype
  189. dnn_compare_op (dfp_binary_func op, DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  190. {
  191.   union { DFP_C_TYPE c; decFloat f; } a, b;
  192.  
  193.   a.c = arg_a;
  194.   b.c = arg_b;
  195.   return dfp_compare_op (op, a.f, b.f);  
  196. }
  197. #endif
  198. #if defined(L_conv_sd)
  199. void
  200. __host_to_ieee_32 (_Decimal32 in, decimal32 *out)
  201. {
  202.   memcpy (out, &in, 4);
  203. }
  204.  
  205. void
  206. __ieee_to_host_32 (decimal32 in, _Decimal32 *out)
  207. {
  208.   memcpy (out, &in, 4);
  209. }
  210. #endif /* L_conv_sd */
  211.  
  212. #if defined(L_conv_dd)
  213. void
  214. __host_to_ieee_64 (_Decimal64 in, decimal64 *out)
  215. {
  216.   memcpy (out, &in, 8);
  217. }
  218.  
  219. void
  220. __ieee_to_host_64 (decimal64 in, _Decimal64 *out)
  221. {
  222.   memcpy (out, &in, 8);
  223. }
  224. #endif /* L_conv_dd */
  225.  
  226. #if defined(L_conv_td)
  227. void
  228. __host_to_ieee_128 (_Decimal128 in, decimal128 *out)
  229. {
  230.   memcpy (out, &in, 16);
  231. }
  232.  
  233. void
  234. __ieee_to_host_128 (decimal128 in, _Decimal128 *out)
  235. {
  236.   memcpy (out, &in, 16);
  237. }
  238. #endif /* L_conv_td */
  239.  
  240. #if defined(L_addsub_sd) || defined(L_addsub_dd) || defined(L_addsub_td)
  241. DFP_C_TYPE
  242. DFP_ADD (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  243. {
  244.   return DFP_BINARY_OP (DEC_FLOAT_ADD, arg_a, arg_b);
  245. }
  246.  
  247. DFP_C_TYPE
  248. DFP_SUB (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  249. {
  250.   return DFP_BINARY_OP (DEC_FLOAT_SUBTRACT, arg_a, arg_b);
  251. }
  252. #endif /* L_addsub */
  253.  
  254. #if defined(L_mul_sd) || defined(L_mul_dd) || defined(L_mul_td)
  255. DFP_C_TYPE
  256. DFP_MULTIPLY (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  257. {
  258.   return DFP_BINARY_OP (DEC_FLOAT_MULTIPLY, arg_a, arg_b);
  259. }
  260. #endif /* L_mul */
  261.  
  262. #if defined(L_div_sd) || defined(L_div_dd) || defined(L_div_td)
  263. DFP_C_TYPE
  264. DFP_DIVIDE (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  265. {
  266.   return DFP_BINARY_OP (DEC_FLOAT_DIVIDE, arg_a, arg_b);
  267. }
  268. #endif /* L_div */
  269.  
  270. #if defined (L_eq_sd) || defined (L_eq_dd) || defined (L_eq_td)
  271. CMPtype
  272. DFP_EQ (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  273. {
  274.   CMPtype stat;
  275.   stat = DFP_COMPARE_OP (DEC_FLOAT_COMPARE, arg_a, arg_b);
  276.   /* For EQ return zero for true, nonzero for false.  */
  277.   return stat != 0;
  278. }
  279. #endif /* L_eq */
  280.  
  281. #if defined (L_ne_sd) || defined (L_ne_dd) || defined (L_ne_td)
  282. CMPtype
  283. DFP_NE (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  284. {
  285.   int stat;
  286.   stat = DFP_COMPARE_OP (DEC_FLOAT_COMPARE, arg_a, arg_b);
  287.   /* For NE return zero for true, nonzero for false.  */
  288.   if (__builtin_expect (stat == -2, 0))  /* An operand is NaN.  */
  289.     return 1;
  290.   return stat != 0;
  291. }
  292. #endif /* L_ne */
  293.  
  294. #if defined (L_lt_sd) || defined (L_lt_dd) || defined (L_lt_td)
  295. CMPtype
  296. DFP_LT (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  297. {
  298.   int stat;
  299.   stat = DFP_COMPARE_OP (DEC_FLOAT_COMPARE, arg_a, arg_b);
  300.   /* For LT return -1 (<0) for true, 1 for false.  */
  301.   return (stat == -1) ? -1 : 1;
  302. }
  303. #endif /* L_lt */
  304.  
  305. #if defined (L_gt_sd) || defined (L_gt_dd) || defined (L_gt_td)
  306. CMPtype
  307. DFP_GT (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  308. {
  309.   int stat;
  310.   stat = DFP_COMPARE_OP (DEC_FLOAT_COMPARE, arg_a, arg_b);
  311.   /* For GT return 1 (>0) for true, -1 for false.  */
  312.   return (stat == 1) ? 1 : -1;
  313. }
  314. #endif
  315.  
  316. #if defined (L_le_sd) || defined (L_le_dd) || defined (L_le_td)
  317. CMPtype
  318. DFP_LE (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  319. {
  320.   int stat;
  321.   stat = DFP_COMPARE_OP (DEC_FLOAT_COMPARE, arg_a, arg_b);
  322.   /* For LE return 0 (<= 0) for true, 1 for false.  */
  323.   if (__builtin_expect (stat == -2, 0))  /* An operand is NaN.  */
  324.     return 1;
  325.   return stat == 1;
  326. }
  327. #endif /* L_le */
  328.  
  329. #if defined (L_ge_sd) || defined (L_ge_dd) || defined (L_ge_td)
  330. CMPtype
  331. DFP_GE (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  332. {
  333.   int stat;
  334.   stat = DFP_COMPARE_OP (DEC_FLOAT_COMPARE, arg_a, arg_b);
  335.   /* For GE return 1 (>=0) for true, -1 for false.  */
  336.   if (__builtin_expect (stat == -2, 0))  /* An operand is NaN.  */
  337.     return -1;
  338.   return (stat != -1) ? 1 : -1;
  339. }
  340. #endif /* L_ge */
  341.  
  342. #define BUFMAX 128
  343.  
  344. /* Check for floating point exceptions that are relevant for conversions
  345.    between decimal float values and handle them.  */
  346. static inline void
  347. dfp_conversion_exceptions (const int status)
  348. {
  349.   /* decNumber exception flags we care about here.  */
  350.   int ieee_flags;
  351.   int dec_flags = DEC_IEEE_854_Inexact | DEC_IEEE_854_Invalid_operation
  352.                   | DEC_IEEE_854_Overflow;
  353.   dec_flags &= status;
  354.   ieee_flags = DFP_IEEE_FLAGS (dec_flags);
  355.   if (ieee_flags != 0)
  356.     DFP_HANDLE_EXCEPTIONS (ieee_flags);
  357. }
  358.  
  359. #if defined (L_sd_to_dd)
  360. /* Use decNumber to convert directly from _Decimal32 to _Decimal64.  */
  361. _Decimal64
  362. DFP_TO_DFP (_Decimal32 f_from)
  363. {
  364.   union { _Decimal32 c; decSingle f; } from;
  365.   union { _Decimal64 c; decDouble f; } to;
  366.  
  367.   from.c = f_from;
  368.   to.f = *decSingleToWider (&from.f, &to.f);
  369.   return to.c;
  370. }
  371. #endif
  372.  
  373. #if defined (L_sd_to_td)
  374. /* Use decNumber to convert directly from _Decimal32 to _Decimal128.  */
  375. _Decimal128
  376. DFP_TO_DFP (_Decimal32 f_from)
  377. {
  378.   union { _Decimal32 c; decSingle f; } from;
  379.   union { _Decimal128 c; decQuad f; } to;
  380.   decDouble temp;
  381.  
  382.   from.c = f_from;
  383.   temp = *decSingleToWider (&from.f, &temp);
  384.   to.f = *decDoubleToWider (&temp, &to.f);
  385.   return to.c;
  386. }
  387. #endif
  388.  
  389. #if defined (L_dd_to_td)
  390. /* Use decNumber to convert directly from _Decimal64 to _Decimal128.  */
  391. _Decimal128
  392. DFP_TO_DFP (_Decimal64 f_from)
  393. {
  394.   union { _Decimal64 c; decDouble f; } from;
  395.   union { _Decimal128 c; decQuad f; } to;
  396.  
  397.   from.c = f_from;
  398.   to.f = *decDoubleToWider (&from.f, &to.f);
  399.   return to.c;
  400. }
  401. #endif
  402.  
  403. #if defined (L_dd_to_sd)
  404. /* Use decNumber to convert directly from _Decimal64 to _Decimal32.  */
  405. _Decimal32
  406. DFP_TO_DFP (_Decimal64 f_from)
  407. {
  408.   union { _Decimal32 c; decSingle f; } to;
  409.   union { _Decimal64 c; decDouble f; } from;
  410.   decContext context;
  411.  
  412.   decContextDefault (&context, CONTEXT_INIT);
  413.   DFP_INIT_ROUNDMODE (context.round);
  414.   from.c = f_from;
  415.   to.f = *decSingleFromWider (&to.f, &from.f, &context);
  416.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  417.     dfp_conversion_exceptions (context.status);
  418.   return to.c;
  419. }
  420. #endif
  421.  
  422. #if defined (L_td_to_sd)
  423. /* Use decNumber to convert directly from _Decimal128 to _Decimal32.  */
  424. _Decimal32
  425. DFP_TO_DFP (_Decimal128 f_from)
  426. {
  427.   union { _Decimal32 c; decSingle f; } to;
  428.   union { _Decimal128 c; decQuad f; } from;
  429.   decDouble temp;
  430.   decContext context;
  431.  
  432.   decContextDefault (&context, CONTEXT_INIT);
  433.   DFP_INIT_ROUNDMODE (context.round);
  434.   from.c = f_from;
  435.   temp = *decDoubleFromWider (&temp, &from.f, &context);
  436.   to.f = *decSingleFromWider (&to.f, &temp, &context);
  437.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  438.     dfp_conversion_exceptions (context.status);
  439.   return to.c;
  440. }
  441. #endif
  442.  
  443. #if defined (L_td_to_dd)
  444. /* Use decNumber to convert directly from _Decimal128 to _Decimal64.  */
  445. _Decimal64
  446. DFP_TO_DFP (_Decimal128 f_from)
  447. {
  448.   union { _Decimal64 c; decDouble f; } to;
  449.   union { _Decimal128 c; decQuad f; } from;
  450.   decContext context;
  451.  
  452.   decContextDefault (&context, CONTEXT_INIT);
  453.   DFP_INIT_ROUNDMODE (context.round);
  454.   from.c = f_from;
  455.   to.f = *decDoubleFromWider (&to.f, &from.f, &context);
  456.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  457.     dfp_conversion_exceptions (context.status);
  458.   return to.c;
  459. }
  460. #endif
  461.  
  462. #if defined (L_dd_to_si) || defined (L_td_to_si) \
  463.   || defined (L_dd_to_usi) || defined (L_td_to_usi)
  464. /* Use decNumber to convert directly from decimal float to integer types.  */
  465. INT_TYPE
  466. DFP_TO_INT (DFP_C_TYPE x)
  467. {
  468.   union { DFP_C_TYPE c; decFloat f; } u;
  469.   decContext context;
  470.   INT_TYPE i;
  471.  
  472.   decContextDefault (&context, DEC_INIT_DECIMAL128);
  473.   context.round = DEC_ROUND_DOWN;
  474.   u.c = x;
  475.   i = DEC_FLOAT_TO_INT (&u.f, &context, context.round);
  476.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  477.     dfp_conversion_exceptions (context.status);
  478.   return i;
  479. }
  480. #endif
  481.  
  482. #if defined (L_sd_to_si) || (L_sd_to_usi)
  483. /* Use decNumber to convert directly from decimal float to integer types.  */
  484. INT_TYPE
  485. DFP_TO_INT (_Decimal32 x)
  486. {
  487.   union { _Decimal32 c; decSingle f; } u32;
  488.   decDouble f64;
  489.   decContext context;
  490.   INT_TYPE i;
  491.  
  492.   decContextDefault (&context, DEC_INIT_DECIMAL128);
  493.   context.round = DEC_ROUND_DOWN;
  494.   u32.c = x;
  495.   f64 = *decSingleToWider (&u32.f, &f64);
  496.   i = DEC_FLOAT_TO_INT (&f64, &context, context.round);
  497.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  498.     dfp_conversion_exceptions (context.status);
  499.   return i;
  500. }
  501. #endif
  502.  
  503. #if defined (L_sd_to_di) || defined (L_dd_to_di) || defined (L_td_to_di) \
  504.   || defined (L_sd_to_udi) || defined (L_dd_to_udi) || defined (L_td_to_udi)
  505. /* decNumber doesn't provide support for conversions to 64-bit integer
  506.    types, so do it the hard way.  */
  507. INT_TYPE
  508. DFP_TO_INT (DFP_C_TYPE x)
  509. {
  510.   /* decNumber's decimal* types have the same format as C's _Decimal*
  511.      types, but they have different calling conventions.  */
  512.  
  513.   /* TODO: Decimal float to integer conversions should raise FE_INVALID
  514.      if the result value does not fit into the result type.  */
  515.  
  516.   IEEE_TYPE s;
  517.   char buf[BUFMAX];
  518.   char *pos;
  519.   decNumber qval, n1, n2;
  520.   decContext context;
  521.  
  522.   /* Use a large context to avoid losing precision.  */
  523.   decContextDefault (&context, DEC_INIT_DECIMAL128);
  524.   /* Need non-default rounding mode here.  */
  525.   context.round = DEC_ROUND_DOWN;
  526.  
  527.   HOST_TO_IEEE (x, &s);
  528.   TO_INTERNAL (&s, &n1);
  529.   /* Rescale if the exponent is less than zero.  */
  530.   decNumberToIntegralValue (&n2, &n1, &context);
  531.   /* Get a value to use for the quantize call.  */
  532.   decNumberFromString (&qval, "1.", &context);
  533.   /* Force the exponent to zero.  */
  534.   decNumberQuantize (&n1, &n2, &qval, &context);
  535.   /* Get a string, which at this point will not include an exponent.  */
  536.   decNumberToString (&n1, buf);
  537.   /* Ignore the fractional part.  */
  538.   pos = strchr (buf, '.');
  539.   if (pos)
  540.     *pos = 0;
  541.   /* Use a C library function to convert to the integral type.  */
  542.   return STR_TO_INT (buf, NULL, 10);
  543. }
  544. #endif
  545.  
  546. #if defined (L_si_to_dd) || defined (L_si_to_td) \
  547.   || defined (L_usi_to_dd) || defined (L_usi_to_td)
  548. /* Use decNumber to convert directly from integer to decimal float types.  */
  549. DFP_C_TYPE
  550. INT_TO_DFP (INT_TYPE i)
  551. {
  552.   union { DFP_C_TYPE c; decFloat f; } u;
  553.  
  554.   u.f = *DEC_FLOAT_FROM_INT (&u.f, i);
  555.   return u.c;
  556. }
  557. #endif
  558.  
  559. #if defined (L_si_to_sd) || defined (L_usi_to_sd)
  560. _Decimal32
  561. /* Use decNumber to convert directly from integer to decimal float types.  */
  562. INT_TO_DFP (INT_TYPE i)
  563. {
  564.   union { _Decimal32 c; decSingle f; } u32;
  565.   decDouble f64;
  566.   decContext context;
  567.  
  568.   decContextDefault (&context, DEC_INIT_DECIMAL128);
  569.   f64 = *DEC_FLOAT_FROM_INT (&f64, i);
  570.   u32.f = *decSingleFromWider (&u32.f, &f64, &context);
  571.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  572.     dfp_conversion_exceptions (context.status);
  573.   return u32.c;
  574. }
  575. #endif
  576.  
  577. #if defined (L_di_to_sd) || defined (L_di_to_dd) || defined (L_di_to_td) \
  578.   || defined (L_udi_to_sd) || defined (L_udi_to_dd) || defined (L_udi_to_td)
  579. /* decNumber doesn't provide support for conversions from 64-bit integer
  580.    types, so do it the hard way.  */
  581. DFP_C_TYPE
  582. INT_TO_DFP (INT_TYPE i)
  583. {
  584.   DFP_C_TYPE f;
  585.   IEEE_TYPE s;
  586.   char buf[BUFMAX];
  587.   decContext context;
  588.  
  589.   decContextDefault (&context, CONTEXT_INIT);
  590.   DFP_INIT_ROUNDMODE (context.round);
  591.  
  592.   /* Use a C library function to get a floating point string.  */
  593.   sprintf (buf, INT_FMT ".", CAST_FOR_FMT(i));
  594.   /* Convert from the floating point string to a decimal* type.  */
  595.   FROM_STRING (&s, buf, &context);
  596.   IEEE_TO_HOST (s, &f);
  597.  
  598.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  599.     dfp_conversion_exceptions (context.status);
  600.  
  601.   return f;
  602. }
  603. #endif
  604.  
  605. #if defined (L_sd_to_sf) || defined (L_dd_to_sf) || defined (L_td_to_sf) \
  606.  || defined (L_sd_to_df) || defined (L_dd_to_df) || defined (L_td_to_df) \
  607.  || ((defined (L_sd_to_xf) || defined (L_dd_to_xf) || defined (L_td_to_xf)) \
  608.      && LONG_DOUBLE_HAS_XF_MODE) \
  609.  || ((defined (L_sd_to_tf) || defined (L_dd_to_tf) || defined (L_td_to_tf)) \
  610.      && LONG_DOUBLE_HAS_TF_MODE)
  611. BFP_TYPE
  612. DFP_TO_BFP (DFP_C_TYPE f)
  613. {
  614.   IEEE_TYPE s;
  615.   char buf[BUFMAX];
  616.  
  617.   HOST_TO_IEEE (f, &s);
  618.   /* Write the value to a string.  */
  619.   TO_STRING (&s, buf);
  620.   /* Read it as the binary floating point type and return that.  */
  621.   return STR_TO_BFP (buf, NULL);
  622. }
  623. #endif
  624.                                                                                
  625. #if defined (L_sf_to_sd) || defined (L_sf_to_dd) || defined (L_sf_to_td) \
  626.  || defined (L_df_to_sd) || defined (L_df_to_dd) || defined (L_df_to_td) \
  627.  || ((defined (L_xf_to_sd) || defined (L_xf_to_dd) || defined (L_xf_to_td)) \
  628.      && LONG_DOUBLE_HAS_XF_MODE) \
  629.  || ((defined (L_tf_to_sd) || defined (L_tf_to_dd) || defined (L_tf_to_td)) \
  630.      && LONG_DOUBLE_HAS_TF_MODE)
  631. DFP_C_TYPE
  632. BFP_TO_DFP (BFP_TYPE x)
  633. {
  634.   DFP_C_TYPE f;
  635.   IEEE_TYPE s;
  636.   char buf[BUFMAX];
  637.   decContext context;
  638.  
  639.   decContextDefault (&context, CONTEXT_INIT);
  640.   DFP_INIT_ROUNDMODE (context.round);
  641.  
  642.   /* Use a C library function to write the floating point value to a string.  */
  643.   sprintf (buf, BFP_FMT, (BFP_VIA_TYPE) x);
  644.  
  645.   /* Convert from the floating point string to a decimal* type.  */
  646.   FROM_STRING (&s, buf, &context);
  647.   IEEE_TO_HOST (s, &f);
  648.  
  649.   if (DFP_EXCEPTIONS_ENABLED && context.status != 0)
  650.     {
  651.       /* decNumber exception flags we care about here.  */
  652.       int ieee_flags;
  653.       int dec_flags = DEC_IEEE_854_Inexact | DEC_IEEE_854_Invalid_operation
  654.                       | DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow;
  655.       dec_flags &= context.status;
  656.       ieee_flags = DFP_IEEE_FLAGS (dec_flags);
  657.       if (ieee_flags != 0)
  658.         DFP_HANDLE_EXCEPTIONS (ieee_flags);
  659.     }
  660.  
  661.   return f;
  662. }
  663. #endif
  664.  
  665. #if defined (L_unord_sd) || defined (L_unord_dd) || defined (L_unord_td)
  666. CMPtype
  667. DFP_UNORD (DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
  668. {
  669.   decNumber arg1, arg2;
  670.   IEEE_TYPE a, b;
  671.  
  672.   HOST_TO_IEEE (arg_a, &a);
  673.   HOST_TO_IEEE (arg_b, &b);
  674.   TO_INTERNAL (&a, &arg1);
  675.   TO_INTERNAL (&b, &arg2);
  676.   return (decNumberIsNaN (&arg1) || decNumberIsNaN (&arg2));
  677. }
  678. #endif /* L_unord_sd || L_unord_dd || L_unord_td */
  679.