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