bid64_logb.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "bid_internal.h"
  19. #define MAX_FORMAT_DIGITS 16
  20. #define DECIMAL_EXPONENT_BIAS 398
  21. #if DECIMAL_CALL_BY_REFERENCE
  22. void
  23. bid64_logb (int * pres, UINT64 * px
  24. _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  25. UINT64 x;
  26. #else
  27. int
  28. bid64_logb (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
  29. #endif
  30. UINT64 sign_x, coefficient_x;
  31. int_double dx;
  32. int exponent_x, bin_expon_cx, digits;
  33. #if DECIMAL_CALL_BY_REFERENCE
  34. x = *px;
  35. #endif
  36. // unpack arguments, check for NaN or Infinity
  37. if (!unpack_BID64 (&sign_x, &exponent_x, &coefficient_x, x)) {
  38. // x is Inf. or NaN
  39. #ifdef SET_STATUS_FLAGS
  40. __set_status_flags (pfpsf, INVALID_EXCEPTION);
  41. #endif
  42. BID_RETURN (0x80000000);
  43. }
  44. // find number of digits in coefficient
  45. if (coefficient_x >= 1000000000000000ull) {
  46. digits = 16;
  47. } else {
  48. dx.d = (double)coefficient_x; // exact conversion;
  49. bin_expon_cx = (int)(dx.i >> 52) - 1023;
  50. digits = estimate_decimal_digits[bin_expon_cx];
  51. if (coefficient_x >= power10_table_128[digits].w[0])
  52. digits++;
  53. }
  54. exponent_x = exponent_x - DECIMAL_EXPONENT_BIAS + digits - 1;
  55. BID_RETURN (exponent_x);
  56. }