[gcc]
[official-gcc.git] / libgcc / config / rs6000 / ppc64-fp.c
blobfaffc82eedab8be390ae5150c1829a9805228524
1 /* Functions needed for soft-float on powerpc64-linux, copied from
2 libgcc2.c with macros expanded to force the use of specific types.
4 Copyright (C) 1989-2018 Free Software Foundation, Inc.
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 #if defined(__powerpc64__) || defined (__64BIT__) || defined(__ppc64__)
28 #define TMODES
29 #include "fp-bit.h"
31 extern DItype __fixtfdi (TFtype);
32 extern DItype __fixdfdi (DFtype);
33 extern DItype __fixsfdi (SFtype);
34 extern USItype __fixunsdfsi (DFtype);
35 extern USItype __fixunssfsi (SFtype);
36 extern TFtype __floatditf (DItype);
37 extern TFtype __floatunditf (UDItype);
38 extern DFtype __floatdidf (DItype);
39 extern DFtype __floatundidf (UDItype);
40 extern SFtype __floatdisf (DItype);
41 extern SFtype __floatundisf (UDItype);
42 extern DItype __fixunstfdi (TFtype);
44 static DItype local_fixunssfdi (SFtype);
45 static DItype local_fixunsdfdi (DFtype);
47 DItype
48 __fixtfdi (TFtype a)
50 if (a < 0)
51 return - __fixunstfdi (-a);
52 return __fixunstfdi (a);
55 DItype
56 __fixdfdi (DFtype a)
58 if (a < 0)
59 return - local_fixunsdfdi (-a);
60 return local_fixunsdfdi (a);
63 DItype
64 __fixsfdi (SFtype a)
66 if (a < 0)
67 return - local_fixunssfdi (-a);
68 return local_fixunssfdi (a);
71 USItype
72 __fixunsdfsi (DFtype a)
74 if (a >= - (DFtype) (- ((SItype)(((USItype)1 << ((4 * 8) - 1)) - 1)) - 1))
75 return (SItype) (a + (- ((SItype)(((USItype)1 << ((4 * 8) - 1)) - 1)) - 1))
76 - (- ((SItype)(((USItype)1 << ((4 * 8) - 1)) - 1)) - 1);
77 return (SItype) a;
80 USItype
81 __fixunssfsi (SFtype a)
83 if (a >= - (SFtype) (- ((SItype)(((USItype)1 << ((4 * 8) - 1)) - 1)) - 1))
84 return (SItype) (a + (- ((SItype)(((USItype)1 << ((4 * 8) - 1)) - 1)) - 1))
85 - (- ((SItype)(((USItype)1 << ((4 * 8) - 1)) - 1)) - 1);
86 return (SItype) a;
89 TFtype
90 __floatditf (DItype u)
92 DFtype dh, dl;
94 dh = (SItype) (u >> (sizeof (SItype) * 8));
95 dh *= 2.0 * (((UDItype) 1) << ((sizeof (SItype) * 8) - 1));
96 dl = (USItype) (u & ((((UDItype) 1) << (sizeof (SItype) * 8)) - 1));
98 return (TFtype) dh + (TFtype) dl;
101 TFtype
102 __floatunditf (UDItype u)
104 DFtype dh, dl;
106 dh = (USItype) (u >> (sizeof (SItype) * 8));
107 dh *= 2.0 * (((UDItype) 1) << ((sizeof (SItype) * 8) - 1));
108 dl = (USItype) (u & ((((UDItype) 1) << (sizeof (SItype) * 8)) - 1));
110 return (TFtype) dh + (TFtype) dl;
113 DFtype
114 __floatdidf (DItype u)
116 DFtype d;
118 d = (SItype) (u >> (sizeof (SItype) * 8));
119 d *= 2.0 * (((UDItype) 1) << ((sizeof (SItype) * 8) - 1));
120 d += (USItype) (u & ((((UDItype) 1) << (sizeof (SItype) * 8)) - 1));
122 return d;
125 DFtype
126 __floatundidf (UDItype u)
128 DFtype d;
130 d = (USItype) (u >> (sizeof (SItype) * 8));
131 d *= 2.0 * (((UDItype) 1) << ((sizeof (SItype) * 8) - 1));
132 d += (USItype) (u & ((((UDItype) 1) << (sizeof (SItype) * 8)) - 1));
134 return d;
137 SFtype
138 __floatdisf (DItype u)
140 DFtype f;
142 if (53 < (sizeof (DItype) * 8)
143 && 53 > ((sizeof (DItype) * 8) - 53 + 24))
145 if (! (- ((DItype) 1 << 53) < u
146 && u < ((DItype) 1 << 53)))
148 if ((UDItype) u & (((UDItype) 1 << ((sizeof (DItype) * 8) - 53)) - 1))
150 u &= ~ (((UDItype) 1 << ((sizeof (DItype) * 8) - 53)) - 1);
151 u |= ((UDItype) 1 << ((sizeof (DItype) * 8) - 53));
155 f = (SItype) (u >> (sizeof (SItype) * 8));
156 f *= 2.0 * (((UDItype) 1) << ((sizeof (SItype) * 8) - 1));
157 f += (USItype) (u & ((((UDItype) 1) << (sizeof (SItype) * 8)) - 1));
159 return (SFtype) f;
162 SFtype
163 __floatundisf (UDItype u)
165 DFtype f;
167 if (53 < (sizeof (DItype) * 8)
168 && 53 > ((sizeof (DItype) * 8) - 53 + 24))
170 if (u >= ((UDItype) 1 << 53))
172 if ((UDItype) u & (((UDItype) 1 << ((sizeof (DItype) * 8) - 53)) - 1))
174 u &= ~ (((UDItype) 1 << ((sizeof (DItype) * 8) - 53)) - 1);
175 u |= ((UDItype) 1 << ((sizeof (DItype) * 8) - 53));
179 f = (USItype) (u >> (sizeof (SItype) * 8));
180 f *= 2.0 * (((UDItype) 1) << ((sizeof (SItype) * 8) - 1));
181 f += (USItype) (u & ((((UDItype) 1) << (sizeof (SItype) * 8)) - 1));
183 return (SFtype) f;
186 DItype
187 __fixunstfdi (TFtype a)
189 if (a < 0)
190 return 0;
192 /* Compute high word of result, as a flonum. */
193 const TFtype b = (a / (((UDItype) 1) << (sizeof (SItype) * 8)));
194 /* Convert that to fixed (but not to DItype!),
195 and shift it into the high word. */
196 UDItype v = (USItype) b;
197 v <<= (sizeof (SItype) * 8);
198 /* Remove high part from the TFtype, leaving the low part as flonum. */
199 a -= (TFtype) v;
200 /* Convert that to fixed (but not to DItype!) and add it in.
201 Sometimes A comes out negative. This is significant, since
202 A has more bits than a long int does. */
203 if (a < 0)
204 v -= (USItype) (-a);
205 else
206 v += (USItype) a;
207 return v;
210 /* This version is needed to prevent recursion; fixunsdfdi in libgcc
211 calls fixdfdi, which in turn calls calls fixunsdfdi. */
213 static DItype
214 local_fixunsdfdi (DFtype a)
216 USItype hi, lo;
218 hi = a / (((UDItype) 1) << (sizeof (SItype) * 8));
219 lo = (a - ((DFtype) hi) * (((UDItype) 1) << (sizeof (SItype) * 8)));
220 return ((UDItype) hi << (sizeof (SItype) * 8)) | lo;
223 /* This version is needed to prevent recursion; fixunssfdi in libgcc
224 calls fixsfdi, which in turn calls calls fixunssfdi. */
226 static DItype
227 local_fixunssfdi (SFtype original_a)
229 DFtype a = original_a;
230 USItype hi, lo;
232 hi = a / (((UDItype) 1) << (sizeof (SItype) * 8));
233 lo = (a - ((DFtype) hi) * (((UDItype) 1) << (sizeof (SItype) * 8)));
234 return ((UDItype) hi << (sizeof (SItype) * 8)) | lo;
237 #endif /* __powerpc64__ */