(root)/
gcc-13.2.0/
libgcc/
config/
libbid/
bid128_scalb.c
       1  /* Copyright (C) 2007-2023 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 3, or (at your option) any later
       8  version.
       9  
      10  GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11  WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13  for more details.
      14  
      15  Under Section 7 of GPL version 3, you are granted additional
      16  permissions described in the GCC Runtime Library Exception, version
      17  3.1, as published by the Free Software Foundation.
      18  
      19  You should have received a copy of the GNU General Public License and
      20  a copy of the GCC Runtime Library Exception along with this program;
      21  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      22  <http://www.gnu.org/licenses/>.  */
      23  
      24  #define BID_128RES
      25  #include "bid_internal.h"
      26  
      27  #define DECIMAL_EXPONENT_BIAS_128 6176
      28  #define MAX_DECIMAL_EXPONENT_128  12287
      29  
      30  
      31  
      32  BID128_FUNCTION_ARG128_ARGTYPE2 (bid128_scalb, x, int, n)
      33  
      34       UINT128 CX, CX2, CX8, res;
      35       SINT64 exp64;
      36       UINT64 sign_x;
      37       int exponent_x, rmode;
      38  
      39    // unpack arguments, check for NaN or Infinity
      40  if (!unpack_BID128_value (&sign_x, &exponent_x, &CX, x)) {
      41      // x is Inf. or NaN or 0
      42  #ifdef SET_STATUS_FLAGS
      43  if ((x.w[1] & SNAN_MASK64) == SNAN_MASK64)	// y is sNaN
      44    __set_status_flags (pfpsf, INVALID_EXCEPTION);
      45  #endif
      46  res.w[1] = CX.w[1] & QUIET_MASK64;
      47  res.w[0] = CX.w[0];
      48  if (!CX.w[1]) {
      49         exp64 = (SINT64) exponent_x + (SINT64) n;
      50  	   if(exp64<0) exp64=0;
      51  	   if(exp64>MAX_DECIMAL_EXPONENT_128) exp64=MAX_DECIMAL_EXPONENT_128;
      52         exponent_x = exp64;
      53    get_BID128_very_fast (&res, sign_x, exponent_x, CX);
      54  }
      55  BID_RETURN (res);
      56  }
      57  
      58  exp64 = (SINT64) exponent_x + (SINT64) n;
      59  exponent_x = exp64;
      60  
      61  if ((UINT32) exponent_x <= MAX_DECIMAL_EXPONENT_128) {
      62    get_BID128_very_fast (&res, sign_x, exponent_x, CX);
      63    BID_RETURN (res);
      64  }
      65    // check for overflow
      66  if (exp64 > MAX_DECIMAL_EXPONENT_128) {
      67    if (CX.w[1] < 0x314dc6448d93ull) {
      68      // try to normalize coefficient
      69      do {
      70        CX8.w[1] = (CX.w[1] << 3) | (CX.w[0] >> 61);
      71        CX8.w[0] = CX.w[0] << 3;
      72        CX2.w[1] = (CX.w[1] << 1) | (CX.w[0] >> 63);
      73        CX2.w[0] = CX.w[0] << 1;
      74        __add_128_128 (CX, CX2, CX8);
      75  
      76        exponent_x--;
      77        exp64--;
      78      }
      79      while (CX.w[1] < 0x314dc6448d93ull
      80  	   && exp64 > MAX_DECIMAL_EXPONENT_128);
      81  
      82    }
      83    if (exp64 <= MAX_DECIMAL_EXPONENT_128) {
      84      get_BID128_very_fast (&res, sign_x, exponent_x, CX);
      85      BID_RETURN (res);
      86    } else
      87      exponent_x = 0x7fffffff;	// overflow
      88  }
      89    // exponent < 0
      90    // the BID pack routine will round the coefficient
      91  rmode = rnd_mode;
      92  get_BID128 (&res, sign_x, exponent_x, CX, (unsigned int *) &rmode,
      93  	    pfpsf);
      94  BID_RETURN (res);
      95  
      96  }