1/* Copyright (C) 2007-2015 Free Software Foundation, Inc.
2
3This file is part of GCC.
4
5GCC is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free
7Software Foundation; either version 3, or (at your option) any later
8version.
9
10GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or
12FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13for more details.
14
15Under Section 7 of GPL version 3, you are granted additional
16permissions described in the GCC Runtime Library Exception, version
173.1, as published by the Free Software Foundation.
18
19You should have received a copy of the GNU General Public License and
20a copy of the GCC Runtime Library Exception along with this program;
21see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
22<http://www.gnu.org/licenses/>.  */
23
24#include "bid_internal.h"
25
26#define MAX_FORMAT_DIGITS     16
27#define DECIMAL_EXPONENT_BIAS 398
28
29#if DECIMAL_CALL_BY_REFERENCE
30
31void
32bid64_logb (int * pres, UINT64 * px
33	    _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
34  UINT64 x;
35#else
36
37int
38bid64_logb (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
39#endif
40  UINT64 sign_x, coefficient_x;
41  int_double dx;
42  int exponent_x, bin_expon_cx, digits;
43
44#if DECIMAL_CALL_BY_REFERENCE
45  x = *px;
46#endif
47  // unpack arguments, check for NaN or Infinity
48  if (!unpack_BID64 (&sign_x, &exponent_x, &coefficient_x, x)) {
49    // x is Inf. or NaN
50#ifdef SET_STATUS_FLAGS
51      __set_status_flags (pfpsf, INVALID_EXCEPTION);
52#endif
53      BID_RETURN (0x80000000);
54  }
55  // find number of digits in coefficient
56  if (coefficient_x >= 1000000000000000ull) {
57    digits = 16;
58  } else {
59    dx.d = (double)coefficient_x;   // exact conversion;
60    bin_expon_cx = (int)(dx.i >> 52) - 1023;
61    digits = estimate_decimal_digits[bin_expon_cx];
62    if (coefficient_x >= power10_table_128[digits].w[0])
63      digits++;
64  }
65  exponent_x = exponent_x - DECIMAL_EXPONENT_BIAS + digits - 1;
66
67  BID_RETURN (exponent_x);
68}
69