decbin.S 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. |
  2. | decbin.sa 3.3 12/19/90
  3. |
  4. | Description: Converts normalized packed bcd value pointed to by
  5. | register A6 to extended-precision value in FP0.
  6. |
  7. | Input: Normalized packed bcd value in ETEMP(a6).
  8. |
  9. | Output: Exact floating-point representation of the packed bcd value.
  10. |
  11. | Saves and Modifies: D2-D5
  12. |
  13. | Speed: The program decbin takes ??? cycles to execute.
  14. |
  15. | Object Size:
  16. |
  17. | External Reference(s): None.
  18. |
  19. | Algorithm:
  20. | Expected is a normal bcd (i.e. non-exceptional; all inf, zero,
  21. | and NaN operands are dispatched without entering this routine)
  22. | value in 68881/882 format at location ETEMP(A6).
  23. |
  24. | A1. Convert the bcd exponent to binary by successive adds and muls.
  25. | Set the sign according to SE. Subtract 16 to compensate
  26. | for the mantissa which is to be interpreted as 17 integer
  27. | digits, rather than 1 integer and 16 fraction digits.
  28. | Note: this operation can never overflow.
  29. |
  30. | A2. Convert the bcd mantissa to binary by successive
  31. | adds and muls in FP0. Set the sign according to SM.
  32. | The mantissa digits will be converted with the decimal point
  33. | assumed following the least-significant digit.
  34. | Note: this operation can never overflow.
  35. |
  36. | A3. Count the number of leading/trailing zeros in the
  37. | bcd string. If SE is positive, count the leading zeros;
  38. | if negative, count the trailing zeros. Set the adjusted
  39. | exponent equal to the exponent from A1 and the zero count
  40. | added if SM = 1 and subtracted if SM = 0. Scale the
  41. | mantissa the equivalent of forcing in the bcd value:
  42. |
  43. | SM = 0 a non-zero digit in the integer position
  44. | SM = 1 a non-zero digit in Mant0, lsd of the fraction
  45. |
  46. | this will insure that any value, regardless of its
  47. | representation (ex. 0.1E2, 1E1, 10E0, 100E-1), is converted
  48. | consistently.
  49. |
  50. | A4. Calculate the factor 10^exp in FP1 using a table of
  51. | 10^(2^n) values. To reduce the error in forming factors
  52. | greater than 10^27, a directed rounding scheme is used with
  53. | tables rounded to RN, RM, and RP, according to the table
  54. | in the comments of the pwrten section.
  55. |
  56. | A5. Form the final binary number by scaling the mantissa by
  57. | the exponent factor. This is done by multiplying the
  58. | mantissa in FP0 by the factor in FP1 if the adjusted
  59. | exponent sign is positive, and dividing FP0 by FP1 if
  60. | it is negative.
  61. |
  62. | Clean up and return. Check if the final mul or div resulted
  63. | in an inex2 exception. If so, set inex1 in the fpsr and
  64. | check if the inex1 exception is enabled. If so, set d7 upper
  65. | word to $0100. This will signal unimp.sa that an enabled inex1
  66. | exception occurred. Unimp will fix the stack.
  67. |
  68. | Copyright (C) Motorola, Inc. 1990
  69. | All Rights Reserved
  70. |
  71. | For details on the license for this file, please see the
  72. | file, README, in this same directory.
  73. |DECBIN idnt 2,1 | Motorola 040 Floating Point Software Package
  74. |section 8
  75. #include "fpsp.h"
  76. |
  77. | PTENRN, PTENRM, and PTENRP are arrays of powers of 10 rounded
  78. | to nearest, minus, and plus, respectively. The tables include
  79. | 10**{1,2,4,8,16,32,64,128,256,512,1024,2048,4096}. No rounding
  80. | is required until the power is greater than 27, however, all
  81. | tables include the first 5 for ease of indexing.
  82. |
  83. |xref PTENRN
  84. |xref PTENRM
  85. |xref PTENRP
  86. RTABLE: .byte 0,0,0,0
  87. .byte 2,3,2,3
  88. .byte 2,3,3,2
  89. .byte 3,2,2,3
  90. .global decbin
  91. .global calc_e
  92. .global pwrten
  93. .global calc_m
  94. .global norm
  95. .global ap_st_z
  96. .global ap_st_n
  97. |
  98. .set FNIBS,7
  99. .set FSTRT,0
  100. |
  101. .set ESTRT,4
  102. .set EDIGITS,2 |
  103. |
  104. | Constants in single precision
  105. FZERO: .long 0x00000000
  106. FONE: .long 0x3F800000
  107. FTEN: .long 0x41200000
  108. .set TEN,10
  109. |
  110. decbin:
  111. | fmovel #0,FPCR ;clr real fpcr
  112. moveml %d2-%d5,-(%a7)
  113. |
  114. | Calculate exponent:
  115. | 1. Copy bcd value in memory for use as a working copy.
  116. | 2. Calculate absolute value of exponent in d1 by mul and add.
  117. | 3. Correct for exponent sign.
  118. | 4. Subtract 16 to compensate for interpreting the mant as all integer digits.
  119. | (i.e., all digits assumed left of the decimal point.)
  120. |
  121. | Register usage:
  122. |
  123. | calc_e:
  124. | (*) d0: temp digit storage
  125. | (*) d1: accumulator for binary exponent
  126. | (*) d2: digit count
  127. | (*) d3: offset pointer
  128. | ( ) d4: first word of bcd
  129. | ( ) a0: pointer to working bcd value
  130. | ( ) a6: pointer to original bcd value
  131. | (*) FP_SCR1: working copy of original bcd value
  132. | (*) L_SCR1: copy of original exponent word
  133. |
  134. calc_e:
  135. movel #EDIGITS,%d2 |# of nibbles (digits) in fraction part
  136. moveql #ESTRT,%d3 |counter to pick up digits
  137. leal FP_SCR1(%a6),%a0 |load tmp bcd storage address
  138. movel ETEMP(%a6),(%a0) |save input bcd value
  139. movel ETEMP_HI(%a6),4(%a0) |save words 2 and 3
  140. movel ETEMP_LO(%a6),8(%a0) |and work with these
  141. movel (%a0),%d4 |get first word of bcd
  142. clrl %d1 |zero d1 for accumulator
  143. e_gd:
  144. mulul #TEN,%d1 |mul partial product by one digit place
  145. bfextu %d4{%d3:#4},%d0 |get the digit and zero extend into d0
  146. addl %d0,%d1 |d1 = d1 + d0
  147. addqb #4,%d3 |advance d3 to the next digit
  148. dbf %d2,e_gd |if we have used all 3 digits, exit loop
  149. btst #30,%d4 |get SE
  150. beqs e_pos |don't negate if pos
  151. negl %d1 |negate before subtracting
  152. e_pos:
  153. subl #16,%d1 |sub to compensate for shift of mant
  154. bges e_save |if still pos, do not neg
  155. negl %d1 |now negative, make pos and set SE
  156. orl #0x40000000,%d4 |set SE in d4,
  157. orl #0x40000000,(%a0) |and in working bcd
  158. e_save:
  159. movel %d1,L_SCR1(%a6) |save exp in memory
  160. |
  161. |
  162. | Calculate mantissa:
  163. | 1. Calculate absolute value of mantissa in fp0 by mul and add.
  164. | 2. Correct for mantissa sign.
  165. | (i.e., all digits assumed left of the decimal point.)
  166. |
  167. | Register usage:
  168. |
  169. | calc_m:
  170. | (*) d0: temp digit storage
  171. | (*) d1: lword counter
  172. | (*) d2: digit count
  173. | (*) d3: offset pointer
  174. | ( ) d4: words 2 and 3 of bcd
  175. | ( ) a0: pointer to working bcd value
  176. | ( ) a6: pointer to original bcd value
  177. | (*) fp0: mantissa accumulator
  178. | ( ) FP_SCR1: working copy of original bcd value
  179. | ( ) L_SCR1: copy of original exponent word
  180. |
  181. calc_m:
  182. moveql #1,%d1 |word counter, init to 1
  183. fmoves FZERO,%fp0 |accumulator
  184. |
  185. |
  186. | Since the packed number has a long word between the first & second parts,
  187. | get the integer digit then skip down & get the rest of the
  188. | mantissa. We will unroll the loop once.
  189. |
  190. bfextu (%a0){#28:#4},%d0 |integer part is ls digit in long word
  191. faddb %d0,%fp0 |add digit to sum in fp0
  192. |
  193. |
  194. | Get the rest of the mantissa.
  195. |
  196. loadlw:
  197. movel (%a0,%d1.L*4),%d4 |load mantissa longword into d4
  198. moveql #FSTRT,%d3 |counter to pick up digits
  199. moveql #FNIBS,%d2 |reset number of digits per a0 ptr
  200. md2b:
  201. fmuls FTEN,%fp0 |fp0 = fp0 * 10
  202. bfextu %d4{%d3:#4},%d0 |get the digit and zero extend
  203. faddb %d0,%fp0 |fp0 = fp0 + digit
  204. |
  205. |
  206. | If all the digits (8) in that long word have been converted (d2=0),
  207. | then inc d1 (=2) to point to the next long word and reset d3 to 0
  208. | to initialize the digit offset, and set d2 to 7 for the digit count;
  209. | else continue with this long word.
  210. |
  211. addqb #4,%d3 |advance d3 to the next digit
  212. dbf %d2,md2b |check for last digit in this lw
  213. nextlw:
  214. addql #1,%d1 |inc lw pointer in mantissa
  215. cmpl #2,%d1 |test for last lw
  216. ble loadlw |if not, get last one
  217. |
  218. | Check the sign of the mant and make the value in fp0 the same sign.
  219. |
  220. m_sign:
  221. btst #31,(%a0) |test sign of the mantissa
  222. beq ap_st_z |if clear, go to append/strip zeros
  223. fnegx %fp0 |if set, negate fp0
  224. |
  225. | Append/strip zeros:
  226. |
  227. | For adjusted exponents which have an absolute value greater than 27*,
  228. | this routine calculates the amount needed to normalize the mantissa
  229. | for the adjusted exponent. That number is subtracted from the exp
  230. | if the exp was positive, and added if it was negative. The purpose
  231. | of this is to reduce the value of the exponent and the possibility
  232. | of error in calculation of pwrten.
  233. |
  234. | 1. Branch on the sign of the adjusted exponent.
  235. | 2p.(positive exp)
  236. | 2. Check M16 and the digits in lwords 2 and 3 in descending order.
  237. | 3. Add one for each zero encountered until a non-zero digit.
  238. | 4. Subtract the count from the exp.
  239. | 5. Check if the exp has crossed zero in #3 above; make the exp abs
  240. | and set SE.
  241. | 6. Multiply the mantissa by 10**count.
  242. | 2n.(negative exp)
  243. | 2. Check the digits in lwords 3 and 2 in descending order.
  244. | 3. Add one for each zero encountered until a non-zero digit.
  245. | 4. Add the count to the exp.
  246. | 5. Check if the exp has crossed zero in #3 above; clear SE.
  247. | 6. Divide the mantissa by 10**count.
  248. |
  249. | *Why 27? If the adjusted exponent is within -28 < expA < 28, than
  250. | any adjustment due to append/strip zeros will drive the resultant
  251. | exponent towards zero. Since all pwrten constants with a power
  252. | of 27 or less are exact, there is no need to use this routine to
  253. | attempt to lessen the resultant exponent.
  254. |
  255. | Register usage:
  256. |
  257. | ap_st_z:
  258. | (*) d0: temp digit storage
  259. | (*) d1: zero count
  260. | (*) d2: digit count
  261. | (*) d3: offset pointer
  262. | ( ) d4: first word of bcd
  263. | (*) d5: lword counter
  264. | ( ) a0: pointer to working bcd value
  265. | ( ) FP_SCR1: working copy of original bcd value
  266. | ( ) L_SCR1: copy of original exponent word
  267. |
  268. |
  269. | First check the absolute value of the exponent to see if this
  270. | routine is necessary. If so, then check the sign of the exponent
  271. | and do append (+) or strip (-) zeros accordingly.
  272. | This section handles a positive adjusted exponent.
  273. |
  274. ap_st_z:
  275. movel L_SCR1(%a6),%d1 |load expA for range test
  276. cmpl #27,%d1 |test is with 27
  277. ble pwrten |if abs(expA) <28, skip ap/st zeros
  278. btst #30,(%a0) |check sign of exp
  279. bne ap_st_n |if neg, go to neg side
  280. clrl %d1 |zero count reg
  281. movel (%a0),%d4 |load lword 1 to d4
  282. bfextu %d4{#28:#4},%d0 |get M16 in d0
  283. bnes ap_p_fx |if M16 is non-zero, go fix exp
  284. addql #1,%d1 |inc zero count
  285. moveql #1,%d5 |init lword counter
  286. movel (%a0,%d5.L*4),%d4 |get lword 2 to d4
  287. bnes ap_p_cl |if lw 2 is zero, skip it
  288. addql #8,%d1 |and inc count by 8
  289. addql #1,%d5 |inc lword counter
  290. movel (%a0,%d5.L*4),%d4 |get lword 3 to d4
  291. ap_p_cl:
  292. clrl %d3 |init offset reg
  293. moveql #7,%d2 |init digit counter
  294. ap_p_gd:
  295. bfextu %d4{%d3:#4},%d0 |get digit
  296. bnes ap_p_fx |if non-zero, go to fix exp
  297. addql #4,%d3 |point to next digit
  298. addql #1,%d1 |inc digit counter
  299. dbf %d2,ap_p_gd |get next digit
  300. ap_p_fx:
  301. movel %d1,%d0 |copy counter to d2
  302. movel L_SCR1(%a6),%d1 |get adjusted exp from memory
  303. subl %d0,%d1 |subtract count from exp
  304. bges ap_p_fm |if still pos, go to pwrten
  305. negl %d1 |now its neg; get abs
  306. movel (%a0),%d4 |load lword 1 to d4
  307. orl #0x40000000,%d4 | and set SE in d4
  308. orl #0x40000000,(%a0) | and in memory
  309. |
  310. | Calculate the mantissa multiplier to compensate for the striping of
  311. | zeros from the mantissa.
  312. |
  313. ap_p_fm:
  314. movel #PTENRN,%a1 |get address of power-of-ten table
  315. clrl %d3 |init table index
  316. fmoves FONE,%fp1 |init fp1 to 1
  317. moveql #3,%d2 |init d2 to count bits in counter
  318. ap_p_el:
  319. asrl #1,%d0 |shift lsb into carry
  320. bccs ap_p_en |if 1, mul fp1 by pwrten factor
  321. fmulx (%a1,%d3),%fp1 |mul by 10**(d3_bit_no)
  322. ap_p_en:
  323. addl #12,%d3 |inc d3 to next rtable entry
  324. tstl %d0 |check if d0 is zero
  325. bnes ap_p_el |if not, get next bit
  326. fmulx %fp1,%fp0 |mul mantissa by 10**(no_bits_shifted)
  327. bra pwrten |go calc pwrten
  328. |
  329. | This section handles a negative adjusted exponent.
  330. |
  331. ap_st_n:
  332. clrl %d1 |clr counter
  333. moveql #2,%d5 |set up d5 to point to lword 3
  334. movel (%a0,%d5.L*4),%d4 |get lword 3
  335. bnes ap_n_cl |if not zero, check digits
  336. subl #1,%d5 |dec d5 to point to lword 2
  337. addql #8,%d1 |inc counter by 8
  338. movel (%a0,%d5.L*4),%d4 |get lword 2
  339. ap_n_cl:
  340. movel #28,%d3 |point to last digit
  341. moveql #7,%d2 |init digit counter
  342. ap_n_gd:
  343. bfextu %d4{%d3:#4},%d0 |get digit
  344. bnes ap_n_fx |if non-zero, go to exp fix
  345. subql #4,%d3 |point to previous digit
  346. addql #1,%d1 |inc digit counter
  347. dbf %d2,ap_n_gd |get next digit
  348. ap_n_fx:
  349. movel %d1,%d0 |copy counter to d0
  350. movel L_SCR1(%a6),%d1 |get adjusted exp from memory
  351. subl %d0,%d1 |subtract count from exp
  352. bgts ap_n_fm |if still pos, go fix mantissa
  353. negl %d1 |take abs of exp and clr SE
  354. movel (%a0),%d4 |load lword 1 to d4
  355. andl #0xbfffffff,%d4 | and clr SE in d4
  356. andl #0xbfffffff,(%a0) | and in memory
  357. |
  358. | Calculate the mantissa multiplier to compensate for the appending of
  359. | zeros to the mantissa.
  360. |
  361. ap_n_fm:
  362. movel #PTENRN,%a1 |get address of power-of-ten table
  363. clrl %d3 |init table index
  364. fmoves FONE,%fp1 |init fp1 to 1
  365. moveql #3,%d2 |init d2 to count bits in counter
  366. ap_n_el:
  367. asrl #1,%d0 |shift lsb into carry
  368. bccs ap_n_en |if 1, mul fp1 by pwrten factor
  369. fmulx (%a1,%d3),%fp1 |mul by 10**(d3_bit_no)
  370. ap_n_en:
  371. addl #12,%d3 |inc d3 to next rtable entry
  372. tstl %d0 |check if d0 is zero
  373. bnes ap_n_el |if not, get next bit
  374. fdivx %fp1,%fp0 |div mantissa by 10**(no_bits_shifted)
  375. |
  376. |
  377. | Calculate power-of-ten factor from adjusted and shifted exponent.
  378. |
  379. | Register usage:
  380. |
  381. | pwrten:
  382. | (*) d0: temp
  383. | ( ) d1: exponent
  384. | (*) d2: {FPCR[6:5],SM,SE} as index in RTABLE; temp
  385. | (*) d3: FPCR work copy
  386. | ( ) d4: first word of bcd
  387. | (*) a1: RTABLE pointer
  388. | calc_p:
  389. | (*) d0: temp
  390. | ( ) d1: exponent
  391. | (*) d3: PWRTxx table index
  392. | ( ) a0: pointer to working copy of bcd
  393. | (*) a1: PWRTxx pointer
  394. | (*) fp1: power-of-ten accumulator
  395. |
  396. | Pwrten calculates the exponent factor in the selected rounding mode
  397. | according to the following table:
  398. |
  399. | Sign of Mant Sign of Exp Rounding Mode PWRTEN Rounding Mode
  400. |
  401. | ANY ANY RN RN
  402. |
  403. | + + RP RP
  404. | - + RP RM
  405. | + - RP RM
  406. | - - RP RP
  407. |
  408. | + + RM RM
  409. | - + RM RP
  410. | + - RM RP
  411. | - - RM RM
  412. |
  413. | + + RZ RM
  414. | - + RZ RM
  415. | + - RZ RP
  416. | - - RZ RP
  417. |
  418. |
  419. pwrten:
  420. movel USER_FPCR(%a6),%d3 |get user's FPCR
  421. bfextu %d3{#26:#2},%d2 |isolate rounding mode bits
  422. movel (%a0),%d4 |reload 1st bcd word to d4
  423. asll #2,%d2 |format d2 to be
  424. bfextu %d4{#0:#2},%d0 | {FPCR[6],FPCR[5],SM,SE}
  425. addl %d0,%d2 |in d2 as index into RTABLE
  426. leal RTABLE,%a1 |load rtable base
  427. moveb (%a1,%d2),%d0 |load new rounding bits from table
  428. clrl %d3 |clear d3 to force no exc and extended
  429. bfins %d0,%d3{#26:#2} |stuff new rounding bits in FPCR
  430. fmovel %d3,%FPCR |write new FPCR
  431. asrl #1,%d0 |write correct PTENxx table
  432. bccs not_rp |to a1
  433. leal PTENRP,%a1 |it is RP
  434. bras calc_p |go to init section
  435. not_rp:
  436. asrl #1,%d0 |keep checking
  437. bccs not_rm
  438. leal PTENRM,%a1 |it is RM
  439. bras calc_p |go to init section
  440. not_rm:
  441. leal PTENRN,%a1 |it is RN
  442. calc_p:
  443. movel %d1,%d0 |copy exp to d0;use d0
  444. bpls no_neg |if exp is negative,
  445. negl %d0 |invert it
  446. orl #0x40000000,(%a0) |and set SE bit
  447. no_neg:
  448. clrl %d3 |table index
  449. fmoves FONE,%fp1 |init fp1 to 1
  450. e_loop:
  451. asrl #1,%d0 |shift next bit into carry
  452. bccs e_next |if zero, skip the mul
  453. fmulx (%a1,%d3),%fp1 |mul by 10**(d3_bit_no)
  454. e_next:
  455. addl #12,%d3 |inc d3 to next rtable entry
  456. tstl %d0 |check if d0 is zero
  457. bnes e_loop |not zero, continue shifting
  458. |
  459. |
  460. | Check the sign of the adjusted exp and make the value in fp0 the
  461. | same sign. If the exp was pos then multiply fp1*fp0;
  462. | else divide fp0/fp1.
  463. |
  464. | Register Usage:
  465. | norm:
  466. | ( ) a0: pointer to working bcd value
  467. | (*) fp0: mantissa accumulator
  468. | ( ) fp1: scaling factor - 10**(abs(exp))
  469. |
  470. norm:
  471. btst #30,(%a0) |test the sign of the exponent
  472. beqs mul |if clear, go to multiply
  473. div:
  474. fdivx %fp1,%fp0 |exp is negative, so divide mant by exp
  475. bras end_dec
  476. mul:
  477. fmulx %fp1,%fp0 |exp is positive, so multiply by exp
  478. |
  479. |
  480. | Clean up and return with result in fp0.
  481. |
  482. | If the final mul/div in decbin incurred an inex exception,
  483. | it will be inex2, but will be reported as inex1 by get_op.
  484. |
  485. end_dec:
  486. fmovel %FPSR,%d0 |get status register
  487. bclrl #inex2_bit+8,%d0 |test for inex2 and clear it
  488. fmovel %d0,%FPSR |return status reg w/o inex2
  489. beqs no_exc |skip this if no exc
  490. orl #inx1a_mask,USER_FPSR(%a6) |set inex1/ainex
  491. no_exc:
  492. moveml (%a7)+,%d2-%d5
  493. rts
  494. |end