OSDN Git Service

Merged with libbbid branch at revision 126349.
[pf3gnuchains/gcc-fork.git] / libgcc / config / libbid / bid128_logb.c
1 /* Copyright (C) 2007  Free Software Foundation, Inc.
2
3 This file is part of GCC.
4
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version.
9
10 In addition to the permissions in the GNU General Public License, the
11 Free Software Foundation gives you unlimited permission to link the
12 compiled version of this file into combinations with other programs,
13 and to distribute those combinations without any restriction coming
14 from the use of this file.  (The General Public License restrictions
15 do apply in other respects; for example, they cover modification of
16 the file, and distribution when not linked into a combine
17 executable.)
18
19 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
20 WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with GCC; see the file COPYING.  If not, write to the Free
26 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
27 02110-1301, USA.  */
28
29 #define BID_128RES
30 #include "bid_internal.h"
31
32 BID128_FUNCTION_ARG1(__bid128_logb, x)
33
34   UINT128 CX, L, res;
35   UINT64 sign_x, sign_e, logb_sign;
36   SINT64 D;
37   int_float f64, fx;
38   int exponent_x, bin_expon_cx, digits;
39
40   if (!unpack_BID128_value (&sign_x, &exponent_x, &CX, x)) {
41     res.w[1] = x.w[1];
42     res.w[0] = x.w[0];
43     // x is Infinity?
44     if ((x.w[1] & 0x7800000000000000ull) == 0x7800000000000000ull) {
45       if ((x.w[1] & 0x7c00000000000000ull) != 0x7c00000000000000ull)
46         // +/-Inf, return Inf
47         res.w[1] = 0x7800000000000000ull;
48       BID_RETURN (res);
49     }
50     // x is 0 otherwise
51
52 #ifdef SET_STATUS_FLAGS
53     // set status flags
54     __set_status_flags (pfpsf, ZERO_DIVIDE_EXCEPTION);
55 #endif
56     res.w[1] = 0xf800000000000000ull;
57     res.w[0] = 0;
58     BID_RETURN (res);
59   }
60   // find number of digits in coefficient
61   // 2^64
62   f64.i = 0x5f800000;
63   // fx ~ CX
64   fx.d = (float) CX.w[1] * f64.d + (float) CX.w[0];
65   bin_expon_cx = ((fx.i >> 23) & 0xff) - 0x7f;
66   digits = __bid_estimate_decimal_digits[bin_expon_cx];
67   // scale = 38-__bid_estimate_decimal_digits[bin_expon_cx];
68   D = CX.w[1] - __bid_power10_index_binexp_128[bin_expon_cx].w[1];
69   if (D > 0
70       || (!D && CX.w[0] >= __bid_power10_index_binexp_128[bin_expon_cx].w[0]))
71     digits++;
72
73   exponent_x = exponent_x - DECIMAL_EXPONENT_BIAS_128 - 1 + digits;
74
75   // extract sign and absolute value from exponent_x
76   sign_e = ((SINT32) exponent_x) >> 31;
77   exponent_x = (exponent_x + sign_e) ^ sign_e;
78
79   L.w[0] = exponent_x;
80   L.w[1] = 0;
81   logb_sign = sign_e << 63;
82
83   get_BID128_very_fast (&res, logb_sign, DECIMAL_EXPONENT_BIAS_128, L);
84   BID_RETURN (res);
85
86 }