re PR tree-optimization/33565 (spurious warning: assuming signed overflow does not...
[gcc.git] / libgcc / config / libbid / bid64_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 #include "bid_internal.h"
30
31 #define MAX_FORMAT_DIGITS 16
32 #define DECIMAL_EXPONENT_BIAS 398
33 #define MAX_DECIMAL_EXPONENT 767
34
35 #if DECIMAL_CALL_BY_REFERENCE
36
37 void
38 __bid64_scalb (UINT64 * pres, UINT64 * px,
39 int *pn _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
40 _EXC_INFO_PARAM) {
41 UINT64 x;
42 int n;
43 #else
44
45 UINT64
46 __bid64_scalb (UINT64 x,
47 int n _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
48 _EXC_INFO_PARAM) {
49 #endif
50 UINT64 sign_x, coefficient_x, res;
51 int exponent_x, rmode;
52
53 #if DECIMAL_CALL_BY_REFERENCE
54 #if !DECIMAL_GLOBAL_ROUNDING
55 _IDEC_round rnd_mode = *prnd_mode;
56 #endif
57 x = *px;
58 n = *pn;
59 #endif
60
61 // unpack arguments, check for NaN or Infinity
62 if (!unpack_BID64 (&sign_x, &exponent_x, &coefficient_x, x)) {
63 // x is Inf. or NaN or 0
64 res = x;
65 BID_RETURN (res);
66 }
67
68 exponent_x += n;
69
70 if ((UINT32) exponent_x <= MAX_DECIMAL_EXPONENT) {
71 res = very_fast_get_BID64 (sign_x, exponent_x, coefficient_x);
72 BID_RETURN (res);
73 }
74 // check for overflow
75 if (exponent_x > MAX_DECIMAL_EXPONENT) {
76 // try to normalize coefficient
77 while ((coefficient_x < 1000000000000000ull)
78 && (exponent_x > MAX_DECIMAL_EXPONENT)) {
79 // coefficient_x < 10^15, scale by 10
80 coefficient_x = (coefficient_x << 1) + (coefficient_x << 3);
81 exponent_x--;
82 }
83 if (exponent_x <= MAX_DECIMAL_EXPONENT) {
84 res = very_fast_get_BID64 (sign_x, exponent_x, coefficient_x);
85 BID_RETURN (res);
86 }
87 }
88 // exponent < 0
89 // the BID pack routine will round the coefficient
90 rmode = rnd_mode;
91 res = get_BID64 (sign_x, exponent_x, coefficient_x, rmode, pfpsf);
92 BID_RETURN (res);
93
94 }