1 /* TCC runtime library.
2 Parts of this code are (c) 2002 Fabrice Bellard
4 Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc.
6 This file is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 This file is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; see the file COPYING. If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA.
31 #define W_TYPE_SIZE 32
32 #define BITS_PER_UNIT 8
35 typedef unsigned int UWtype
;
36 typedef unsigned int USItype
;
37 typedef long long DWtype
;
38 typedef unsigned long long UDWtype
;
50 typedef long double XFtype
;
51 #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
52 #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
54 /* the following deal with IEEE single-precision numbers */
56 #define SIGNBIT 0x80000000
57 #define HIDDEN (1 << 23)
58 #define SIGN(fp) ((fp) & SIGNBIT)
59 #define EXP(fp) (((fp) >> 23) & 0xFF)
60 #define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN)
61 #define PACK(s,e,m) ((s) | ((e) << 23) | (m))
63 /* the following deal with IEEE double-precision numbers */
65 #define HIDDEND (1 << 20)
66 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
67 #define SIGND(fp) ((fp.l.upper) & SIGNBIT)
68 #define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \
70 #define HIDDEND_LL ((long long)1 << 52)
71 #define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL)
72 #define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m))
74 /* the following deal with x86 long double-precision numbers */
75 #define EXCESSLD 16382
76 #define EXPLD(fp) (fp.l.upper & 0x7fff)
77 #define SIGNLD(fp) ((fp.l.upper) & 0x8000)
83 unsigned long long lower
;
109 /* XXX: use gcc/tcc intrinsic ? */
110 #if defined(__i386__)
111 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
112 __asm__ ("subl %5,%1\n\tsbbl %3,%0" \
113 : "=r" ((USItype) (sh)), \
114 "=&r" ((USItype) (sl)) \
115 : "0" ((USItype) (ah)), \
116 "g" ((USItype) (bh)), \
117 "1" ((USItype) (al)), \
118 "g" ((USItype) (bl)))
119 #define umul_ppmm(w1, w0, u, v) \
121 : "=a" ((USItype) (w0)), \
122 "=d" ((USItype) (w1)) \
123 : "%0" ((USItype) (u)), \
124 "rm" ((USItype) (v)))
125 #define udiv_qrnnd(q, r, n1, n0, dv) \
127 : "=a" ((USItype) (q)), \
128 "=d" ((USItype) (r)) \
129 : "0" ((USItype) (n0)), \
130 "1" ((USItype) (n1)), \
131 "rm" ((USItype) (dv)))
132 #define count_leading_zeros(count, x) \
135 __asm__ ("bsrl %1,%0" \
136 : "=r" (__cbtmp) : "rm" ((USItype) (x))); \
137 (count) = __cbtmp ^ 31; \
140 #error unsupported CPU type
143 /* most of this code is taken from libgcc2.c from gcc */
145 static UDWtype
__udivmoddi4 (UDWtype n
, UDWtype d
, UDWtype
*rp
)
150 UWtype d0
, d1
, n0
, n1
, n2
;
162 #if !UDIV_NEEDS_NORMALIZATION
169 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
172 /* Remainder in n0. */
179 d0
= 1 / d0
; /* Divide intentionally by zero. */
181 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
182 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
184 /* Remainder in n0. */
195 #else /* UDIV_NEEDS_NORMALIZATION */
203 count_leading_zeros (bm
, d0
);
207 /* Normalize, i.e. make the most significant bit of the
211 n1
= (n1
<< bm
) | (n0
>> (W_TYPE_SIZE
- bm
));
215 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
218 /* Remainder in n0 >> bm. */
225 d0
= 1 / d0
; /* Divide intentionally by zero. */
227 count_leading_zeros (bm
, d0
);
231 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
232 conclude (the most significant bit of n1 is set) /\ (the
233 leading quotient digit q1 = 1).
235 This special case is necessary, not an optimization.
236 (Shifts counts of W_TYPE_SIZE are undefined.) */
245 b
= W_TYPE_SIZE
- bm
;
249 n1
= (n1
<< bm
) | (n0
>> b
);
252 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
257 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
259 /* Remainder in n0 >> bm. */
269 #endif /* UDIV_NEEDS_NORMALIZATION */
280 /* Remainder in n1n0. */
292 count_leading_zeros (bm
, d1
);
295 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
296 conclude (the most significant bit of n1 is set) /\ (the
297 quotient digit q0 = 0 or 1).
299 This special case is necessary, not an optimization. */
301 /* The condition on the next line takes advantage of that
302 n1 >= d1 (true due to program flow). */
303 if (n1
> d1
|| n0
>= d0
)
306 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
325 b
= W_TYPE_SIZE
- bm
;
327 d1
= (d1
<< bm
) | (d0
>> b
);
330 n1
= (n1
<< bm
) | (n0
>> b
);
333 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
334 umul_ppmm (m1
, m0
, q0
, d0
);
336 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
339 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
344 /* Remainder in (n1n0 - m1m0) >> bm. */
347 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
348 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
349 rr
.s
.high
= n1
>> bm
;
361 #define __negdi2(a) (-(a))
363 long long __divdi3(long long u
, long long v
)
374 uu
.ll
= __negdi2 (uu
.ll
);
378 vv
.ll
= __negdi2 (vv
.ll
);
380 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDWtype
*) 0);
386 long long __moddi3(long long u
, long long v
)
397 uu
.ll
= __negdi2 (uu
.ll
);
400 vv
.ll
= __negdi2 (vv
.ll
);
402 __udivmoddi4 (uu
.ll
, vv
.ll
, (UDWtype
*) &w
);
408 unsigned long long __udivdi3(unsigned long long u
, unsigned long long v
)
410 return __udivmoddi4 (u
, v
, (UDWtype
*) 0);
413 unsigned long long __umoddi3(unsigned long long u
, unsigned long long v
)
417 __udivmoddi4 (u
, v
, &w
);
421 /* XXX: fix tcc's code generator to do this instead */
422 long long __ashrdi3(long long a
, int b
)
428 u
.s
.low
= u
.s
.high
>> (b
- 32);
429 u
.s
.high
= u
.s
.high
>> 31;
431 u
.s
.low
= ((unsigned)u
.s
.low
>> b
) | (u
.s
.high
<< (32 - b
));
432 u
.s
.high
= u
.s
.high
>> b
;
440 /* XXX: fix tcc's code generator to do this instead */
441 unsigned long long __lshrdi3(unsigned long long a
, int b
)
447 u
.s
.low
= (unsigned)u
.s
.high
>> (b
- 32);
450 u
.s
.low
= ((unsigned)u
.s
.low
>> b
) | (u
.s
.high
<< (32 - b
));
451 u
.s
.high
= (unsigned)u
.s
.high
>> b
;
459 /* XXX: fix tcc's code generator to do this instead */
460 long long __ashldi3(long long a
, int b
)
466 u
.s
.high
= (unsigned)u
.s
.low
<< (b
- 32);
469 u
.s
.high
= ((unsigned)u
.s
.high
<< b
) | ((unsigned)u
.s
.low
>> (32 - b
));
470 u
.s
.low
= (unsigned)u
.s
.low
<< b
;
478 #if defined(__i386__)
479 /* FPU control word for rounding to nearest mode */
480 unsigned short __tcc_fpu_control
= 0x137f;
481 /* FPU control word for round to zero mode for int conversion */
482 unsigned short __tcc_int_fpu_control
= 0x137f | 0x0c00;
485 /* XXX: fix tcc's code generator to do this instead */
486 float __floatundisf(unsigned long long a
)
492 if (uu
.s
.high
>= 0) {
496 r
+= 18446744073709551616.0;
501 double __floatundidf(unsigned long long a
)
507 if (uu
.s
.high
>= 0) {
508 return (double)uu
.ll
;
511 r
+= 18446744073709551616.0;
516 long double __floatundixf(unsigned long long a
)
522 if (uu
.s
.high
>= 0) {
523 return (long double)uu
.ll
;
526 r
+= 18446744073709551616.0;
527 return (long double)r
;
531 unsigned long long __fixunssfdi (float a1
)
533 register union float_long fl1
;
535 register unsigned long l
;
542 exp
= EXP (fl1
.l
) - EXCESS
- 24;
546 return (unsigned long long)-1;
548 return (unsigned long long)l
<< exp
;
555 unsigned long long __fixunsdfdi (double a1
)
557 register union double_long dl1
;
559 register unsigned long long l
;
566 exp
= EXPD (dl1
) - EXCESSD
- 53;
571 return (unsigned long long)-1;
580 unsigned long long __fixunsxfdi (long double a1
)
582 register union ldouble_long dl1
;
584 register unsigned long long l
;
588 if (dl1
.l
.lower
== 0 && dl1
.l
.upper
== 0)
591 exp
= EXPLD (dl1
) - EXCESSLD
- 64;
596 return (unsigned long long)-1;