1 /* Software floating-point emulation. 2 Convert IEEE extended to signed or unsigned _BitInt. 3 4 Copyright (C) 2023 Free Software Foundation, Inc. 5 6 This file is part of GCC. 7 8 GCC is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free 10 Software Foundation; either version 3, or (at your option) any later 11 version. 12 13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 for more details. 17 18 Under Section 7 of GPL version 3, you are granted additional 19 permissions described in the GCC Runtime Library Exception, version 20 3.1, as published by the Free Software Foundation. 21 22 You should have received a copy of the GNU General Public License and 23 a copy of the GCC Runtime Library Exception along with this program; 24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25 <http://www.gnu.org/licenses/>. */ 26 27 #include "soft-fp.h" 28 #include "extended.h" 29 #include "bitint.h" 30 31 #ifdef __BITINT_MAXWIDTH__ 32 33 #ifndef TI_BITS 34 /* While mantissa is 64 bits including 1 explicit bit, extended.h uses 35 op-2.h for W_TYPE_SIZE 64 and op-4.h for W_TYPE_SIZE 32, so we have 36 to use 128-bit type here. On most 32-bit architectures TImode isn't 37 supported, so use _BitInt(128) instead. */ 38 typedef _BitInt(128) TItype; 39 typedef unsigned _BitInt(128) UTItype; 40 #define TI_BITS 128 41 #endif 42 43 void 44 __fixxfbitint (UBILtype *r, SItype rprec, XFtype a) 45 { 46 FP_DECL_EX; 47 FP_DECL_E (A); 48 USItype arprec = rprec < 0 ? -rprec : rprec; 49 USItype rn = ((USItype) arprec + BIL_TYPE_SIZE - 1) / BIL_TYPE_SIZE; 50 UTItype rv; 51 USItype rsize = arprec > TI_BITS ? TI_BITS : arprec; 52 USItype rsigned = rprec < 0; 53 USItype ovf = 0; 54 USItype shift = 0; 55 56 FP_INIT_EXCEPTIONS; 57 FP_UNPACK_RAW_E (A, a); 58 if (arprec > TI_BITS) 59 { 60 if (A_e < _FP_EXPBIAS_E || (A_s && !rsigned)) 61 ovf = 1; 62 else if (A_e >= (_FP_EXPMAX_E < _FP_EXPBIAS_E + arprec 63 ? _FP_EXPMAX_E 64 : _FP_EXPBIAS_E + arprec - rsigned)) 65 { 66 ovf = 1; 67 if (A_s 68 && A_e == _FP_EXPBIAS_E + arprec - 1 69 && A_e < _FP_EXPMAX_E) 70 A_e -= arprec - TI_BITS; 71 } 72 else if (A_e >= _FP_EXPBIAS_E + TI_BITS - rsigned) 73 { 74 shift = A_e - (_FP_EXPBIAS_E + TI_BITS - rsigned - 1); 75 A_e -= shift; 76 } 77 } 78 FP_TO_INT_E (rv, A, rsize, rsigned); 79 FP_HANDLE_EXCEPTIONS; 80 FP_TO_BITINT (r, rn, arprec, shift, rv, rsize, rsigned, ovf, TI); 81 } 82 #endif 83