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