_fixunstfdi.c revision 1.3
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
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16for more details.
17
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see 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
40typedef int DItype_x __attribute__ ((mode (DI)));
41typedef unsigned int UDItype_x __attribute__ ((mode (DI)));
42typedef int SItype_x __attribute__ ((mode (SI)));
43typedef unsigned int USItype_x __attribute__ ((mode (SI)));
44
45union 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
53UDItype_x __fixunstfdi (long double a1);
54
55/* convert double to unsigned int */
56UDItype_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