gcc:
[official-gcc.git] / gcc / config / m68k / fpgnulib.c
blob2e8dd5d14307af65a6717f7dbaaf2d7ca7b5130c
1 /* This is a stripped down version of floatlib.c. It supplies only those
2 functions which exist in libgcc, but for which there is not assembly
3 language versions in m68k/lb1sf68.asm.
5 It also includes simplistic support for extended floats (by working in
6 double precision). You must compile this file again with -DEXTFLOAT
7 to get this support. */
9 /*
10 ** gnulib support for software floating point.
11 ** Copyright (C) 1991 by Pipeline Associates, Inc. All rights reserved.
12 ** Permission is granted to do *anything* you want with this file,
13 ** commercial or otherwise, provided this message remains intact. So there!
14 ** I would appreciate receiving any updates/patches/changes that anyone
15 ** makes, and am willing to be the repository for said changes (am I
16 ** making a big mistake?).
18 ** Pat Wood
19 ** Pipeline Associates, Inc.
20 ** pipeline!phw@motown.com or
21 ** sun!pipeline!phw or
22 ** uunet!motown!pipeline!phw
24 ** 05/01/91 -- V1.0 -- first release to gcc mailing lists
25 ** 05/04/91 -- V1.1 -- added float and double prototypes and return values
26 ** -- fixed problems with adding and subtracting zero
27 ** -- fixed rounding in truncdfsf2
28 ** -- fixed SWAP define and tested on 386
32 ** The following are routines that replace the gnulib soft floating point
33 ** routines that are called automatically when -msoft-float is selected.
34 ** The support single and double precision IEEE format, with provisions
35 ** for byte-swapped machines (tested on 386). Some of the double-precision
36 ** routines work at full precision, but most of the hard ones simply punt
37 ** and call the single precision routines, producing a loss of accuracy.
38 ** long long support is not assumed or included.
39 ** Overall accuracy is close to IEEE (actually 68882) for single-precision
40 ** arithmetic. I think there may still be a 1 in 1000 chance of a bit
41 ** being rounded the wrong way during a multiply. I'm not fussy enough to
42 ** bother with it, but if anyone is, knock yourself out.
44 ** Efficiency has only been addressed where it was obvious that something
45 ** would make a big difference. Anyone who wants to do this right for
46 ** best speed should go in and rewrite in assembler.
48 ** I have tested this only on a 68030 workstation and 386/ix integrated
49 ** in with -msoft-float.
52 /* the following deal with IEEE single-precision numbers */
53 #define EXCESS 126L
54 #define SIGNBIT 0x80000000L
55 #define HIDDEN (1L << 23L)
56 #define SIGN(fp) ((fp) & SIGNBIT)
57 #define EXP(fp) (((fp) >> 23L) & 0xFF)
58 #define MANT(fp) (((fp) & 0x7FFFFFL) | HIDDEN)
59 #define PACK(s,e,m) ((s) | ((e) << 23L) | (m))
61 /* the following deal with IEEE double-precision numbers */
62 #define EXCESSD 1022L
63 #define HIDDEND (1L << 20L)
64 #define EXPDBITS 11
65 #define EXPDMASK 0x7FFL
66 #define EXPD(fp) (((fp.l.upper) >> 20L) & 0x7FFL)
67 #define SIGND(fp) ((fp.l.upper) & SIGNBIT)
68 #define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \
69 (fp.l.lower >> 22))
70 #define MANTDMASK 0xFFFFFL /* mask of upper part */
72 /* the following deal with IEEE extended-precision numbers */
73 #define EXCESSX 16382L
74 #define HIDDENX (1L << 31L)
75 #define EXPXBITS 15
76 #define EXPXMASK 0x7FFF
77 #define EXPX(fp) (((fp.l.upper) >> 16) & EXPXMASK)
78 #define SIGNX(fp) ((fp.l.upper) & SIGNBIT)
79 #define MANTXMASK 0x7FFFFFFFL /* mask of upper part */
81 union double_long
83 double d;
84 struct {
85 long upper;
86 unsigned long lower;
87 } l;
90 union float_long {
91 float f;
92 long l;
95 union long_double_long
97 long double ld;
98 struct
100 long upper;
101 unsigned long middle;
102 unsigned long lower;
103 } l;
106 #ifndef EXTFLOAT
108 /* convert int to double */
109 double
110 __floatsidf (long a1)
112 long sign = 0, exp = 31 + EXCESSD;
113 union double_long dl;
115 if (!a1)
117 dl.l.upper = dl.l.lower = 0;
118 return dl.d;
121 if (a1 < 0)
123 sign = SIGNBIT;
124 a1 = (long)-(unsigned long)a1;
125 if (a1 < 0)
127 dl.l.upper = SIGNBIT | ((32 + EXCESSD) << 20L);
128 dl.l.lower = 0;
129 return dl.d;
133 while (a1 < 0x1000000L)
135 a1 <<= 4;
136 exp -= 4;
139 while (a1 < 0x40000000L)
141 a1 <<= 1;
142 exp--;
145 /* pack up and go home */
146 dl.l.upper = sign;
147 dl.l.upper |= exp << 20L;
148 dl.l.upper |= (a1 >> 10L) & ~HIDDEND;
149 dl.l.lower = a1 << 22L;
151 return dl.d;
154 /* convert int to float */
155 float
156 __floatsisf (long l)
158 double foo = __floatsidf (l);
159 return foo;
162 /* convert float to double */
163 double
164 __extendsfdf2 (float a1)
166 register union float_long fl1;
167 register union double_long dl;
168 register long exp;
169 register long mant;
171 fl1.f = a1;
173 if (!fl1.l)
175 dl.l.upper = dl.l.lower = 0;
176 return dl.d;
179 dl.l.upper = SIGN (fl1.l);
180 exp = EXP(fl1.l);
181 mant = MANT (fl1.l) & ~HIDDEN;
182 if (exp == 0)
184 /* Denormal. */
185 exp = 1;
186 while (!(mant & HIDDEN))
188 mant <<= 1;
189 exp--;
191 mant &= ~HIDDEN;
193 exp = exp - EXCESS + EXCESSD;
194 dl.l.upper |= exp << 20;
195 dl.l.upper |= mant >> 3;
196 dl.l.lower = mant << 29;
198 return dl.d;
201 /* convert double to float */
202 float
203 __truncdfsf2 (double a1)
205 register long exp;
206 register long mant;
207 register union float_long fl;
208 register union double_long dl1;
210 dl1.d = a1;
212 if (!dl1.l.upper && !dl1.l.lower)
213 return 0;
215 exp = EXPD (dl1) - EXCESSD + EXCESS;
217 /* shift double mantissa 6 bits so we can round */
218 mant = MANTD (dl1) >> 6;
220 /* Check for underflow and denormals. */
221 if (exp <= 0)
223 if (exp < -24)
224 mant = 0;
225 else
226 mant >>= 1 - exp;
227 exp = 0;
230 /* now round and shift down */
231 mant += 1;
232 mant >>= 1;
234 /* did the round overflow? */
235 if (mant & 0xFF000000L)
237 mant >>= 1;
238 exp++;
241 mant &= ~HIDDEN;
243 /* pack up and go home */
244 fl.l = PACK (SIGND (dl1), exp, mant);
245 return (fl.f);
248 /* convert double to int */
249 long
250 __fixdfsi (double a1)
252 register union double_long dl1;
253 register long exp;
254 register long l;
256 dl1.d = a1;
258 if (!dl1.l.upper && !dl1.l.lower)
259 return 0;
261 exp = EXPD (dl1) - EXCESSD - 31;
262 l = MANTD (dl1);
264 if (exp > 0)
266 /* Return largest integer. */
267 return SIGND (dl1) ? 0x80000000L : 0x7fffffffL;
270 if (exp <= -32)
271 return 0;
273 /* shift down until exp = 0 */
274 if (exp < 0)
275 l >>= -exp;
277 return (SIGND (dl1) ? -l : l);
280 /* convert float to int */
281 long
282 __fixsfsi (float a1)
284 double foo = a1;
285 return __fixdfsi (foo);
288 #else /* EXTFLOAT */
290 /* Primitive extended precision floating point support.
292 We assume all numbers are normalized, don't do any rounding, etc. */
294 /* Prototypes for the above in case we use them. */
295 double __floatsidf (long);
296 float __floatsisf (long);
297 double __extendsfdf2 (float);
298 float __truncdfsf2 (double);
299 long __fixdfsi (double);
300 long __fixsfsi (float);
302 /* convert double to long double */
303 long double
304 __extenddfxf2 (double d)
306 register union double_long dl;
307 register union long_double_long ldl;
308 register long exp;
310 dl.d = d;
311 /*printf ("dfxf in: %g\n", d);*/
313 if (!dl.l.upper && !dl.l.lower)
314 return 0;
316 ldl.l.upper = SIGND (dl);
317 exp = EXPD (dl) - EXCESSD + EXCESSX;
318 ldl.l.upper |= exp << 16;
319 ldl.l.middle = HIDDENX;
320 /* 31-20: # mantissa bits in ldl.l.middle - # mantissa bits in dl.l.upper */
321 ldl.l.middle |= (dl.l.upper & MANTDMASK) << (31 - 20);
322 /* 1+20: explicit-integer-bit + # mantissa bits in dl.l.upper */
323 ldl.l.middle |= dl.l.lower >> (1 + 20);
324 /* 32 - 21: # bits of dl.l.lower in ldl.l.middle */
325 ldl.l.lower = dl.l.lower << (32 - 21);
327 /*printf ("dfxf out: %s\n", dumpxf (ldl.ld));*/
328 return ldl.ld;
331 /* convert long double to double */
332 double
333 __truncxfdf2 (long double ld)
335 register long exp;
336 register union double_long dl;
337 register union long_double_long ldl;
339 ldl.ld = ld;
340 /*printf ("xfdf in: %s\n", dumpxf (ld));*/
342 if (!ldl.l.upper && !ldl.l.middle && !ldl.l.lower)
343 return 0;
345 exp = EXPX (ldl) - EXCESSX + EXCESSD;
346 /* ??? quick and dirty: keep `exp' sane */
347 if (exp >= EXPDMASK)
348 exp = EXPDMASK - 1;
349 dl.l.upper = SIGNX (ldl);
350 dl.l.upper |= exp << (32 - (EXPDBITS + 1));
351 /* +1-1: add one for sign bit, but take one off for explicit-integer-bit */
352 dl.l.upper |= (ldl.l.middle & MANTXMASK) >> (EXPDBITS + 1 - 1);
353 dl.l.lower = (ldl.l.middle & MANTXMASK) << (32 - (EXPDBITS + 1 - 1));
354 dl.l.lower |= ldl.l.lower >> (EXPDBITS + 1 - 1);
356 /*printf ("xfdf out: %g\n", dl.d);*/
357 return dl.d;
360 /* convert a float to a long double */
361 long double
362 __extendsfxf2 (float f)
364 long double foo = __extenddfxf2 (__extendsfdf2 (f));
365 return foo;
368 /* convert a long double to a float */
369 float
370 __truncxfsf2 (long double ld)
372 float foo = __truncdfsf2 (__truncxfdf2 (ld));
373 return foo;
376 /* convert an int to a long double */
377 long double
378 __floatsixf (long l)
380 double foo = __floatsidf (l);
381 return foo;
384 /* convert a long double to an int */
385 long
386 __fixxfsi (long double ld)
388 long foo = __fixdfsi ((double) ld);
389 return foo;
392 /* The remaining provide crude math support by working in double precision. */
394 long double
395 __addxf3 (long double x1, long double x2)
397 return (double) x1 + (double) x2;
400 long double
401 __subxf3 (long double x1, long double x2)
403 return (double) x1 - (double) x2;
406 long double
407 __mulxf3 (long double x1, long double x2)
409 return (double) x1 * (double) x2;
412 long double
413 __divxf3 (long double x1, long double x2)
415 return (double) x1 / (double) x2;
418 long double
419 __negxf2 (long double x1)
421 return - (double) x1;
424 long
425 __cmpxf2 (long double x1, long double x2)
427 return __cmpdf2 ((double) x1, (double) x2);
430 long
431 __eqxf2 (long double x1, long double x2)
433 return __cmpdf2 ((double) x1, (double) x2);
436 long
437 __nexf2 (long double x1, long double x2)
439 return __cmpdf2 ((double) x1, (double) x2);
442 long
443 __ltxf2 (long double x1, long double x2)
445 return __cmpdf2 ((double) x1, (double) x2);
448 long
449 __lexf2 (long double x1, long double x2)
451 return __cmpdf2 ((double) x1, (double) x2);
454 long
455 __gtxf2 (long double x1, long double x2)
457 return __cmpdf2 ((double) x1, (double) x2);
460 long
461 __gexf2 (long double x1, long double x2)
463 return __cmpdf2 ((double) x1, (double) x2);
466 #endif /* EXTFLOAT */