| 1 | /* SPDX-License-Identifier: GPL-2.0 */
|
| 2 | /*
|
| 3 | * arch/alpha/lib/ev6-divide.S
|
| 4 | *
|
| 5 | * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
|
| 6 | *
|
| 7 | * Alpha division..
|
| 8 | */
|
| 9 |
|
| 10 | /*
|
| 11 | * The alpha chip doesn't provide hardware division, so we have to do it
|
| 12 | * by hand. The compiler expects the functions
|
| 13 | *
|
| 14 | * __divqu: 64-bit unsigned long divide
|
| 15 | * __remqu: 64-bit unsigned long remainder
|
| 16 | * __divqs/__remqs: signed 64-bit
|
| 17 | * __divlu/__remlu: unsigned 32-bit
|
| 18 | * __divls/__remls: signed 32-bit
|
| 19 | *
|
| 20 | * These are not normal C functions: instead of the normal
|
| 21 | * calling sequence, these expect their arguments in registers
|
| 22 | * $24 and $25, and return the result in $27. Register $28 may
|
| 23 | * be clobbered (assembly temporary), anything else must be saved.
|
| 24 | *
|
| 25 | * In short: painful.
|
| 26 | *
|
| 27 | * This is a rather simple bit-at-a-time algorithm: it's very good
|
| 28 | * at dividing random 64-bit numbers, but the more usual case where
|
| 29 | * the divisor is small is handled better by the DEC algorithm
|
| 30 | * using lookup tables. This uses much less memory, though, and is
|
| 31 | * nicer on the cache.. Besides, I don't know the copyright status
|
| 32 | * of the DEC code.
|
| 33 | */
|
| 34 |
|
| 35 | /*
|
| 36 | * My temporaries:
|
| 37 | * $0 - current bit
|
| 38 | * $1 - shifted divisor
|
| 39 | * $2 - modulus/quotient
|
| 40 | *
|
| 41 | * $23 - return address
|
| 42 | * $24 - dividend
|
| 43 | * $25 - divisor
|
| 44 | *
|
| 45 | * $27 - quotient/modulus
|
| 46 | * $28 - compare status
|
| 47 | *
|
| 48 | * Much of the information about 21264 scheduling/coding comes from:
|
| 49 | * Compiler Writer's Guide for the Alpha 21264
|
| 50 | * abbreviated as 'CWG' in other comments here
|
| 51 | * ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
|
| 52 | * Scheduling notation:
|
| 53 | * E - either cluster
|
| 54 | * U - upper subcluster; U0 - subcluster U0; U1 - subcluster U1
|
| 55 | * L - lower subcluster; L0 - subcluster L0; L1 - subcluster L1
|
| 56 | * Try not to change the actual algorithm if possible for consistency.
|
| 57 | */
|
| 58 |
|
| 59 | |
| 60 | |
| 61 |
|
| 62 | /*
|
| 63 | * Select function type and registers
|
| 64 | */
|
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 |
|
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 |
|
| 89 | /*
|
| 90 | * For 32-bit operations, we need to extend to 64-bit
|
| 91 | */
|
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 |
|
| 104 | .set noat
|
| 105 | .align 4
|
| 106 | .globl ufunction
|
| 107 | .ent ufunction
|
| 108 | ufunction:
|
| 109 | subq $30,STACK,$30 |
| 110 | .frame $30,STACK,$23
|
| 111 | .prologue 0
|
| 112 |
|
| 113 | 7: stq $1, 0($30) |
| 114 | bis $25,$25,divisor |
| 115 | stq $2, 8($30) |
| 116 |
|
| 117 | bis $24,$24,modulus |
| 118 | stq $0,16($30) |
| 119 | bis $31,$31,quotient |
| 120 | LONGIFY(divisor) |
| 121 |
|
| 122 | stq tmp1,24($30) |
| 123 | LONGIFY(modulus) |
| 124 | bis $31,1,mask |
| 125 | DIV_ONLY(stq tmp2,32($30)) |
| 126 |
|
| 127 | beq divisor, 9f /* div by zero */
|
| 128 | /*
|
| 129 | * In spite of the DIV_ONLY being either a non-instruction
|
| 130 | * or an actual stq, the addition of the .align directive
|
| 131 | * below ensures that label 1 is going to be nicely aligned
|
| 132 | */
|
| 133 |
|
| 134 | .align 4
|
| 135 | |
| 136 | /*
|
| 137 | * shift divisor left, using 3-bit shifts for
|
| 138 | * 32-bit divides as we can't overflow. Three-bit
|
| 139 | * shifts will result in looping three times less
|
| 140 | * here, but can result in two loops more later.
|
| 141 | * Thus using a large shift isn't worth it (and
|
| 142 | * s8add pairs better than a sll..)
|
| 143 | */
|
| 144 | 1: cmpult divisor,modulus,compare |
| 145 | s8addq divisor,$31,divisor |
| 146 | s8addq mask,$31,mask |
| 147 | bne compare,1b |
| 148 | |
| 149 | 1: cmpult divisor,modulus,compare |
| 150 | nop |
| 151 | nop |
| 152 | blt divisor, 2f |
| 153 |
|
| 154 | addq divisor,divisor,divisor |
| 155 | addq mask,mask,mask |
| 156 | unop |
| 157 | bne compare,1b |
| 158 | |
| 159 |
|
| 160 | /* ok, start to go right again.. */
|
| 161 | 2:
|
| 162 | /*
|
| 163 | * Keep things nicely bundled... use a nop instead of not
|
| 164 | * having an instruction for DIV_ONLY
|
| 165 | */
|
| 166 | |
| 167 | DIV_ONLY(addq quotient,mask,tmp2) |
| 168 | |
| 169 | nop |
| 170 | |
| 171 | srl mask,1,mask |
| 172 | cmpule divisor,modulus,compare |
| 173 | subq modulus,divisor,tmp1 |
| 174 |
|
| 175 | |
| 176 | DIV_ONLY(cmovne compare,tmp2,quotient) |
| 177 | nop |
| 178 | srl divisor,1,divisor |
| 179 | nop |
| 180 |
|
| 181 | nop |
| 182 | cmovne compare,tmp1,modulus |
| 183 | nop |
| 184 | bne mask,2b |
| 185 | |
| 186 | srl divisor,1,divisor |
| 187 | cmovne compare,tmp1,modulus |
| 188 | nop |
| 189 | bne mask,2b |
| 190 | |
| 191 |
|
| 192 | 9: ldq $1, 0($30) |
| 193 | ldq $2, 8($30) |
| 194 | nop |
| 195 | nop |
| 196 |
|
| 197 | ldq $0,16($30) |
| 198 | ldq tmp1,24($30) |
| 199 | nop |
| 200 | nop |
| 201 |
|
| 202 | |
| 203 | DIV_ONLY(ldq tmp2,32($30)) |
| 204 | |
| 205 | nop |
| 206 | |
| 207 | addq $30,STACK,$30 |
| 208 | ret $31,($23),1 |
| 209 | .end ufunction
|
| 210 | EXPORT_SYMBOL(ufunction)
|
| 211 |
|
| 212 | /*
|
| 213 | * Uhh.. Ugly signed division. I'd rather not have it at all, but
|
| 214 | * it's needed in some circumstances. There are different ways to
|
| 215 | * handle this, really. This does:
|
| 216 | * -a / b = a / -b = -(a / b)
|
| 217 | * -a % b = -(a % b)
|
| 218 | * a % -b = a % b
|
| 219 | * which is probably not the best solution, but at least should
|
| 220 | * have the property that (x/y)*y + (x%y) = x.
|
| 221 | */
|
| 222 | .align 4
|
| 223 | .globl sfunction
|
| 224 | .ent sfunction
|
| 225 | sfunction:
|
| 226 | subq $30,STACK,$30 |
| 227 | .frame $30,STACK,$23
|
| 228 | .prologue 0
|
| 229 | bis $24,$25,$28 |
| 230 | SLONGIFY($28) |
| 231 | bge $28,7b |
| 232 |
|
| 233 | stq $24,0($30) |
| 234 | subq $31,$24,$28 |
| 235 | stq $25,8($30) |
| 236 | nop |
| 237 |
|
| 238 | cmovlt $24,$28,$24 /* abs($24) */ |
| 239 | nop |
| 240 | stq $23,16($30) |
| 241 | subq $31,$25,$28 |
| 242 |
|
| 243 | stq tmp1,24($30) |
| 244 | cmovlt $25,$28,$25 /* abs($25) */ |
| 245 | nop |
| 246 | bsr $23,ufunction |
| 247 |
|
| 248 | ldq $24,0($30) |
| 249 | ldq $25,8($30) |
| 250 | GETSIGN($28) |
| 251 | subq $31,$27,tmp1 |
| 252 |
|
| 253 | SLONGIFY($28) |
| 254 | ldq $23,16($30) |
| 255 | cmovlt $28,tmp1,$27 |
| 256 | nop |
| 257 |
|
| 258 | ldq tmp1,24($30) |
| 259 | nop |
| 260 | addq $30,STACK,$30 |
| 261 | ret $31,($23),1 |
| 262 | .end sfunction
|
| 263 | EXPORT_SYMBOL(sfunction)
|