decimal64.c 4.0 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 decimal64FromString __dpd64FromString
  19. #define decimal64ToString __dpd64ToString
  20. #define decimal64ToEngString __dpd64ToEngString
  21. #define decimal64FromNumber __dpd64FromNumber
  22. #define decimal64ToNumber __dpd64ToNumber
  23. #include "dpd/decimal64.c"
  24. #undef decimal64FromString
  25. #undef decimal64ToString
  26. #undef decimal64ToEngString
  27. #undef decimal64FromNumber
  28. #undef decimal64ToNumber
  29. #include "bid-dpd.h"
  30. #ifdef IN_LIBGCC2
  31. #define decimal64FromString __decimal64FromString
  32. #define decimal64ToString __decimal64ToString
  33. #define decimal64ToEngString __decimal64ToEngString
  34. #define decimal64FromNumber __decimal64FromNumber
  35. #define decimal64ToNumber __decimal64ToNumber
  36. #endif
  37. decimal64 *decimal64FromString (decimal64 *, const char *, decContext *);
  38. char *decimal64ToString (const decimal64 *, char *);
  39. char *decimal64ToEngString (const decimal64 *, char *);
  40. decimal64 *decimal64FromNumber (decimal64 *, const decNumber *, decContext *);
  41. decNumber *decimal64ToNumber (const decimal64 *, decNumber *);
  42. void __host_to_ieee_64 (_Decimal64 in, decimal64 *out);
  43. void __ieee_to_host_64 (decimal64 in, _Decimal64 *out);
  44. decimal64 *
  45. decimal64FromNumber (decimal64 *d64, const decNumber *dn,
  46. decContext *set)
  47. {
  48. /* decimal64 and _Decimal64 are different types. */
  49. union
  50. {
  51. _Decimal64 _Dec;
  52. decimal64 dec;
  53. } u;
  54. __dpd64FromNumber (d64, dn, set);
  55. /* __dpd64FromNumber returns in big endian. But _dpd_to_bid64 takes
  56. host endian. */
  57. __ieee_to_host_64 (*d64, &u._Dec);
  58. /* Convert DPD to BID. */
  59. _dpd_to_bid64 (&u._Dec, &u._Dec);
  60. /* dfp.c is in bid endian. */
  61. __host_to_ieee_64 (u._Dec, &u.dec);
  62. /* d64 is returned as a pointer to _Decimal64 here. */
  63. *d64 = u.dec;
  64. return d64;
  65. }
  66. decNumber *
  67. decimal64ToNumber (const decimal64 *bid64, decNumber *dn)
  68. {
  69. /* decimal64 and _Decimal64 are different types. */
  70. union
  71. {
  72. _Decimal64 _Dec;
  73. decimal64 dec;
  74. } u;
  75. /* bid64 is a pointer to _Decimal64 in bid endian. But _bid_to_dpd64
  76. takes host endian. */
  77. __ieee_to_host_64 (*bid64, &u._Dec);
  78. /* Convert BID to DPD. */
  79. _bid_to_dpd64 (&u._Dec, &u._Dec);
  80. /* __dpd64ToNumber is in bid endian. */
  81. __host_to_ieee_64 (u._Dec, &u.dec);
  82. return __dpd64ToNumber (&u.dec, dn);
  83. }
  84. char *
  85. decimal64ToString (const decimal64 *d64, char *string)
  86. {
  87. decNumber dn; /* work */
  88. decimal64ToNumber (d64, &dn);
  89. decNumberToString (&dn, string);
  90. return string;
  91. }
  92. char *
  93. decimal64ToEngString (const decimal64 *d64, char *string)
  94. {
  95. decNumber dn; /* work */
  96. decimal64ToNumber (d64, &dn);
  97. decNumberToEngString (&dn, string);
  98. return string;
  99. }
  100. decimal64 *
  101. decimal64FromString (decimal64 *result, const char *string,
  102. decContext *set)
  103. {
  104. decContext dc; /* work */
  105. decNumber dn; /* .. */
  106. decContextDefault (&dc, DEC_INIT_DECIMAL64); /* no traps, please */
  107. dc.round = set->round; /* use supplied rounding */
  108. decNumberFromString (&dn, string, &dc); /* will round if needed */
  109. decimal64FromNumber (result, &dn, &dc);
  110. if (dc.status != 0)
  111. { /* something happened */
  112. decContextSetStatus (set, dc.status); /* .. pass it on */
  113. }
  114. return result;
  115. }