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#define MAX_DECIMAL_EXPONENT  767
29
30#if DECIMAL_CALL_BY_REFERENCE
31
32void
33bid64_scalb (UINT64 * pres, UINT64 * px,
34	     int *pn _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
35	     _EXC_INFO_PARAM) {
36  UINT64 x;
37  int n;
38#else
39
40UINT64
41bid64_scalb (UINT64 x,
42	     int n _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
43	     _EXC_INFO_PARAM) {
44#endif
45  UINT64 sign_x, coefficient_x, res;
46  SINT64 exp64;
47  int exponent_x, rmode;
48
49#if DECIMAL_CALL_BY_REFERENCE
50#if !DECIMAL_GLOBAL_ROUNDING
51  _IDEC_round rnd_mode = *prnd_mode;
52#endif
53  x = *px;
54  n = *pn;
55#endif
56
57  // unpack arguments, check for NaN or Infinity
58  if (!unpack_BID64 (&sign_x, &exponent_x, &coefficient_x, x)) {
59    // x is Inf. or NaN or 0
60#ifdef SET_STATUS_FLAGS
61    if ((x & SNAN_MASK64) == SNAN_MASK64)	// y is sNaN
62      __set_status_flags (pfpsf, INVALID_EXCEPTION);
63#endif
64    if (coefficient_x)
65      res = coefficient_x & QUIET_MASK64;
66	else {
67       exp64 = (SINT64) exponent_x + (SINT64) n;
68	   if(exp64<0) exp64=0;
69	   if(exp64>MAX_DECIMAL_EXPONENT) exp64=MAX_DECIMAL_EXPONENT;
70       exponent_x = exp64;
71      res = very_fast_get_BID64 (sign_x, exponent_x, coefficient_x);	// 0
72	}
73    BID_RETURN (res);
74  }
75
76  exp64 = (SINT64) exponent_x + (SINT64) n;
77  exponent_x = exp64;
78
79  if ((UINT32) exponent_x <= MAX_DECIMAL_EXPONENT) {
80    res = very_fast_get_BID64 (sign_x, exponent_x, coefficient_x);
81    BID_RETURN (res);
82  }
83  // check for overflow
84  if (exp64 > MAX_DECIMAL_EXPONENT) {
85    // try to normalize coefficient
86    while ((coefficient_x < 1000000000000000ull)
87	   && (exp64 > MAX_DECIMAL_EXPONENT)) {
88      // coefficient_x < 10^15, scale by 10
89      coefficient_x = (coefficient_x << 1) + (coefficient_x << 3);
90      exponent_x--;
91      exp64--;
92    }
93    if (exp64 <= MAX_DECIMAL_EXPONENT) {
94      res = very_fast_get_BID64 (sign_x, exponent_x, coefficient_x);
95      BID_RETURN (res);
96    } else
97      exponent_x = 0x7fffffff;	// overflow
98  }
99  // exponent < 0
100  // the BID pack routine will round the coefficient
101  rmode = rnd_mode;
102  res = get_BID64 (sign_x, exponent_x, coefficient_x, rmode, pfpsf);
103  BID_RETURN (res);
104
105}
106