bid128_scalb.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (C) 2007-2022 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 BID_128RES
  19. #include "bid_internal.h"
  20. #define DECIMAL_EXPONENT_BIAS_128 6176
  21. #define MAX_DECIMAL_EXPONENT_128 12287
  22. BID128_FUNCTION_ARG128_ARGTYPE2 (bid128_scalb, x, int, n)
  23. UINT128 CX, CX2, CX8, res;
  24. SINT64 exp64;
  25. UINT64 sign_x;
  26. int exponent_x, rmode;
  27. // unpack arguments, check for NaN or Infinity
  28. if (!unpack_BID128_value (&sign_x, &exponent_x, &CX, x)) {
  29. // x is Inf. or NaN or 0
  30. #ifdef SET_STATUS_FLAGS
  31. if ((x.w[1] & SNAN_MASK64) == SNAN_MASK64) // y is sNaN
  32. __set_status_flags (pfpsf, INVALID_EXCEPTION);
  33. #endif
  34. res.w[1] = CX.w[1] & QUIET_MASK64;
  35. res.w[0] = CX.w[0];
  36. if (!CX.w[1]) {
  37. exp64 = (SINT64) exponent_x + (SINT64) n;
  38. if(exp64<0) exp64=0;
  39. if(exp64>MAX_DECIMAL_EXPONENT_128) exp64=MAX_DECIMAL_EXPONENT_128;
  40. exponent_x = exp64;
  41. get_BID128_very_fast (&res, sign_x, exponent_x, CX);
  42. }
  43. BID_RETURN (res);
  44. }
  45. exp64 = (SINT64) exponent_x + (SINT64) n;
  46. exponent_x = exp64;
  47. if ((UINT32) exponent_x <= MAX_DECIMAL_EXPONENT_128) {
  48. get_BID128_very_fast (&res, sign_x, exponent_x, CX);
  49. BID_RETURN (res);
  50. }
  51. // check for overflow
  52. if (exp64 > MAX_DECIMAL_EXPONENT_128) {
  53. if (CX.w[1] < 0x314dc6448d93ull) {
  54. // try to normalize coefficient
  55. do {
  56. CX8.w[1] = (CX.w[1] << 3) | (CX.w[0] >> 61);
  57. CX8.w[0] = CX.w[0] << 3;
  58. CX2.w[1] = (CX.w[1] << 1) | (CX.w[0] >> 63);
  59. CX2.w[0] = CX.w[0] << 1;
  60. __add_128_128 (CX, CX2, CX8);
  61. exponent_x--;
  62. exp64--;
  63. }
  64. while (CX.w[1] < 0x314dc6448d93ull
  65. && exp64 > MAX_DECIMAL_EXPONENT_128);
  66. }
  67. if (exp64 <= MAX_DECIMAL_EXPONENT_128) {
  68. get_BID128_very_fast (&res, sign_x, exponent_x, CX);
  69. BID_RETURN (res);
  70. } else
  71. exponent_x = 0x7fffffff; // overflow
  72. }
  73. // exponent < 0
  74. // the BID pack routine will round the coefficient
  75. rmode = rnd_mode;
  76. get_BID128 (&res, sign_x, exponent_x, CX, (unsigned int *) &rmode,
  77. pfpsf);
  78. BID_RETURN (res);
  79. }