Reverting merge from trunk
[official-gcc.git] / libgcc / config / s390 / 32 / _fixtfdi.c
blobfd4cb5d27979b16a9d5b0adf5e7157e20c582200
1 /* Definitions of target machine for GNU compiler, for IBM S/390
2 Copyright (C) 1999-2013 Free Software Foundation, Inc.
3 Contributed by Hartmut Penner (hpenner@de.ibm.com) and
4 Ulrich Weigand (uweigand@de.ibm.com).
6 This file is part of GCC.
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.
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.
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.
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/>. */
27 #define EXPD(fp) (((fp.l.i[0]) >> 16) & 0x7FFF)
28 #define EXPONENT_BIAS 16383
29 #define MANTISSA_BITS 112
30 #define PRECISION (MANTISSA_BITS + 1)
31 #define SIGNBIT 0x80000000
32 #define SIGND(fp) ((fp.l.i[0]) & SIGNBIT)
33 #define MANTD_HIGH_LL(fp) ((fp.ll[0] & HIGH_LL_FRAC_MASK) | HIGH_LL_UNIT_BIT)
34 #define MANTD_LOW_LL(fp) (fp.ll[1])
35 #define FRACD_ZERO_P(fp) (!fp.ll[1] && !(fp.ll[0] & HIGH_LL_FRAC_MASK))
36 #define HIGH_LL_FRAC_BITS 48
37 #define HIGH_LL_UNIT_BIT ((UDItype_x)1 << HIGH_LL_FRAC_BITS)
38 #define HIGH_LL_FRAC_MASK (HIGH_LL_UNIT_BIT - 1)
40 typedef int DItype_x __attribute__ ((mode (DI)));
41 typedef unsigned int UDItype_x __attribute__ ((mode (DI)));
42 typedef int SItype_x __attribute__ ((mode (SI)));
43 typedef unsigned int USItype_x __attribute__ ((mode (SI)));
45 union double_long {
46 long double d;
47 struct {
48 SItype_x i[4]; /* 32 bit parts: 0 upper ... 3 lowest */
49 } l;
50 UDItype_x ll[2]; /* 64 bit parts: 0 upper, 1 lower */
53 DItype_x __fixtfdi (long double a1);
55 /* convert double to unsigned int */
56 DItype_x
57 __fixtfdi (long double a1)
59 register union double_long dl1;
60 register int exp;
61 register UDItype_x l;
63 dl1.d = a1;
65 /* +/- 0, denormalized */
66 if (!EXPD (dl1))
67 return 0;
69 /* The exponent - considered the binary point at the right end of
70 the mantissa. */
71 exp = EXPD (dl1) - EXPONENT_BIAS - MANTISSA_BITS;
73 /* number < 1: If the mantissa would need to be right-shifted more bits than
74 its size the result would be zero. */
75 if (exp <= -PRECISION)
76 return 0;
78 /* NaN: All exponent bits set and a nonzero fraction. */
79 if ((EXPD(dl1) == 0x7fff) && !FRACD_ZERO_P (dl1))
80 return 0x8000000000000000ULL;
82 /* One extra bit is needed for the unit bit which is appended by
83 MANTD_HIGH_LL on the left of the matissa. */
84 exp += HIGH_LL_FRAC_BITS + 1;
86 /* If the result would still need a left shift it will be too large
87 to be represented. Compared to the unsigned variant we have to
88 take care that there is still space for the sign bit to be
89 applied. So we can only go on if there is a right-shift by one
90 or more. */
91 if (exp >= 0)
93 l = 1ULL << 63; /* long long min */
94 return SIGND (dl1) ? l : l - 1;
97 l = MANTD_LOW_LL (dl1) >> (HIGH_LL_FRAC_BITS + 1)
98 | MANTD_HIGH_LL (dl1) << (64 - (HIGH_LL_FRAC_BITS + 1));
100 return SIGND (dl1) ? -(l >> -exp) : l >> -exp;