xref: /csrg-svn/lib/libc/vax/stdio.old/doprnt.c (revision 2041)
1 /* @(#)doprnt.c	4.1 (Berkeley) 12/21/80 */
2 	# C library -- conversions
3 
4 .globl	__doprnt
5 .globl	__flsbuf
6 
7 #define vbit 1
8 #define flags r10
9 #define ndfnd 0
10 #define prec 1
11 #define zfill 2
12 #define minsgn 3
13 #define plssgn 4
14 #define numsgn 5
15 #define caps 6
16 #define blank 7
17 #define gflag 8
18 #define dpflag 9
19 #define width r9
20 #define ndigit r8
21 #define llafx r7
22 #define lrafx r6
23 #define fdesc -4(fp)
24 #define exp -8(fp)
25 #define sexp -12(fp)
26 #define nchar -16(fp)
27 #define sign -17(fp)
28 	.set ch.zer,'0			# cpp doesn't like single appostrophes
29 
30 	.align 2
31 strtab:		# translate table for detecting null and percent
32 	.byte	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
33 	.byte	16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
34 	.byte	' ,'!,'",'#,'$, 0,'&,'','(,'),'*,'+,',,'-,'.,'/
35 	.byte	'0,'1,'2,'3,'4,'5,'6,'7,'8,'9,':,';,'<,'=,'>,'?
36 	.byte	'@,'A,'B,'C,'D,'E,'F,'G,'H,'I,'J,'K,'L,'M,'N,'O
37 	.byte	'P,'Q,'R,'S,'T,'U,'V,'W,'X,'Y,'Z,'[,'\,'],'^,'_
38 	.byte	'`,'a,'b,'c,'d,'e,'f,'g,'h,'i,'j,'k,'l,'m,'n,'o
39 	.byte	'p,'q,'r,'s,'t,'u,'v,'w,'x,'y,'z,'{,'|,'},'~,127
40 	.byte	128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143
41 	.byte	144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159
42 	.byte	160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175
43 	.byte	176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191
44 	.byte	192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207
45 	.byte	208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223
46 	.byte	224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239
47 	.byte	240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
48 
49 strfoo:
50 	clrl r4					# fix interrupt race
51 	jbr strok				# and try again
52 strmore:
53 	movzbl (r1)+,r2			# one char
54 	tstb (r3)[r2]			# translate
55 	jeql stresc2			# bad guy in disguise (outbuf is full)
56 strout2:		# enter here to force out r2; r0,r1 must be set
57 	pushr $3				# save input descriptor
58 	pushl fdesc				# FILE
59 	pushl r2				# the char
60 	calls $2,__flsbuf		# please empty the buffer and handle 1 char
61 	tstl r0					# successful?
62 	jgeq strm1				# yes
63 	jbcs $31,nchar,strm1	# turn on sign bit of nchar to signify error
64 strm1:
65 	incl nchar				# count the char
66 	popr $3					# get input descriptor back
67 strout:			# enter via bsb with (r0,r1)=input descriptor
68 	movab strtab,r3			# table address
69 	movq *fdesc,r4			# output descriptor
70 	jbs $31,r4,strfoo		# negative count is a no no
71 strok:
72 	addl2 r0,nchar			# we intend to move this many chars
73 	movtuc r0,(r1),$0,(r3),r4,(r5)
74 	movpsl r2				# squirrel away condition codes
75 	movq r4,*fdesc			# update output descriptor
76 	subl2 r0,nchar			# some chars not moved
77 	jbs $vbit,r2,stresc		# terminated by escape?
78 	sobgeq r0,strmore		# no; but out buffer might be full
79 stresc:
80 	rsb
81 stresc2:
82 	incl r0					# fix the length
83 	decl r1					# and the addr
84 	movl $1<vbit,r2			# fake condition codes
85 	rsb
86 
87 errdone:
88 	jbcs $31,nchar,prdone	# set error bit
89 prdone:
90 	movl nchar,r0
91 	ret
92 
93 	.align	1
94 __doprnt:
95 	.word	0xfc0			# uses r11-r6
96 	movab -256(sp),sp		# work space
97 	movl 4(ap),r11			# addr of format string
98 	movl 12(ap),fdesc		# output FILE ptr
99 	movl 8(ap),ap			# addr of first arg
100 	clrl nchar				# number of chars transferred
101 loop:
102 	movzwl $65535,r0		# pseudo length
103 	movl r11,r1				# fmt addr
104 	bsbw strout				# copy to output, stop at null or percent
105 	movl r1,r11				# new fmt
106 	jbc $vbit,r2,loop		# if no escape, then very long fmt
107 	tstb (r11)+				# escape; null or percent?
108 	jeql prdone				# null means end of fmt
109 
110 	movl sp,r5			# reset output buffer pointer
111 	clrq r9				# width; flags
112 	clrq r6				# lrafx,llafx
113 longorunsg:				# we can ignore both of these distinctions
114 short:
115 L4a:
116 	movzbl (r11)+,r0		# so capital letters can tail merge
117 L4:	caseb r0,$' ,$'x-' 		# format char
118 L5:
119 	.word space-L5			# space
120 	.word fmtbad-L5			# !
121 	.word fmtbad-L5			# "
122 	.word sharp-L5			# #
123 	.word fmtbad-L5			# $
124 	.word fmtbad-L5			# %
125 	.word fmtbad-L5			# &
126 	.word fmtbad-L5			# '
127 	.word fmtbad-L5			# (
128 	.word fmtbad-L5			# )
129 	.word indir-L5			# *
130 	.word plus-L5			# +
131 	.word fmtbad-L5			# ,
132 	.word minus-L5			# -
133 	.word dot-L5			# .
134 	.word fmtbad-L5			# /
135 	.word gnum0-L5			# 0
136 	.word gnum-L5			# 1
137 	.word gnum-L5			# 2
138 	.word gnum-L5			# 3
139 	.word gnum-L5			# 4
140 	.word gnum-L5			# 5
141 	.word gnum-L5			# 6
142 	.word gnum-L5			# 7
143 	.word gnum-L5			# 8
144 	.word gnum-L5			# 9
145 	.word fmtbad-L5			# :
146 	.word fmtbad-L5			# ;
147 	.word fmtbad-L5			# <
148 	.word fmtbad-L5			# =
149 	.word fmtbad-L5			# >
150 	.word fmtbad-L5			# ?
151 	.word fmtbad-L5			# @
152 	.word fmtbad-L5			# A
153 	.word fmtbad-L5			# B
154 	.word fmtbad-L5			# C
155 	.word decimal-L5		# D
156 	.word capital-L5		# E
157 	.word fmtbad-L5			# F
158 	.word capital-L5		# G
159 	.word fmtbad-L5			# H
160 	.word fmtbad-L5			# I
161 	.word fmtbad-L5			# J
162 	.word fmtbad-L5			# K
163 	.word fmtbad-L5			# L
164 	.word fmtbad-L5			# M
165 	.word fmtbad-L5			# N
166 	.word octal-L5			# O
167 	.word fmtbad-L5			# P
168 	.word fmtbad-L5			# Q
169 	.word fmtbad-L5			# R
170 	.word fmtbad-L5			# S
171 	.word fmtbad-L5			# T
172 	.word unsigned-L5		# U
173 	.word fmtbad-L5			# V
174 	.word fmtbad-L5			# W
175 	.word hex-L5			# X
176 	.word fmtbad-L5			# Y
177 	.word fmtbad-L5			# Z
178 	.word fmtbad-L5			# [
179 	.word fmtbad-L5			# \
180 	.word fmtbad-L5			# ]
181 	.word fmtbad-L5			# ^
182 	.word fmtbad-L5			# _
183 	.word fmtbad-L5			# `
184 	.word fmtbad-L5			# a
185 	.word fmtbad-L5			# b
186 	.word charac-L5			# c
187 	.word decimal-L5		# d
188 	.word scien-L5			# e
189 	.word float-L5			# f
190 	.word general-L5		# g
191 	.word short-L5			# h
192 	.word fmtbad-L5			# i
193 	.word fmtbad-L5			# j
194 	.word fmtbad-L5			# k
195 	.word longorunsg-L5		# l
196 	.word fmtbad-L5			# m
197 	.word fmtbad-L5			# n
198 	.word octal-L5			# o
199 	.word fmtbad-L5			# p
200 	.word fmtbad-L5			# q
201 	.word fmtbad-L5			# r
202 	.word string-L5			# s
203 	.word fmtbad-L5			# t
204 	.word unsigned-L5		# u
205 	.word fmtbad-L5			# v
206 	.word fmtbad-L5			# w
207 	.word hex-L5			# x
208 fmtbad:
209 	movb r0,(r5)+			# print the unfound character
210 	jeql errdone			# dumb users who end the format with a %
211 	jbr prbuf
212 capital:
213 	bisl2 $1<caps,flags		# note that it was capitalized
214 	xorb2 $'a^'A,r0			# make it small
215 	jbr L4					# and try again
216 
217 string:
218 	movl ndigit,r0
219 	jbs $prec,flags,L20		# max length was specified
220 	mnegl $1,r0			# default max length
221 L20:	movl (ap)+,r2			# addr first byte
222 	locc $0,r0,(r2)			# find the zero at the end
223 	movl r1,r5			# addr last byte +1
224 	movl r2,r1			# addr first byte
225 	jbr prstr
226 
227 htab:	.byte	'0,'1,'2,'3,'4,'5,'6,'7,'8,'9,'a,'b,'c,'d,'e,'f
228 Htab:	.byte	'0,'1,'2,'3,'4,'5,'6,'7,'8,'9,'A,'B,'C,'D,'E,'F
229 
230 octal:
231 	movl $30,r2			# init position
232 	movl $3,r3			# field width
233 	movab htab,llafx	# translate table
234 	jbr L10
235 
236 hex:
237 	movl $28,r2			# init position
238 	movl $4,r3			# field width
239 	movab htab,llafx	# translate table
240 	jbc $caps,flags,L10
241 	movab Htab,llafx
242 L10:	mnegl r3,r6			# increment
243 	clrl r1
244 	addl2 $4,r5			# room for left affix (2) and slop [forced sign?]
245 	movl (ap)+,r0			# fetch arg
246 L11:	extzv r2,r3,r0,r1		# pull out a digit
247 	movb (llafx)[r1],(r5)+		# convert to character
248 L12:	acbl $0,r6,r2,L11		# continue until done
249 	clrq r6				# lrafx, llafx
250 	clrb (r5)			# flag end
251 	skpc $'0,$11,4(sp)		# skip over leading zeroes
252 	jbc $numsgn,flags,prn3	# easy if no left affix
253 	tstl -4(ap)				# original value
254 	jeql prn3			# no affix on 0, for some reason
255 	cmpl r3,$4			# were we doing hex or octal?
256 	jneq L12a			# octal
257 	movb $'x,r0
258 	jbc $caps,flags,L12b
259 	movb $'X,r0
260 L12b:	movb r0,-(r1)
261 	movl $2,llafx		# leading 0x for hex is an affix
262 L12a:	movb $'0,-(r1)	# leading zero for octal is a digit, not an affix
263 	jbr prn3			# omit sign (plus, blank) massaging
264 
265 unsigned:
266 lunsigned:
267 	bicl2 $1<plssgn|1<blank,flags	# omit sign (plus, blank) massaging
268 	extzv $1,$31,(ap),r0		# right shift logical 1 bit
269 	cvtlp r0,$10,(sp)		# convert [n/2] to packed
270 	movp $10,(sp),8(sp)		# copy packed
271 	addp4 $10,8(sp),$10,(sp)	# 2*[n/2] in packed, at (sp)
272 	blbc (ap)+,L14			# n was even
273 	addp4 $1,pone,$10,(sp)		# n was odd
274 	jbr L14
275 
276 patdec:					# editpc pattern for decimal printing
277 	.byte 0xAA			# eo$float 10
278 	.byte 0x01			# eo$end_float
279 	.byte 0				# eo$end
280 
281 decimal:
282 	cvtlp (ap)+,$10,(sp)		# 10 digits max
283 	jgeq L14
284 	incl llafx			# minus sign is a left affix
285 L14:	editpc $10,(sp),patdec,8(sp)	# ascii at 8(sp); r5=end+1
286 	skpc $' ,$11,8(sp)		# skip leading blanks; r1=first
287 
288 prnum:			# r1=addr first byte, r5=addr last byte +1, llafx=size of signs
289 				# -1(r1) vacant, for forced sign
290 	tstl llafx
291 	jneq prn3			# already some left affix, dont fuss
292 	jbc $plssgn,flags,prn2
293 	movb $'+,-(r1)		# needs a plus sign
294 	jbr prn4
295 prn2:	jbc $blank,flags,prn3
296 	movb $' ,-(r1)		# needs a blank sign
297 prn4:	incl llafx
298 prn3:	jbs $prec,flags,prn1
299 	movl $1,ndigit		# default precision is 1
300 prn1:	subl3 r1,r5,lrafx	# raw width
301 	subl2 llafx,lrafx	# number of digits
302 	subl2 lrafx,ndigit	# number of leading zeroes needed
303 	jleq prstr			# none
304 	addl2 llafx,r1		# where current digits start
305 	pushl r1			# movcx gobbles registers
306 		# check bounds on users who say %.300d
307 	movab 32(r5)[ndigit],r2
308 	subl2 fp,r2
309 	jlss prn5
310 	subl2 r2,ndigit
311 prn5:
312 		#
313 	movc3 lrafx,(r1),(r1)[ndigit]	# make room in middle
314 	movc5 $0,(r1),$ch.zer,ndigit,*(sp)	# '0 fill
315 	subl3 llafx,(sp)+,r1	# first byte addr
316 	addl3 lrafx,r3,r5	# last byte addr +1
317 
318 prstr:			# r1=addr first byte; r5=addr last byte +1
319 				# width=minimum width; llafx=len. left affix
320 				# ndigit=<avail>
321 	subl3 r1,r5,ndigit		# raw width
322 	subl3 ndigit,width,r0	# pad length
323 	jleq padlno				# in particular, no left padding
324 	jbs $minsgn,flags,padlno
325 			# extension for %0 flag causing left zero padding to field width
326 	jbs $zfill,flags,padlz
327 			# this bsbb needed even if %0 flag extension is removed
328 	bsbb padb				# blank pad on left
329 	jbr padnlz
330 padlz:
331 	movl llafx,r0
332 	jleq padnlx				# left zero pad requires left affix first
333 	subl2 r0,ndigit			# part of total length will be transferred
334 	subl2 r0,width			# and will account for part of minimum width
335 	bsbw strout				# left affix
336 padnlx:
337 	subl3 ndigit,width,r0	# pad length
338 	bsbb padz				# zero pad on left
339 padnlz:
340 			# end of extension for left zero padding
341 padlno:			# remaining: root, possible right padding
342 	subl2 ndigit,width		# root reduces minimum width
343 	movl ndigit,r0			# root length
344 p1:	bsbw strout				# transfer to output buffer
345 p3:	jbc $vbit,r2,padnpct	# percent sign (or null byte via %c) ?
346 	decl r0					# yes; adjust count
347 	movzbl (r1)+,r2			# fetch byte
348 	movq *fdesc,r4			# output buffer descriptor
349 	sobgeq r4,p2			# room at the out [inn] ?
350 	bsbw strout2			# no; force it, then try rest
351 	jbr p3					# here we go 'round the mullberry bush, ...
352 p2:	movb r2,(r5)+			# hand-deposit the percent or null
353 	incl nchar				# count it
354 	movq r4,*fdesc			# store output descriptor
355 	jbr p1					# what an expensive hiccup!
356 padnpct:
357 	movl width,r0	# size of pad
358 	jleq loop
359 	bsbb padb
360 	jbr loop
361 
362 padz:
363 	movb $'0,r2
364 	jbr pad
365 padb:
366 	movb $' ,r2
367 pad:
368 	subl2 r0,width			# pad width decreases minimum width
369 	pushl r1				# save non-pad addr
370 	movl r0,llafx			# remember width of pad
371 	subl2 r0,sp				# allocate
372 	movc5 $0,(r0),r2,llafx,(sp)	# create pad string
373 	movl llafx,r0			# length
374 	movl sp,r1				# addr
375 	bsbw strout
376 	addl2 llafx,sp			# deallocate
377 	movl (sp)+,r1			# recover non-pad addr
378 	rsb
379 
380 pone:	.byte	0x1C			# packed 1
381 
382 charac:
383 	movl (ap)+,r0		# word containing the char
384 	movb r0,(r5)+		# one byte, that's all
385 
386 prbuf:
387 	movl sp,r1			# addr first byte
388 	jbr prstr
389 
390 space:	bisl2 $1<blank,flags		# constant width e fmt, no plus sign
391 	jbr L4a
392 sharp:	bisl2 $1<numsgn,flags		# 'self identifying', please
393 	jbr L4a
394 plus:	bisl2 $1<plssgn,flags		# always print sign for floats
395 	jbr L4a
396 minus:	bisl2 $1<minsgn,flags		# left justification, please
397 	jbr L4a
398 gnum0:	jbs $ndfnd,flags,gnum
399 	jbs $prec,flags,gnump		# ignore when reading precision
400 	bisl2 $1<zfill,flags		# leading zero fill, please
401 gnum:	jbs $prec,flags,gnump
402 	moval (width)[width],width	# width *= 5;
403 	movaw -ch.zer(r0)[width],width	# width = 2*witdh + r0 - '0';
404 	jbr gnumd
405 gnump:	moval (ndigit)[ndigit],ndigit	# ndigit *= 5;
406 	movaw -ch.zer(r0)[ndigit],ndigit # ndigit = 2*ndigit + r0 - '0';
407 gnumd:	bisl2 $1<ndfnd,flags		# digit seen
408 	jbr L4a
409 dot:	clrl ndigit			# start on the precision
410 	bisl2 $1<prec,flags
411 	bicl2 $1<ndfnd,flags
412 	jbr L4a
413 indir:
414 	jbs $prec,flags,in1
415 	movl (ap)+,width		# width specified by parameter
416 	jgeq gnumd
417 	xorl2 $1<minsgn,flags		# parameterized left adjustment
418 	mnegl width,width
419 	jbr gnumd
420 in1:
421 	movl (ap)+,ndigit		# precision specified by paratmeter
422 	jgeq gnumd
423 	mnegl ndigit,ndigit
424 	jbr gnumd
425 
426 float:
427 	jbs $prec,flags,float1
428 	movl $6,ndigit			# default # digits to right of decpt.
429 float1:	bsbw fltcvt
430 	addl3 exp,ndigit,r7
431 	movl r7,r6			# for later "underflow" checking
432 	bgeq fxplrd
433 	clrl r7				# poor programmer planning
434 fxplrd:	cmpl r7,$31			# expressible in packed decimal?
435 	bleq fnarro			# yes
436 	movl $31,r7
437 fnarro:	subl3 $17,r7,r0			# where to round
438 	ashp r0,$17,(sp),$5,r7,16(sp)	# do it
439 	bvc fnovfl
440 		# band-aid for microcode error (spurious overflow)
441 	#	clrl r0				# assume even length result
442 	#	jlbc r7,fleven			# right
443 	#	movl $4,r0			# odd length result
444 	#fleven:	cmpv r0,$4,16(sp),$0		# top digit zero iff true overflow
445 	#	bneq fnovfl
446 		# end band-aid
447 	aobleq $0,r6,fnovfl		# if "underflow" then jump
448 	movl r7,r0
449 	incl exp
450 	incl r7
451 	ashp r0,$1,pone,$0,r7,16(sp)
452 	ashl $-1,r7,r0			# displ to last byte
453 	bisb2 sign,16(sp)[r0]		# insert sign
454 fnovfl:
455 	movab 16(sp),r1		# packed source
456 	movl r7,r6		# packed length
457 	pushab prnum	# goto prnum after fall-through call to fedit
458 
459 
460 	# enter via bsb
461 	#	r1=addr of packed source
462 	#	   16(r1) used to unpack source
463 	#	   48(r1) used to construct pattern to unpack source
464 	#	   48(r1) used to hold result
465 	#	r6=length of packed source (destroyed)
466 	#	exp=# digits to left of decimal point (destroyed)
467 	#	ndigit=# digits to right of decimal point (destroyed)
468 	#	sign=1 if negative, 0 otherwise
469 	# stack will be used for work space for pattern and unpacked source
470 	# exits with
471 	#	r1=addr of punctuated result
472 	#	r5=addr of last byte +1
473 	#	llafx=1 if minus sign inserted, 0 otherwise
474 fedit:
475 	pushab 48(r1)			# save result addr
476 	movab 48(r1),r3			# pattern addr
477 	movb $0x03,(r3)+		# eo$set_signif
478 	movc5 $0,(r1),$0x91,r6,(r3)	# eo$move 1
479 	clrb (r3)				# eo$end
480 	editpc r6,(r1),48(r1),16(r1)	# unpack 'em all
481 	subl3 r6,r5,r1			# addr unpacked source
482 	movl (sp),r3			# punctuated output placed here
483 	clrl llafx
484 	jlbc sign,f1
485 	movb $'-,(r3)+		# negative
486 	incl llafx
487 f1:	movl exp,r0
488 	jgtr f2
489 	movb $'0,(r3)+		# must have digit before decimal point
490 	jbr f3
491 f2:	cmpl r0,r6			# limit on packed length
492 	jleq f4
493 	movl r6,r0
494 f4:	subl2 r0,r6			# eat some digits
495 	subl2 r0,exp		# from the exponent
496 	movc3 r0,(r1),(r3)	# (most of the) digits to left of decimal point
497 	movl exp,r0			# need any more?
498 	jleq f3
499 	movc5 $0,(r1),$'0,r0,(r3)	# '0 fill
500 f3:	movl ndigit,r0		# # digits to right of decimal point
501 	jgtr f5
502 	jbs $numsgn,flags,f5	# no decimal point unless forced
503 	jbcs $dpflag,flags,f6	# no decimal point
504 f5:	movb $'.,(r3)+		# the decimal point
505 f6:	mnegl exp,r0		# "leading" zeroes to right of decimal point
506 	jleq f9
507 	cmpl r0,ndigit		# cant exceed this many
508 	jleq fa
509 	movl ndigit,r0
510 fa:	subl2 r0,ndigit
511 	movc5 $0,(r1),$'0,r0,(r3)
512 f9:	movl ndigit,r0
513 	cmpl r0,r6			# limit on packed length
514 	jleq f7
515 	movl r6,r0
516 f7:	subl2 r0,ndigit		# eat some digits from the fraction
517 	movc3 r0,(r1),(r3)	# (most of the) digits to right of decimal point
518 	movl ndigit,r0			# need any more?
519 	jleq f8
520 		# check bounds on users who say %.300f
521 	movab 32(r3)[r0],r2
522 	subl2 fp,r2
523 	jlss fb
524 	subl2 r2,r0			# truncate, willy-nilly
525 	movl r0,ndigit		# and no more digits later, either
526 fb:
527 		#
528 	subl2 r0,ndigit		# eat some digits from the fraction
529 	movc5 $0,(r1),$'0,r0,(r3)	# '0 fill
530 f8:	movl r3,r5			# addr last byte +1
531 	popr $1<1			# [movl (sp)+,r1] addr first byte
532 	rsb
533 
534 patexp:	.byte	0x03			# eo$set_signif
535 	.byte	0x44,'e			# eo$insert 'e
536 	.byte	0x42,'+			# eo$load_plus '+
537 	.byte	0x04			# eo$store_sign
538 	.byte	0x92			# eo$move 2
539 	.byte	0			# eo$end
540 
541 scien:
542 	incl ndigit
543 	jbs $prec,flags,L23
544 	movl $7,ndigit
545 L23:	bsbw fltcvt			# get packed digits
546 	movl ndigit,r7
547 	cmpl r7,$31				# expressible in packed decimal?
548 	jleq snarro				# yes
549 	movl $31,r7
550 snarro:	subl3 $17,r7,r0		# rounding position
551 	ashp r0,$17,(sp),$5,r7,16(sp) # shift and round
552 	bvc snovfl
553 		# band-aid for microcode error (spurious overflow)
554 	#	clrl r0				# assume even length result
555 	#	jlbc ndigit,sceven		# right
556 	#	movl $4,r0			# odd length result
557 	#sceven:	cmpv r0,$4,16(sp),$0		# top digit zero iff true overflow
558 	#	bneq snovfl
559 		# end band-aid
560 	incl exp			# rounding overflowed to 100...
561 	subl3 $1,r7,r0
562 	ashp r0,$1,pone,$0,r7,16(sp)
563 	ashl $-1,r7,r0		# displ to last byte
564 	bisb2 sign,16(sp)[r0]		# insert sign
565 snovfl:
566 	jbs $gflag,flags,gfmt		# %g format
567 	movab 16(sp),r1
568 	bsbb eedit
569 eexp:
570 	movl r1,r6		# save fwa from destruction by cvtlp
571 	subl3 $1,sexp,r0	# 1P exponent
572 	cvtlp r0,$2,(sp)	# packed
573 	editpc $2,(sp),patexp,(r5)
574 	movl r6,r1		# fwa
575 	jbc $caps,flags,prnum
576 	xorb2 $'e^'E,-4(r5)
577 	jbr prnum
578 
579 eedit:
580 	movl r7,r6		# packed length
581 	decl ndigit		# 1 digit before decimal point
582 	movl exp,sexp	# save from destruction
583 	movl $1,exp		# and pretend
584 	jbr fedit
585 
586 gfmt:
587 	addl3 $3,exp,r0		# exp is 1 more than e
588 	jlss gfmte		# (e+1)+3<0, e+4<=-1, e<=-5
589 	subl2 $3,r0		# exp [==(e+1)]
590 	cmpl r0,ndigit
591 	jgtr gfmte		# e+1>n, e>=n
592 gfmtf:
593 	movl r7,r6
594 	subl2 r0,ndigit		# n-e-1
595 	movab 16(sp),r1
596 	bsbw fedit
597 g1:	jbs $numsgn,flags,g2
598 	jbs $dpflag,flags,g2	# dont strip if no decimal point
599 g3:	cmpb -(r5),$'0		# strip trailing zeroes
600 	jeql g3
601 	cmpb (r5),$'.		# and trailing decimal point
602 	jeql g2
603 	incl r5
604 g2:	jbc $gflag,flags,eexp
605 	jbr prnum
606 gfmte:
607 	movab 16(sp),r1		# packed source
608 	bsbw eedit
609 	jbsc $gflag,flags,g1	# gflag now means "use %f" [hence no exponent]
610 
611 general:
612 	jbs $prec,flags,gn1
613 	movl $6,ndigit		# default precision is 6 significant digits
614 gn1:	tstl ndigit		# cannot allow precision of 0
615 	jgtr gn2
616 	movl $1,ndigit		# change 0 to 1, willy-nilly
617 gn2:	jbcs $gflag,flags,L23
618 	jbr L23			# safety net
619 
620 	# convert double-floating at (ap) to 17-digit packed at (sp),
621 	# set 'sign' and 'exp', advance ap.
622 fltcvt:
623 	clrb sign
624 	movd (ap)+,r5
625 	jeql fzero
626 	bgtr fpos
627 	mnegd r5,r5
628 	incb sign
629 fpos:
630 	extzv $7,$8,r5,r2		# exponent of 2
631 	movab -0200(r2),r2		# unbias
632 	mull2 $59,r2			# 59/196: 3rd convergent continued frac of log10(2)
633 	jlss eneg
634 	movab 196(r2),r2
635 eneg:
636 	movab -98(r2),r2
637 	divl2 $196,r2
638 	bsbw expten
639 	cmpd r0,r5
640 	bgtr ceil
641 	incl r2
642 ceil:	movl r2,exp
643 	mnegl r2,r2
644 	cmpl r2,$29			# 10^(29+9) is all we can handle
645 	bleq getman
646 	muld2 ten16,r5
647 	subl2 $16,r2
648 getman:	addl2 $9,r2			# -ceil(log10(x)) + 9
649 	bsbb expten
650 	emodd r0,r4,r5,r0,r5		# (r0+r4)*r5; r0=int, r5=frac
651 fz1:	cvtlp r0,$9,16(sp)		# leading 9 digits
652 	ashp $8,$9,16(sp),$0,$17,4(sp)	# as top 9 of 17
653 	emodd ten8,$0,r5,r0,r5
654 	cvtlp r0,$8,16(sp)		# trailing 8 digits
655 		# if precision >= 17, must round here
656 	movl ndigit,r7			# so figure out what precision is
657 	pushab scien
658 	cmpl (sp)+,(sp)
659 	jleq gm1			# who called us?
660 	addl2 exp,r7			# float; adjust for exponent
661 gm1:	cmpl r7,$17
662 	jlss gm2
663 	cmpd r5,$0d0.5			# must round here; check fraction
664 	jlss gm2
665 	bisb2 $0x10,8+4(sp)		# increment l.s. digit
666 gm2:		# end of "round here" code
667 	addp4 $8,16(sp),$17,4(sp)	# combine leading and trailing
668 	bisb2 sign,12(sp)		# and insert sign
669 	rsb
670 fzero:	clrl r0
671 	movl $1,exp		# 0.000e+00 and 0.000 rather than 0.000e-01 and .000
672 	jbr fz1
673 
674 	.align 2
675 lsb: .long 0x00010000		# lsb in the crazy floating-point format
676 
677 	# return 10^r2 as a double float in r0||r1 and 8 extra bits of precision in r4
678 	# preserve r2, r5||r6
679 expten:
680 	movd $0d1.0,r0			# begin computing 10^exp10
681 	clrl r4				# bit counter
682 	movad ten1,r3			# table address
683 	tstl r2
684 	bgeq e10lp
685 	mnegl r2,r2			# get absolute value
686 	jbss $6,r2,e10lp		# flag as negative
687 e10lp:	jbc r4,r2,el1			# want this power?
688 	muld2 (r3),r0			# yes
689 el1:	addl2 $8,r3			# advance to next power
690 	aobleq $5,r4,e10lp		# through 10^32
691 	jbcc $6,r2,el2			# correct for negative exponent
692 	divd3 r0,$0d1.0,r0		# by taking reciprocal
693 	cmpl $28,r2
694 	jneq enm28
695 	addl2 lsb,r1			# 10**-28 needs lsb incremented
696 enm28:	mnegl r2,r2			# original exponent of 10
697 el2:	addl3 $5*8,r2,r3		# negative bit positions are illegal?
698 	jbc r3,xlsbh-5,eoklsb
699 	subl2 lsb,r1			# lsb was too high
700 eoklsb:
701 	movzbl xprec[r2],r4		# 8 extra bits
702 	rsb
703 
704 	# powers of ten
705 	.align	2
706 ten1:	.word	0x4220,0,0,0
707 ten2:	.word	0x43c8,0,0,0
708 ten4:	.word	0x471c,0x4000,0,0
709 ten8:	.word	0x4dbe,0xbc20,0,0
710 ten16:	.word	0x5b0e,0x1bc9,0xbf04,0
711 ten32:	.word	0x759d,0xc5ad,0xa82b,0x70b6
712 
713 	# whether lsb is too high or not
714 	.byte 1:0,1:0,1:0,1:0,1:1,1:0,1:1,1:0	# -40 thru -33
715 	.byte 1:0,1:1,1:0,1:0,1:0,1:0,1:1,1:0	# -32 thru -25
716 	.byte 1:0,1:0,1:1,1:1,1:1,1:1,1:0,1:0	# -24 thru -17
717 	.byte 1:0,1:1,1:0,1:0,1:1,1:1,1:1,1:1	# -16 thru -9
718 	.byte 1:1,1:1,1:1,1:0,1:0,1:0,1:0,1:1	# -8  thru -1
719 xlsbh:
720 	.byte 1:0,1:0,1:0,1:0,1:0,1:0,1:0,1:0	# 0 thru 7
721 	.byte 1:0,1:0,1:0,1:0,1:0,1:0,1:0,1:0	# 8 thru 15
722 	.byte 1:0,1:0,1:0,1:0,1:0,1:0,1:0,1:0	# 16 thru 23
723 	.byte 1:0,1:1,1:1,1:0,1:1,1:1,1:1,1:1	# 24 thru 31
724 	.byte 1:1,1:1,1:1,1:1,1:1,1:1,1:1    	# 32 thru 38
725 
726 	# bytes of extra precision
727 	.byte           0x56,0x76,0xd3,0x88,0xb5,0x62	# -38 thru -33
728 	.byte 0xba,0xf5,0x32,0x3e,0x0e,0x48,0xdb,0x51	# -32 thru -25
729 	.byte 0x53,0x27,0xb1,0xef,0xeb,0xa5,0x07,0x49	# -24 thru -17
730 	.byte 0x5b,0xd9,0x0f,0x13,0xcd,0xff,0xbf,0x97	# -16 thru -9
731 	.byte 0xfd,0xbc,0xb6,0x23,0x2c,0x3b,0x0a,0xcd	# -8  thru -1
732 xprec:
733 	.byte 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00	# 0  thru 7
734 	.byte 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00	# 8  thru 15
735 	.byte 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00	# 16 thru 23
736 	.byte 0x00,0xa0,0xc8,0x3a,0x84,0xe4,0xdc,0x92	# 24 thru 31
737 	.byte 0x9b,0x00,0xc0,0x58,0xae,0x18,0xef     	# 32 thru 38
738