decRound.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Internal testing support for rounding for decimal float.
  2. Copyright (C) 2005-2013 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "dconfig.h"
  20. #include "decContext.h"
  21. #include "decRound.h"
  22. /* Internal, non-documented functions for testing libgcc functions.
  23. This support is not sufficient for application use. */
  24. #define FE_DEC_DOWNWARD 0
  25. #define FE_DEC_TONEAREST 1
  26. #define FE_DEC_TONEARESTFROMZERO 2
  27. #define FE_DEC_TOWARDZERO 3
  28. #define FE_DEC_UPWARD 4
  29. #define FE_DEC_MAX 5
  30. static enum rounding __dfp_rounding_mode = DEC_ROUND_HALF_EVEN;
  31. /* Set the decNumber rounding mode from the FE_DEC_* value in MODE. */
  32. void
  33. __dfp_set_round (int mode)
  34. {
  35. switch (mode)
  36. {
  37. case FE_DEC_DOWNWARD:
  38. __dfp_rounding_mode = DEC_ROUND_FLOOR; break;
  39. case FE_DEC_TONEAREST:
  40. __dfp_rounding_mode = DEC_ROUND_HALF_EVEN; break;
  41. case FE_DEC_TONEARESTFROMZERO:
  42. __dfp_rounding_mode = DEC_ROUND_HALF_UP; break;
  43. case FE_DEC_TOWARDZERO:
  44. __dfp_rounding_mode = DEC_ROUND_DOWN; break;
  45. case FE_DEC_UPWARD:
  46. __dfp_rounding_mode = DEC_ROUND_CEILING; break;
  47. default:
  48. /* We can't use assert in libgcc, so just return the default mode. */
  49. __dfp_rounding_mode = DEC_ROUND_HALF_EVEN; break;
  50. }
  51. }
  52. /* Return the decNumber rounding mode as an FE_DEC_* value. */
  53. int
  54. __dfp_get_round (void)
  55. {
  56. int mode;
  57. switch (__dfp_rounding_mode)
  58. {
  59. case DEC_ROUND_FLOOR:
  60. mode = FE_DEC_DOWNWARD; break;
  61. case DEC_ROUND_HALF_EVEN:
  62. mode = FE_DEC_TONEAREST; break;
  63. case DEC_ROUND_HALF_UP:
  64. mode = FE_DEC_TONEARESTFROMZERO; break;
  65. case DEC_ROUND_DOWN:
  66. mode = FE_DEC_TOWARDZERO; break;
  67. case DEC_ROUND_CEILING:
  68. mode = FE_DEC_UPWARD; break;
  69. default:
  70. /* We shouldn't get here, but can't use assert in libgcc. */
  71. mode = -1;
  72. }
  73. return mode;
  74. }
  75. /* Return the decNumber version of the current rounding mode. */
  76. enum rounding
  77. __decGetRound (void)
  78. {
  79. return __dfp_rounding_mode;
  80. }