|
|
1.1 root 1: /*
2: * Copyright (c) 1980 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: .asciz "@(#)atof.s 5.6 (Berkeley) 6/1/90"
22: #endif /* LIBC_SCCS and not lint */
23:
24: #include "DEFS.h"
25:
26: /*
27: * atof: convert ascii to floating
28: *
29: * C usage:
30: *
31: * double atof (s)
32: * char *s;
33: *
34: * Register usage:
35: *
36: * r0-1: value being developed
37: * r2: first section: pointer to the next character
38: * second section: binary exponent
39: * r3: flags
40: * r4: first section: the current character
41: * second section: scratch
42: * r5: the decimal exponent
43: * r6-7: scratch
44: */
45: .set msign,0 # mantissa has negative sign
46: .set esign,1 # exponent has negative sign
47: .set decpt,2 # decimal point encountered
48:
49: ENTRY(atof, R6|R7)
50: /*
51: * Initialization
52: */
53: clrl r3 # All flags start out false
54: movl 4(ap),r2 # Address the first character
55: clrl r5 # Clear starting exponent
56: /*
57: * Skip leading white space
58: */
59: sk0: movzbl (r2)+,r4 # Fetch the next (first) character
60: cmpb $' ,r4 # Is it blank?
61: jeql sk0 # ...yes
62: cmpb r4,$8 # 8 is lowest of white-space group
63: jlss sk1 # Jump if char too low to be white space
64: cmpb r4,$13 # 13 is highest of white-space group
65: jleq sk0 # Jump if character is white space
66: sk1:
67: /*
68: * Check for a sign
69: */
70: cmpb $'+,r4 # Positive sign?
71: jeql cs1 # ... yes
72: cmpb $'-,r4 # Negative sign?
73: jneq cs2 # ... no
74: bisb2 $1<msign,r3 # Indicate a negative mantissa
75: cs1: movzbl (r2)+,r4 # Skip the character
76: cs2:
77: /*
78: * Accumulate digits, keeping track of the exponent
79: */
80: clrq r0 # Clear the accumulator
81: ad0: cmpb r4,$'0 # Do we have a digit?
82: jlss ad4 # ... no, too small
83: cmpb r4,$'9
84: jgtr ad4 # ... no, too large
85: /*
86: * We got a digit. Accumulate it
87: */
88: cmpl r1,$214748364 # Would this digit cause overflow?
89: jgeq ad1 # ... yes
90: /*
91: * Multiply (r0,r1) by 10. This is done by developing
92: * (r0,r1)*2 in (r6,r7), shifting (r0,r1) left three bits,
93: * and adding the two quadwords.
94: */
95: ashq $1,r0,r6 # (r6,r7)=(r0,r1)*2
96: ashq $3,r0,r0 # (r0,r1)=(r0,r1)*8
97: addl2 r6,r0 # Add low halves
98: adwc r7,r1 # Add high halves
99: /*
100: * Add in the digit
101: */
102: subl2 $'0,r4 # Get the digit value
103: addl2 r4,r0 # Add it into the accumulator
104: adwc $0,r1 # Possible carry into high half
105: jbr ad2 # Join common code
106: /*
107: * Here when the digit won't fit in the accumulator
108: */
109: ad1: incl r5 # Ignore the digit, bump exponent
110: /*
111: * If we have seen a decimal point, decrease the exponent by 1
112: */
113: ad2: jbc $decpt,r3,ad3 # Jump if decimal point not seen
114: decl r5 # Decrease exponent
115: ad3:
116: /*
117: * Fetch the next character, back for more
118: */
119: movzbl (r2)+,r4 # Fetch
120: jbr ad0 # Try again
121: /*
122: * Not a digit. Could it be a decimal point?
123: */
124: ad4: cmpb r4,$'. # If it's not a decimal point, either it's
125: jneq ad5 # the end of the number or the start of
126: # the exponent.
127: jbcs $decpt,r3,ad3 # If it IS a decimal point, we record that
128: # we've seen one, and keep collecting
129: # digits if it is the first one.
130: /*
131: * Check for an exponent
132: */
133: ad5: clrl r6 # Initialize the exponent accumulator
134:
135: cmpb r4,$'e # We allow both lower case e
136: jeql ex1 # ... and ...
137: cmpb r4,$'E # upper-case E
138: jneq ex7
139: /*
140: * Does the exponent have a sign?
141: */
142: ex1: movzbl (r2)+,r4 # Get next character
143: cmpb r4,$'+ # Positive sign?
144: jeql ex2 # ... yes ...
145: cmpb r4,$'- # Negative sign?
146: jneq ex3 # ... no ...
147: bisb2 $1<esign,r3 # Indicate exponent is negative
148: ex2: movzbl (r2)+,r4 # Grab the next character
149: /*
150: * Accumulate exponent digits in r6
151: */
152: ex3: cmpb r4,$'0 # A digit is within the range
153: jlss ex4 # '0' through
154: cmpb r4,$'9 # '9',
155: jgtr ex4 # inclusive.
156: cmpl r6,$214748364 # Exponent outrageously large already?
157: jgeq ex2 # ... yes
158: moval (r6)[r6],r6 # r6 *= 5
159: movaw -'0(r4)[r6],r6 # r6 = r6 * 2 + r4 - '0'
160: jbr ex2 # Go 'round again
161: ex4:
162: /*
163: * Now get the final exponent and force it within a reasonable
164: * range so our scaling loops don't take forever for values
165: * that will ultimately cause overflow or underflow anyway.
166: * A tight check on over/underflow will be done by ldexp.
167: */
168: jbc $esign,r3,ex5 # Jump if exponent not negative
169: mnegl r6,r6 # If sign, negate exponent
170: ex5: addl2 r6,r5 # Add given exponent to calculated exponent
171: cmpl r5,$-100 # Absurdly small?
172: jgtr ex6 # ... no
173: movl $-100,r5 # ... yes, force within limit
174: ex6: cmpl r5,$100 # Absurdly large?
175: jlss ex7 # ... no
176: movl $100,r5 # ... yes, force within bounds
177: ex7:
178: /*
179: * Our number has now been reduced to a mantissa and an exponent.
180: * The mantissa is a 63-bit positive binary integer in r0,r1,
181: * and the exponent is a signed power of 10 in r5. The msign
182: * bit in r3 will be on if the mantissa should ultimately be
183: * considered negative.
184: *
185: * We now have to convert it to a standard format floating point
186: * number. This will be done by accumulating a binary exponent
187: * in r2, as we progressively get r5 closer to zero.
188: *
189: * Don't bother scaling if the mantissa is zero
190: */
191: movq r0,r0 # Mantissa zero?
192: jeql exit # ... yes
193:
194: clrl r2 # Initialize binary exponent
195: tstl r5 # Which way to scale?
196: jleq sd0 # Scale down if decimal exponent <= 0
197: /*
198: * Scale up by "multiplying" r0,r1 by 10 as many times as necessary,
199: * as follows:
200: *
201: * Step 1: Shift r0,r1 right as necessary to ensure that no
202: * overflow can occur when multiplying.
203: */
204: su0: cmpl r1,$429496729 # Compare high word to (2**31)/5
205: jlss su1 # Jump out if guaranteed safe
206: ashq $-1,r0,r0 # Else shift right one bit
207: incl r2 # bump exponent to compensate
208: jbr su0 # and go back to test again.
209: /*
210: * Step 2: Multiply r0,r1 by 5, by appropriate shifting and
211: * double-precision addition
212: */
213: su1: ashq $2,r0,r6 # (r6,r7) := (r0,r1) * 4
214: addl2 r6,r0 # Add low-order halves
215: adwc r7,r1 # and high-order halves
216: /*
217: * Step 3: Increment the binary exponent to take care of the final
218: * factor of 2, and go back if we still need to scale more.
219: */
220: incl r2 # Increment the exponent
221: sobgtr r5,su0 # and back for more (maybe)
222:
223: jbr cm0 # Merge to build final value
224:
225: /*
226: * Scale down. We must "divide" r0,r1 by 10 as many times
227: * as needed, as follows:
228: *
229: * Step 0: Right now, the condition codes reflect the state
230: * of r5. If it's zero, we are done.
231: */
232: sd0: jeql cm0 # If finished, build final number
233: /*
234: * Step 1: Shift r0,r1 left until the high-order bit (not counting
235: * the sign bit) is nonzero, so that the division will preserve
236: * as much precision as possible.
237: */
238: tstl r1 # Is the entire high-order half zero?
239: jneq sd2 # ...no, go shift one bit at a time
240: ashq $30,r0,r0 # ...yes, shift left 30,
241: subl2 $30,r2 # decrement the exponent to compensate,
242: # and now it's known to be safe to shift
243: # at least once more.
244: sd1: ashq $1,r0,r0 # Shift (r0,r1) left one, and
245: decl r2 # decrement the exponent to compensate
246: sd2: jbc $30,r1,sd1 # If the high-order bit is off, go shift
247: /*
248: * Step 2: Divide the high-order part of (r0,r1) by 5,
249: * giving a quotient in r1 and a remainder in r7.
250: */
251: sd3: movl r1,r6 # Copy the high-order part
252: clrl r7 # Zero-extend to 64 bits
253: ediv $5,r6,r1,r7 # Divide (cannot overflow)
254: /*
255: * Step 3: Divide the low-order part of (r0,r1) by 5,
256: * using the remainder from step 2 for rounding.
257: * Note that the result of this computation is unsigned,
258: * so we have to allow for the fact that an ordinary division
259: * by 5 could overflow. We make allowance by dividing by 10,
260: * multiplying the quotient by 2, and using the remainder
261: * to adjust the modified quotient.
262: */
263: addl3 $2,r0,r6 # Dividend is low part of (r0,r1) plus
264: adwc $0,r7 # 2 for rounding plus
265: # (2**32) * previous remainder
266: ediv $10,r6,r0,r6 # r0 := quotient, r6 := remainder.
267: addl2 r0,r0 # Make r0 result of dividing by 5
268: cmpl r6,$5 # If remainder is 5 or greater,
269: jlss sd4 # increment the adjustted quotient.
270: incl r0
271: /*
272: * Step 4: Increment the decimal exponent, decrement the binary
273: * exponent (to make the division by 5 into a division by 10),
274: * and back for another iteration.
275: */
276: sd4: decl r2 # Binary exponent
277: aoblss $0,r5,sd2
278: /*
279: * We now have the following:
280: *
281: * r0: low-order half of a 64-bit integer
282: * r1: high-order half of the same 64-bit integer
283: * r2: a binary exponent
284: *
285: * Our final result is the integer represented by (r0,r1)
286: * multiplied by 2 to the power contained in r2.
287: * We will transform (r0,r1) into a floating-point value,
288: * set the sign appropriately, and let ldexp do the
289: * rest of the work.
290: *
291: * Step 1: if the high-order bit (excluding the sign) of
292: * the high-order half (r1) is 1, then we have 63 bits of
293: * fraction, too many to convert easily. However, we also
294: * know we won't need them all, so we will just throw the
295: * low-order bit away (and adjust the exponent appropriately).
296: */
297: cm0: jbc $30,r1,cm1 # jump if no adjustment needed
298: ashq $-1,r0,r0 # lose the low-order bit
299: incl r2 # increase the exponent to compensate
300: /*
301: * Step 2: split the 62-bit number in (r0,r1) into two
302: * 31-bit positive quantities
303: */
304: cm1: ashq $1,r0,r0 # put the high-order bits in r1
305: # and a 0 in the bottom of r0
306: rotl $-1,r0,r0 # right-justify the bits in r0
307: # moving the 0 from the ashq
308: # into the sign bit.
309: /*
310: * Step 3: convert both halves to floating point
311: */
312: cvtld r0,r6 # low-order part in r6-r7
313: cvtld r1,r0 # high-order part in r0-r1
314: /*
315: * Step 4: multiply the high order part by 2**31 and combine them
316: */
317: muld2 two31,r0 # multiply
318: addd2 r6,r0 # combine
319: /*
320: * Step 5: if appropriate, negate the floating value
321: */
322: jbc $msign,r3,cm2 # Jump if mantissa not signed
323: mnegd r0,r0 # If negative, make it so
324: /*
325: * Step 6: call ldexp to complete the job
326: */
327: cm2: pushl r2 # Put exponent in parameter list
328: movd r0,-(sp) # and also mantissa
329: calls $3,_ldexp # go combine them
330:
331: exit:
332: ret
333:
334: .align 2
335: two31: .word 0x5000 # 2 ** 31
336: .word 0 # (=2147483648)
337: .word 0 # in floating-point
338: .word 0 # (so atof doesn't have to convert it)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.