]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/float.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* float.c floating-point constant support for the Netwide Assembler
26 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
27 * Julian Hall. All rights reserved. The software is
28 * redistributable under the licence given in the file "Licence"
29 * distributed in the NASM archive.
31 * initial version 13/ix/96 by Simon Tatham
43 #define MANT_WORDS 6 /* 64 bits + 32 for accuracy == 96 */
44 #define MANT_DIGITS 28 /* 29 digits don't fit in 96 bits */
47 * guaranteed top bit of from is set
48 * => we only have to worry about _one_ bit shift to the left
51 static int multiply(unsigned short *to
, unsigned short *from
) {
52 unsigned long temp
[MANT_WORDS
*2];
55 for (i
=0; i
<MANT_WORDS
*2; i
++)
58 for (i
=0; i
<MANT_WORDS
; i
++)
59 for (j
=0; j
<MANT_WORDS
; j
++) {
61 n
= (unsigned long)to
[i
] * (unsigned long)from
[j
];
63 temp
[i
+j
+1] += n
& 0xFFFF;
66 for (i
=MANT_WORDS
*2; --i
;) {
67 temp
[i
-1] += temp
[i
] >> 16;
70 if (temp
[0] & 0x8000) {
71 for (i
=0; i
<MANT_WORDS
; i
++)
72 to
[i
] = temp
[i
] & 0xFFFF;
75 for (i
=0; i
<MANT_WORDS
; i
++)
76 to
[i
] = (temp
[i
] << 1) + !!(temp
[i
+1] & 0x8000);
81 static void flconvert(char *string
, unsigned short *mant
, long *exponent
,
83 char digits
[MANT_DIGITS
], *p
, *q
, *r
;
84 unsigned short mult
[MANT_WORDS
], *m
, bit
;
86 int extratwos
, started
, seendot
;
90 started
= seendot
= FALSE
;
91 while (*string
&& *string
!= 'E' && *string
!= 'e') {
97 "too many periods in floating-point constant");
100 } else if (*string
>= '0' && *string
<= '9') {
101 if (*string
== '0' && !started
) {
106 if (p
< digits
+sizeof(digits
))
107 *p
++ = *string
- '0';
113 "floating-point constant: `%c' is invalid character",
120 string
++; /* eat the E */
121 tenpwr
+= atoi(string
);
125 * At this point, the memory interval [digits,p) contains a
126 * series of decimal digits zzzzzzz such that our number X
129 * X = 0.zzzzzzz * 10^tenpwr
133 for (m
=mant
; m
<mant
+MANT_WORDS
; m
++)
139 while (m
< mant
+MANT_WORDS
) {
140 unsigned short carry
= 0;
141 while (p
> q
&& !p
[-1])
145 for (r
= p
; r
-- > q
;) {
156 *m
|= bit
, started
= TRUE
;
168 * At this point the `mant' array contains the first six
169 * fractional places of a base-2^16 real number, which when
170 * multiplied by 2^twopwr and 5^tenpwr gives X. So now we
171 * really do multiply by 5^tenpwr.
175 for (m
=mult
; m
<mult
+MANT_WORDS
; m
++)
179 } else if (tenpwr
> 0) {
181 for (m
=mult
+1; m
<mult
+MANT_WORDS
; m
++)
188 twopwr
+= extratwos
+ multiply (mant
, mult
);
189 extratwos
= extratwos
* 2 + multiply (mult
, mult
);
194 * Conversion is done. The elements of `mant' contain the first
195 * fractional places of a base-2^16 real number in [0.5,1)
196 * which we can multiply by 2^twopwr to get X. Or, of course,
203 * Shift a mantissa to the right by i (i < 16) bits.
205 static void shr(unsigned short *mant
, int i
) {
206 unsigned short n
= 0, m
;
209 for (j
=0; j
<MANT_WORDS
; j
++) {
210 m
= (mant
[j
] << (16-i
)) & 0xFFFF;
211 mant
[j
] = (mant
[j
] >> i
) | n
;
217 * Round a mantissa off after i words.
219 static int round(unsigned short *mant
, int i
) {
220 if (mant
[i
] & 0x8000) {
224 } while (i
> 0 && !mant
[i
]);
225 return !i
&& !mant
[i
];
230 #define put(a,b) ( (*(a)=(b)), ((a)[1]=(b)>>8) )
232 static int to_double(char *str
, long sign
, unsigned char *result
,
234 unsigned short mant
[MANT_WORDS
];
237 sign
= (sign
< 0 ? 0x8000L
: 0L);
239 flconvert (str
, mant
, &exponent
, error
);
240 if (mant
[0] & 0x8000) {
245 if (exponent
>= -1022 && exponent
<= 1024) {
252 if (mant
[0] & 0x20) /* did we scale up by one? */
253 shr(mant
, 1), exponent
++;
254 mant
[0] &= 0xF; /* remove leading one */
255 put(result
+6,(exponent
<< 4) | mant
[0] | sign
);
256 put(result
+4,mant
[1]);
257 put(result
+2,mant
[2]);
258 put(result
+0,mant
[3]);
259 } else if (exponent
< -1022 && exponent
>= -1074) {
263 int shift
= -(exponent
+1011);
264 int sh
= shift
% 16, wds
= shift
/ 16;
266 if (round(mant
, 4-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
272 put(result
+6,(wds
== 0 ? mant
[0] : 0) | sign
);
273 put(result
+4,(wds
<= 1 ? mant
[1-wds
] : 0));
274 put(result
+2,(wds
<= 2 ? mant
[2-wds
] : 0));
275 put(result
+0,(wds
<= 3 ? mant
[3-wds
] : 0));
278 error(ERR_NONFATAL
, "overflow in floating-point constant");
281 memset (result
, 0, 8);
287 memset (result
, 0, 8);
289 return 1; /* success */
292 static int to_float(char *str
, long sign
, unsigned char *result
,
294 unsigned short mant
[MANT_WORDS
];
297 sign
= (sign
< 0 ? 0x8000L
: 0L);
299 flconvert (str
, mant
, &exponent
, error
);
300 if (mant
[0] & 0x8000) {
305 if (exponent
>= -126 && exponent
<= 128) {
312 if (mant
[0] & 0x100) /* did we scale up by one? */
313 shr(mant
, 1), exponent
++;
314 mant
[0] &= 0x7F; /* remove leading one */
315 put(result
+2,(exponent
<< 7) | mant
[0] | sign
);
316 put(result
+0,mant
[1]);
317 } else if (exponent
< -126 && exponent
>= -149) {
321 int shift
= -(exponent
+118);
322 int sh
= shift
% 16, wds
= shift
/ 16;
324 if (round(mant
, 2-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
330 put(result
+2,(wds
== 0 ? mant
[0] : 0) | sign
);
331 put(result
+0,(wds
<= 1 ? mant
[1-wds
] : 0));
334 error(ERR_NONFATAL
, "overflow in floating-point constant");
337 memset (result
, 0, 4);
340 memset (result
, 0, 4);
345 static int to_ldoub(char *str
, long sign
, unsigned char *result
,
347 unsigned short mant
[MANT_WORDS
];
350 sign
= (sign
< 0 ? 0x8000L
: 0L);
352 flconvert (str
, mant
, &exponent
, error
);
353 if (mant
[0] & 0x8000) {
358 if (exponent
>= -16383 && exponent
<= 16384) {
363 if (round(mant
, 4)) /* did we scale up by one? */
364 shr(mant
, 1), mant
[0] |= 0x8000, exponent
++;
365 put(result
+8,exponent
| sign
);
366 put(result
+6,mant
[0]);
367 put(result
+4,mant
[1]);
368 put(result
+2,mant
[2]);
369 put(result
+0,mant
[3]);
370 } else if (exponent
< -16383 && exponent
>= -16446) {
374 int shift
= -(exponent
+16383);
375 int sh
= shift
% 16, wds
= shift
/ 16;
377 if (round(mant
, 4-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
384 put(result
+6,(wds
== 0 ? mant
[0] : 0));
385 put(result
+4,(wds
<= 1 ? mant
[1-wds
] : 0));
386 put(result
+2,(wds
<= 2 ? mant
[2-wds
] : 0));
387 put(result
+0,(wds
<= 3 ? mant
[3-wds
] : 0));
390 error(ERR_NONFATAL
, "overflow in floating-point constant");
393 memset (result
, 0, 10);
399 memset (result
, 0, 10);
404 int float_const (char *number
, long sign
, unsigned char *result
, int bytes
,
407 return to_float (number
, sign
, result
, error
);
409 return to_double (number
, sign
, result
, error
);
410 else if (bytes
== 10)
411 return to_ldoub (number
, sign
, result
, error
);
413 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);