1 1.3 cgd * $NetBSD: binstr.sa,v 1.3 1994/10/26 07:48:53 cgd Exp $ 2 1.3 cgd 3 1.1 mycroft * MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP 4 1.1 mycroft * M68000 Hi-Performance Microprocessor Division 5 1.1 mycroft * M68040 Software Package 6 1.1 mycroft * 7 1.1 mycroft * M68040 Software Package Copyright (c) 1993, 1994 Motorola Inc. 8 1.1 mycroft * All rights reserved. 9 1.1 mycroft * 10 1.1 mycroft * THE SOFTWARE is provided on an "AS IS" basis and without warranty. 11 1.1 mycroft * To the maximum extent permitted by applicable law, 12 1.1 mycroft * MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED, 13 1.1 mycroft * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A 14 1.1 mycroft * PARTICULAR PURPOSE and any warranty against infringement with 15 1.1 mycroft * regard to the SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF) 16 1.1 mycroft * and any accompanying written materials. 17 1.1 mycroft * 18 1.1 mycroft * To the maximum extent permitted by applicable law, 19 1.1 mycroft * IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER 20 1.1 mycroft * (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS 21 1.1 mycroft * PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR 22 1.1 mycroft * OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE 23 1.1 mycroft * SOFTWARE. Motorola assumes no responsibility for the maintenance 24 1.1 mycroft * and support of the SOFTWARE. 25 1.1 mycroft * 26 1.1 mycroft * You are hereby granted a copyright license to use, modify, and 27 1.1 mycroft * distribute the SOFTWARE so long as this entire notice is retained 28 1.1 mycroft * without alteration in any modified and/or redistributed versions, 29 1.1 mycroft * and that such modified versions are clearly identified as such. 30 1.1 mycroft * No licenses are granted by implication, estoppel or otherwise 31 1.1 mycroft * under any patents or trademarks of Motorola, Inc. 32 1.1 mycroft 33 1.1 mycroft * 34 1.1 mycroft * binstr.sa 3.3 12/19/90 35 1.1 mycroft * 36 1.1 mycroft * 37 1.1 mycroft * Description: Converts a 64-bit binary integer to bcd. 38 1.1 mycroft * 39 1.1 mycroft * Input: 64-bit binary integer in d2:d3, desired length (LEN) in 40 1.1 mycroft * d0, and a pointer to start in memory for bcd characters 41 1.1 mycroft * in d0. (This pointer must point to byte 4 of the first 42 1.1 mycroft * lword of the packed decimal memory string.) 43 1.1 mycroft * 44 1.1 mycroft * Output: LEN bcd digits representing the 64-bit integer. 45 1.1 mycroft * 46 1.1 mycroft * Algorithm: 47 1.1 mycroft * The 64-bit binary is assumed to have a decimal point before 48 1.1 mycroft * bit 63. The fraction is multiplied by 10 using a mul by 2 49 1.1 mycroft * shift and a mul by 8 shift. The bits shifted out of the 50 1.1 mycroft * msb form a decimal digit. This process is iterated until 51 1.1 mycroft * LEN digits are formed. 52 1.1 mycroft * 53 1.1 mycroft * A1. Init d7 to 1. D7 is the byte digit counter, and if 1, the 54 1.1 mycroft * digit formed will be assumed the least significant. This is 55 1.1 mycroft * to force the first byte formed to have a 0 in the upper 4 bits. 56 1.1 mycroft * 57 1.1 mycroft * A2. Beginning of the loop: 58 1.1 mycroft * Copy the fraction in d2:d3 to d4:d5. 59 1.1 mycroft * 60 1.1 mycroft * A3. Multiply the fraction in d2:d3 by 8 using bit-field 61 1.1 mycroft * extracts and shifts. The three msbs from d2 will go into 62 1.1 mycroft * d1. 63 1.1 mycroft * 64 1.1 mycroft * A4. Multiply the fraction in d4:d5 by 2 using shifts. The msb 65 1.1 mycroft * will be collected by the carry. 66 1.1 mycroft * 67 1.1 mycroft * A5. Add using the carry the 64-bit quantities in d2:d3 and d4:d5 68 1.1 mycroft * into d2:d3. D1 will contain the bcd digit formed. 69 1.1 mycroft * 70 1.1 mycroft * A6. Test d7. If zero, the digit formed is the ms digit. If non- 71 1.1 mycroft * zero, it is the ls digit. Put the digit in its place in the 72 1.1 mycroft * upper word of d0. If it is the ls digit, write the word 73 1.1 mycroft * from d0 to memory. 74 1.1 mycroft * 75 1.1 mycroft * A7. Decrement d6 (LEN counter) and repeat the loop until zero. 76 1.1 mycroft * 77 1.1 mycroft * Implementation Notes: 78 1.1 mycroft * 79 1.1 mycroft * The registers are used as follows: 80 1.1 mycroft * 81 1.1 mycroft * d0: LEN counter 82 1.1 mycroft * d1: temp used to form the digit 83 1.1 mycroft * d2: upper 32-bits of fraction for mul by 8 84 1.1 mycroft * d3: lower 32-bits of fraction for mul by 8 85 1.1 mycroft * d4: upper 32-bits of fraction for mul by 2 86 1.1 mycroft * d5: lower 32-bits of fraction for mul by 2 87 1.1 mycroft * d6: temp for bit-field extracts 88 1.1 mycroft * d7: byte digit formation word;digit count {0,1} 89 1.1 mycroft * a0: pointer into memory for packed bcd string formation 90 1.1 mycroft * 91 1.1 mycroft 92 1.1 mycroft BINSTR IDNT 2,1 Motorola 040 Floating Point Software Package 93 1.1 mycroft 94 1.1 mycroft section 8 95 1.1 mycroft 96 1.1 mycroft include fpsp.h 97 1.1 mycroft 98 1.1 mycroft xdef binstr 99 1.1 mycroft binstr: 100 1.1 mycroft movem.l d0-d7,-(a7) 101 1.1 mycroft * 102 1.1 mycroft * A1: Init d7 103 1.1 mycroft * 104 1.1 mycroft moveq.l #1,d7 ;init d7 for second digit 105 1.1 mycroft subq.l #1,d0 ;for dbf d0 would have LEN+1 passes 106 1.1 mycroft * 107 1.1 mycroft * A2. Copy d2:d3 to d4:d5. Start loop. 108 1.1 mycroft * 109 1.1 mycroft loop: 110 1.1 mycroft move.l d2,d4 ;copy the fraction before muls 111 1.1 mycroft move.l d3,d5 ;to d4:d5 112 1.1 mycroft * 113 1.1 mycroft * A3. Multiply d2:d3 by 8; extract msbs into d1. 114 1.1 mycroft * 115 1.1 mycroft bfextu d2{0:3},d1 ;copy 3 msbs of d2 into d1 116 1.1 mycroft asl.l #3,d2 ;shift d2 left by 3 places 117 1.1 mycroft bfextu d3{0:3},d6 ;copy 3 msbs of d3 into d6 118 1.1 mycroft asl.l #3,d3 ;shift d3 left by 3 places 119 1.1 mycroft or.l d6,d2 ;or in msbs from d3 into d2 120 1.1 mycroft * 121 1.1 mycroft * A4. Multiply d4:d5 by 2; add carry out to d1. 122 1.1 mycroft * 123 1.2 mycroft add.l d5,d5 ;mul d5 by 2 124 1.2 mycroft addx.l d4,d4 ;mul d4 by 2 125 1.1 mycroft swap d6 ;put 0 in d6 lower word 126 1.1 mycroft addx.w d6,d1 ;add in extend from mul by 2 127 1.1 mycroft * 128 1.1 mycroft * A5. Add mul by 8 to mul by 2. D1 contains the digit formed. 129 1.1 mycroft * 130 1.1 mycroft add.l d5,d3 ;add lower 32 bits 131 1.1 mycroft nop ;ERRATA FIX #13 (Rev. 1.2 6/6/90) 132 1.1 mycroft addx.l d4,d2 ;add with extend upper 32 bits 133 1.1 mycroft nop ;ERRATA FIX #13 (Rev. 1.2 6/6/90) 134 1.1 mycroft addx.w d6,d1 ;add in extend from add to d1 135 1.1 mycroft swap d6 ;with d6 = 0; put 0 in upper word 136 1.1 mycroft * 137 1.1 mycroft * A6. Test d7 and branch. 138 1.1 mycroft * 139 1.1 mycroft tst.w d7 ;if zero, store digit & to loop 140 1.1 mycroft beq.b first_d ;if non-zero, form byte & write 141 1.1 mycroft sec_d: 142 1.1 mycroft swap d7 ;bring first digit to word d7b 143 1.1 mycroft asl.w #4,d7 ;first digit in upper 4 bits d7b 144 1.1 mycroft add.w d1,d7 ;add in ls digit to d7b 145 1.1 mycroft move.b d7,(a0)+ ;store d7b byte in memory 146 1.1 mycroft swap d7 ;put LEN counter in word d7a 147 1.1 mycroft clr.w d7 ;set d7a to signal no digits done 148 1.1 mycroft dbf.w d0,loop ;do loop some more! 149 1.1 mycroft bra.b end_bstr ;finished, so exit 150 1.1 mycroft first_d: 151 1.1 mycroft swap d7 ;put digit word in d7b 152 1.1 mycroft move.w d1,d7 ;put new digit in d7b 153 1.1 mycroft swap d7 ;put LEN counter in word d7a 154 1.1 mycroft addq.w #1,d7 ;set d7a to signal first digit done 155 1.1 mycroft dbf.w d0,loop ;do loop some more! 156 1.1 mycroft swap d7 ;put last digit in string 157 1.1 mycroft lsl.w #4,d7 ;move it to upper 4 bits 158 1.1 mycroft move.b d7,(a0)+ ;store it in memory string 159 1.1 mycroft * 160 1.1 mycroft * Clean up and return with result in fp0. 161 1.1 mycroft * 162 1.1 mycroft end_bstr: 163 1.1 mycroft movem.l (a7)+,d0-d7 164 1.1 mycroft rts 165 1.1 mycroft end 166