1/* Definitions of target machine for GNU compiler, for IBM S/390
2   Copyright (C) 1999-2022 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#ifndef __s390x__
28
29#define EXPD(fp)	   (((fp.l.i[0]) >> 16) & 0x7FFF)
30#define EXPONENT_BIAS	   16383
31#define MANTISSA_BITS      112
32#define PRECISION          (MANTISSA_BITS + 1)
33#define SIGNBIT		   0x80000000
34#define SIGND(fp)	   ((fp.l.i[0]) & SIGNBIT)
35#define MANTD_HIGH_LL(fp)  ((fp.ll[0] & HIGH_LL_FRAC_MASK) | HIGH_LL_UNIT_BIT)
36#define MANTD_LOW_LL(fp)   (fp.ll[1])
37#define FRACD_ZERO_P(fp)   (!fp.ll[1] && !(fp.ll[0] & HIGH_LL_FRAC_MASK))
38#define HIGH_LL_FRAC_BITS  48
39#define HIGH_LL_UNIT_BIT   ((UDItype_x)1 << HIGH_LL_FRAC_BITS)
40#define HIGH_LL_FRAC_MASK  (HIGH_LL_UNIT_BIT - 1)
41
42typedef int DItype_x __attribute__ ((mode (DI)));
43typedef unsigned int UDItype_x __attribute__ ((mode (DI)));
44typedef int SItype_x __attribute__ ((mode (SI)));
45typedef unsigned int USItype_x __attribute__ ((mode (SI)));
46
47union double_long {
48  long double d;
49  struct {
50      SItype_x i[4]; /* 32 bit parts: 0 upper ... 3 lowest */
51    } l;
52  UDItype_x ll[2];   /* 64 bit parts: 0 upper, 1 lower */
53};
54
55static __inline__ void
56fexceptdiv (float d, float e)
57{
58  __asm__ __volatile__ ("debr %0,%1" : : "f" (d), "f" (e) );
59}
60
61UDItype_x __fixunstfdi (long double a1);
62
63/* convert double to unsigned int */
64UDItype_x
65__fixunstfdi (long double a1)
66{
67    register union double_long dl1;
68    register int exp;
69    register UDItype_x l;
70
71    dl1.d = a1;
72
73    /* +/- 0, denormalized */
74    if (!EXPD (dl1))
75      return 0;
76
77    /* Negative.  */
78    if (SIGND (dl1))
79      {
80	/* Value is <= -1.0
81	   C99 Annex F.4 requires an "invalid" exception to be thrown.  */
82	if (EXPD (dl1) >= EXPONENT_BIAS)
83	  fexceptdiv (0.0, 0.0);
84	return 0;
85      }
86
87    /* The exponent - considered the binary point at the right end of
88       the mantissa.  */
89    exp = EXPD (dl1) - EXPONENT_BIAS - MANTISSA_BITS;
90
91    /* number < 1: If the mantissa would need to be right-shifted more bits than
92       its size (plus the implied one bit on the left) the result would be
93       zero.  */
94    if (exp <= -PRECISION)
95      return 0;
96
97    /* NaN: All exponent bits set and a nonzero fraction.  */
98    if ((EXPD(dl1) == 0x7fff) && !FRACD_ZERO_P (dl1))
99      {
100	/* C99 Annex F.4 requires an "invalid" exception to be thrown.  */
101	fexceptdiv (0.0, 0.0);
102	return 0;
103      }
104
105    /* One extra bit is needed for the unit bit which is appended by
106       MANTD_HIGH_LL on the left of the matissa.  */
107    exp += HIGH_LL_FRAC_BITS + 1;
108
109    /* If the result would still need a left shift it will be too
110       large to be represented.  Infinities have all exponent bits set
111       and will end up here as well.  */
112    if (exp > 0)
113      {
114	/* C99 Annex F.4 requires an "invalid" exception to be thrown.  */
115	fexceptdiv (0.0, 0.0);
116	return 0xFFFFFFFFFFFFFFFFULL;
117      }
118
119    l = MANTD_LOW_LL (dl1) >> (HIGH_LL_FRAC_BITS + 1)
120        | MANTD_HIGH_LL (dl1) << (64 - (HIGH_LL_FRAC_BITS + 1));
121
122    return l >> -exp;
123}
124
125#endif /* !__s390x__ */
126