OSDN Git Service

libgcc/
[pf3gnuchains/gcc-fork.git] / libgcc / config / libbid / bid128_scalb.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 #define DECIMAL_EXPONENT_BIAS_128 6176
33 #define MAX_DECIMAL_EXPONENT_128  12287
34
35
36
37 BID128_FUNCTION_ARG128_ARGTYPE2 (bid128_scalb, x, int, n)
38
39      UINT128 CX, CX2, CX8, res;
40      SINT64 exp64;
41      UINT64 sign_x;
42      int exponent_x, rmode;
43
44   // unpack arguments, check for NaN or Infinity
45 if (!unpack_BID128_value (&sign_x, &exponent_x, &CX, x)) {
46     // x is Inf. or NaN or 0
47 #ifdef SET_STATUS_FLAGS
48 if ((x.w[1] & SNAN_MASK64) == SNAN_MASK64)      // y is sNaN
49   __set_status_flags (pfpsf, INVALID_EXCEPTION);
50 #endif
51 res.w[1] = CX.w[1] & QUIET_MASK64;
52 res.w[0] = CX.w[0];
53 if (!CX.w[1]) {
54        exp64 = (SINT64) exponent_x + (SINT64) n;
55            if(exp64<0) exp64=0;
56            if(exp64>MAX_DECIMAL_EXPONENT_128) exp64=MAX_DECIMAL_EXPONENT_128;
57        exponent_x = exp64;
58   get_BID128_very_fast (&res, sign_x, exponent_x, CX);
59 }
60 BID_RETURN (res);
61 }
62
63 exp64 = (SINT64) exponent_x + (SINT64) n;
64 exponent_x = exp64;
65
66 if ((UINT32) exponent_x <= MAX_DECIMAL_EXPONENT_128) {
67   get_BID128_very_fast (&res, sign_x, exponent_x, CX);
68   BID_RETURN (res);
69 }
70   // check for overflow
71 if (exp64 > MAX_DECIMAL_EXPONENT_128) {
72   if (CX.w[1] < 0x314dc6448d93ull) {
73     // try to normalize coefficient
74     do {
75       CX8.w[1] = (CX.w[1] << 3) | (CX.w[0] >> 61);
76       CX8.w[0] = CX.w[0] << 3;
77       CX2.w[1] = (CX.w[1] << 1) | (CX.w[0] >> 63);
78       CX2.w[0] = CX.w[0] << 1;
79       __add_128_128 (CX, CX2, CX8);
80
81       exponent_x--;
82       exp64--;
83     }
84     while (CX.w[1] < 0x314dc6448d93ull
85            && exp64 > MAX_DECIMAL_EXPONENT_128);
86
87   }
88   if (exp64 <= MAX_DECIMAL_EXPONENT_128) {
89     get_BID128_very_fast (&res, sign_x, exponent_x, CX);
90     BID_RETURN (res);
91   } else
92     exponent_x = 0x7fffffff;    // overflow
93 }
94   // exponent < 0
95   // the BID pack routine will round the coefficient
96 rmode = rnd_mode;
97 get_BID128 (&res, sign_x, exponent_x, CX, (unsigned int *) &rmode,
98             pfpsf);
99 BID_RETURN (res);
100
101 }