decimal32.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (C) 2007-2015 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 decimal32FromString __dpd32FromString
  19. #define decimal32ToString __dpd32ToString
  20. #define decimal32ToEngString __dpd32ToEngString
  21. #define decimal32FromNumber __dpd32FromNumber
  22. #define decimal32ToNumber __dpd32ToNumber
  23. #include "dpd/decimal32.c"
  24. #undef decimal32FromString
  25. #undef decimal32ToString
  26. #undef decimal32ToEngString
  27. #undef decimal32FromNumber
  28. #undef decimal32ToNumber
  29. #include "bid-dpd.h"
  30. #ifdef IN_LIBGCC2
  31. #define decimal32FromString __decimal32FromString
  32. #define decimal32ToString __decimal32ToString
  33. #define decimal32ToEngString __decimal32ToEngString
  34. #define decimal32FromNumber __decimal32FromNumber
  35. #define decimal32ToNumber __decimal32ToNumber
  36. #endif
  37. decimal32 *decimal32FromString (decimal32 *, const char *, decContext *);
  38. char *decimal32ToString (const decimal32 *, char *);
  39. char *decimal32ToEngString (const decimal32 *, char *);
  40. decimal32 *decimal32FromNumber (decimal32 *, const decNumber *, decContext *);
  41. decNumber *decimal32ToNumber (const decimal32 *, decNumber *);
  42. void __host_to_ieee_32 (_Decimal32 in, decimal32 *out);
  43. void __ieee_to_host_32 (decimal32 in, _Decimal32 *out);
  44. decimal32 *
  45. decimal32FromNumber (decimal32 *d32, const decNumber *dn,
  46. decContext *set)
  47. {
  48. /* decimal32 and _Decimal32 are different types. */
  49. union
  50. {
  51. _Decimal32 _Dec;
  52. decimal32 dec;
  53. } u;
  54. __dpd32FromNumber (d32, dn, set);
  55. /* __dpd32FromNumber returns in big endian. But _dpd_to_bid32 takes
  56. host endian. */
  57. __ieee_to_host_32 (*d32, &u._Dec);
  58. /* Convert DPD to BID. */
  59. _dpd_to_bid32 (&u._Dec, &u._Dec);
  60. /* dfp.c is in bid endian. */
  61. __host_to_ieee_32 (u._Dec, &u.dec);
  62. /* d32 is returned as a pointer to _Decimal32 here. */
  63. *d32 = u.dec;
  64. return d32;
  65. }
  66. decNumber *
  67. decimal32ToNumber (const decimal32 *bid32, decNumber *dn)
  68. {
  69. /* decimal32 and _Decimal32 are different types. */
  70. union
  71. {
  72. _Decimal32 _Dec;
  73. decimal32 dec;
  74. } u;
  75. /* bid32 is a pointer to _Decimal32 in bid endian. But _bid_to_dpd32
  76. takes host endian. */
  77. __ieee_to_host_32 (*bid32, &u._Dec);
  78. /* Convert BID to DPD. */
  79. _bid_to_dpd32 (&u._Dec, &u._Dec);
  80. /* __dpd32ToNumber is in bid endian. */
  81. __host_to_ieee_32 (u._Dec, &u.dec);
  82. return __dpd32ToNumber (&u.dec, dn);
  83. }
  84. char *
  85. decimal32ToString (const decimal32 *d32, char *string)
  86. {
  87. decNumber dn; /* work */
  88. decimal32ToNumber (d32, &dn);
  89. decNumberToString (&dn, string);
  90. return string;
  91. }
  92. char *
  93. decimal32ToEngString (const decimal32 *d32, char *string)
  94. {
  95. decNumber dn; /* work */
  96. decimal32ToNumber (d32, &dn);
  97. decNumberToEngString (&dn, string);
  98. return string;
  99. }
  100. decimal32 *
  101. decimal32FromString (decimal32 *result, const char *string,
  102. decContext *set)
  103. {
  104. decContext dc; /* work */
  105. decNumber dn; /* .. */
  106. decContextDefault (&dc, DEC_INIT_DECIMAL32); /* no traps, please */
  107. dc.round = set->round; /* use supplied rounding */
  108. decNumberFromString (&dn, string, &dc); /* will round if needed */
  109. decimal32FromNumber (result, &dn, &dc);
  110. if (dc.status != 0)
  111. { /* something happened */
  112. decContextSetStatus (set, dc.status); /* .. pass it on */
  113. }
  114. return result;
  115. }