decimal128.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (C) 2007-2013 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 3, or (at your option) any later
  6. version.
  7. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10. for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #define decimal128FromString __dpd128FromString
  19. #define decimal128ToString __dpd128ToString
  20. #define decimal128ToEngString __dpd128ToEngString
  21. #define decimal128FromNumber __dpd128FromNumber
  22. #define decimal128ToNumber __dpd128ToNumber
  23. #include "dpd/decimal128.c"
  24. #undef decimal128FromString
  25. #undef decimal128ToString
  26. #undef decimal128ToEngString
  27. #undef decimal128FromNumber
  28. #undef decimal128ToNumber
  29. #include "bid-dpd.h"
  30. #ifdef IN_LIBGCC2
  31. #define decimal128FromString __decimal128FromString
  32. #define decimal128ToString __decimal128ToString
  33. #define decimal128ToEngString __decimal128ToEngString
  34. #define decimal128FromNumber __decimal128FromNumber
  35. #define decimal128ToNumber __decimal128ToNumber
  36. #endif
  37. decimal128 *decimal128FromString (decimal128 *, const char *, decContext *);
  38. char *decimal128ToString (const decimal128 *, char *);
  39. char *decimal128ToEngString (const decimal128 *, char *);
  40. decimal128 *decimal128FromNumber (decimal128 *, const decNumber *, decContext *);
  41. decNumber *decimal128ToNumber (const decimal128 *, decNumber *);
  42. void __host_to_ieee_128 (_Decimal128 in, decimal128 *out);
  43. void __ieee_to_host_128 (decimal128 in, _Decimal128 *out);
  44. decimal128 *
  45. decimal128FromNumber (decimal128 *d128, const decNumber *dn,
  46. decContext *set)
  47. {
  48. /* decimal128 and _Decimal128 are different types. */
  49. union
  50. {
  51. _Decimal128 _Dec;
  52. decimal128 dec;
  53. } u;
  54. __dpd128FromNumber (d128, dn, set);
  55. /* __dpd128FromNumber returns in big endian. But _dpd_to_bid128 takes
  56. host endian. */
  57. __ieee_to_host_128 (*d128, &u._Dec);
  58. /* Convert DPD to BID. */
  59. _dpd_to_bid128 (&u._Dec, &u._Dec);
  60. /* dfp.c is in bid endian. */
  61. __host_to_ieee_128 (u._Dec, &u.dec);
  62. /* d128 is returned as a pointer to _Decimal128 here. */
  63. *d128 = u.dec;
  64. return d128;
  65. }
  66. decNumber *
  67. decimal128ToNumber (const decimal128 *bid128, decNumber *dn)
  68. {
  69. /* decimal128 and _Decimal128 are different types. */
  70. union
  71. {
  72. _Decimal128 _Dec;
  73. decimal128 dec;
  74. } u;
  75. /* bid128 is a pointer to _Decimal128 in bid endian. But _bid_to_dpd128
  76. takes host endian. */
  77. __ieee_to_host_128 (*bid128, &u._Dec);
  78. /* Convert BID to DPD. */
  79. _bid_to_dpd128 (&u._Dec, &u._Dec);
  80. /* __dpd128ToNumber is in bid endian. */
  81. __host_to_ieee_128 (u._Dec, &u.dec);
  82. return __dpd128ToNumber (&u.dec, dn);
  83. }
  84. char *
  85. decimal128ToString (const decimal128 *d128, char *string)
  86. {
  87. decNumber dn; /* work */
  88. decimal128ToNumber (d128, &dn);
  89. decNumberToString (&dn, string);
  90. return string;
  91. }
  92. char *
  93. decimal128ToEngString (const decimal128 *d128, char *string)
  94. {
  95. decNumber dn; /* work */
  96. decimal128ToNumber (d128, &dn);
  97. decNumberToEngString (&dn, string);
  98. return string;
  99. }
  100. decimal128 *
  101. decimal128FromString (decimal128 *result, const char *string,
  102. decContext *set)
  103. {
  104. decContext dc; /* work */
  105. decNumber dn; /* .. */
  106. decContextDefault (&dc, DEC_INIT_DECIMAL128); /* no traps, please */
  107. dc.round = set->round; /* use supplied rounding */
  108. decNumberFromString (&dn, string, &dc); /* will round if needed */
  109. decimal128FromNumber (result, &dn, &dc);
  110. if (dc.status != 0)
  111. { /* something happened */
  112. decContextSetStatus (set, dc.status); /* .. pass it on */
  113. }
  114. return result;
  115. }