xref: /netbsd-src/external/gpl3/gcc.old/dist/libgcc/config/s390/32/_fixunstfdi.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
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).
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 #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)
39 
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)));
44 
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 */
51 };
52 
53 UDItype_x __fixunstfdi (long double a1);
54 
55 /* convert double to unsigned int */
56 UDItype_x
57 __fixunstfdi (long double a1)
58 {
59     register union double_long dl1;
60     register int exp;
61     register UDItype_x l;
62 
63     dl1.d = a1;
64 
65     /* +/- 0, denormalized, negative */
66     if (!EXPD (dl1) || SIGND(dl1))
67       return 0;
68 
69     /* The exponent - considered the binary point at the right end of
70        the mantissa.  */
71     exp = EXPD (dl1) - EXPONENT_BIAS - MANTISSA_BITS;
72 
73     /* number < 1: If the mantissa would need to be right-shifted more bits than
74        its size (plus the implied one bit on the left) the result would be
75        zero.  */
76     if (exp <= -PRECISION)
77       return 0;
78 
79     /* NaN: All exponent bits set and a nonzero fraction.  */
80     if ((EXPD(dl1) == 0x7fff) && !FRACD_ZERO_P (dl1))
81       return 0x0ULL;
82 
83     /* One extra bit is needed for the unit bit which is appended by
84        MANTD_HIGH_LL on the left of the matissa.  */
85     exp += HIGH_LL_FRAC_BITS + 1;
86 
87     /* If the result would still need a left shift it will be too large
88        to be represented.  */
89     if (exp > 0)
90       return 0xFFFFFFFFFFFFFFFFULL;
91 
92     l = MANTD_LOW_LL (dl1) >> (HIGH_LL_FRAC_BITS + 1)
93         | MANTD_HIGH_LL (dl1) << (64 - (HIGH_LL_FRAC_BITS + 1));
94 
95     return l >> -exp;
96 }
97