Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * arch/alpha/lib/divide.S
0004  *
0005  * (C) 1995 Linus Torvalds
0006  *
0007  * Alpha division..
0008  */
0009 
0010 /*
0011  * The alpha chip doesn't provide hardware division, so we have to do it
0012  * by hand.  The compiler expects the functions
0013  *
0014  *  __divqu: 64-bit unsigned long divide
0015  *  __remqu: 64-bit unsigned long remainder
0016  *  __divqs/__remqs: signed 64-bit
0017  *  __divlu/__remlu: unsigned 32-bit
0018  *  __divls/__remls: signed 32-bit
0019  *
0020  * These are not normal C functions: instead of the normal
0021  * calling sequence, these expect their arguments in registers
0022  * $24 and $25, and return the result in $27. Register $28 may
0023  * be clobbered (assembly temporary), anything else must be saved. 
0024  *
0025  * In short: painful.
0026  *
0027  * This is a rather simple bit-at-a-time algorithm: it's very good
0028  * at dividing random 64-bit numbers, but the more usual case where
0029  * the divisor is small is handled better by the DEC algorithm
0030  * using lookup tables. This uses much less memory, though, and is
0031  * nicer on the cache.. Besides, I don't know the copyright status
0032  * of the DEC code.
0033  */
0034 
0035 /*
0036  * My temporaries:
0037  *  $0 - current bit
0038  *  $1 - shifted divisor
0039  *  $2 - modulus/quotient
0040  *
0041  *  $23 - return address
0042  *  $24 - dividend
0043  *  $25 - divisor
0044  *
0045  *  $27 - quotient/modulus
0046  *  $28 - compare status
0047  */
0048 
0049 #include <asm/export.h>
0050 #define halt .long 0
0051 
0052 /*
0053  * Select function type and registers
0054  */
0055 #define mask    $0
0056 #define divisor $1
0057 #define compare $28
0058 #define tmp1    $3
0059 #define tmp2    $4
0060 
0061 #ifdef DIV
0062 #define DIV_ONLY(x,y...) x,##y
0063 #define MOD_ONLY(x,y...)
0064 #define func(x) __div##x
0065 #define modulus $2
0066 #define quotient $27
0067 #define GETSIGN(x) xor $24,$25,x
0068 #define STACK 48
0069 #else
0070 #define DIV_ONLY(x,y...)
0071 #define MOD_ONLY(x,y...) x,##y
0072 #define func(x) __rem##x
0073 #define modulus $27
0074 #define quotient $2
0075 #define GETSIGN(x) bis $24,$24,x
0076 #define STACK 32
0077 #endif
0078 
0079 /*
0080  * For 32-bit operations, we need to extend to 64-bit
0081  */
0082 #ifdef INTSIZE
0083 #define ufunction func(lu)
0084 #define sfunction func(l)
0085 #define LONGIFY(x) zapnot x,15,x
0086 #define SLONGIFY(x) addl x,0,x
0087 #else
0088 #define ufunction func(qu)
0089 #define sfunction func(q)
0090 #define LONGIFY(x)
0091 #define SLONGIFY(x)
0092 #endif
0093 
0094 .set noat
0095 .align  3
0096 .globl  ufunction
0097 .ent    ufunction
0098 ufunction:
0099     subq    $30,STACK,$30
0100     .frame  $30,STACK,$23
0101     .prologue 0
0102 
0103 7:  stq $1, 0($30)
0104     bis $25,$25,divisor
0105     stq $2, 8($30)
0106     bis $24,$24,modulus
0107     stq $0,16($30)
0108     bis $31,$31,quotient
0109     LONGIFY(divisor)
0110     stq tmp1,24($30)
0111     LONGIFY(modulus)
0112     bis $31,1,mask
0113     DIV_ONLY(stq tmp2,32($30))
0114     beq divisor, 9f         /* div by zero */
0115 
0116 #ifdef INTSIZE
0117     /*
0118      * shift divisor left, using 3-bit shifts for
0119      * 32-bit divides as we can't overflow. Three-bit
0120      * shifts will result in looping three times less
0121      * here, but can result in two loops more later.
0122      * Thus using a large shift isn't worth it (and
0123      * s8add pairs better than a sll..)
0124      */
0125 1:  cmpult  divisor,modulus,compare
0126     s8addq  divisor,$31,divisor
0127     s8addq  mask,$31,mask
0128     bne compare,1b
0129 #else
0130 1:  cmpult  divisor,modulus,compare
0131     blt     divisor, 2f
0132     addq    divisor,divisor,divisor
0133     addq    mask,mask,mask
0134     bne compare,1b
0135     unop
0136 #endif
0137 
0138     /* ok, start to go right again.. */
0139 2:  DIV_ONLY(addq quotient,mask,tmp2)
0140     srl mask,1,mask
0141     cmpule  divisor,modulus,compare
0142     subq    modulus,divisor,tmp1
0143     DIV_ONLY(cmovne compare,tmp2,quotient)
0144     srl divisor,1,divisor
0145     cmovne  compare,tmp1,modulus
0146     bne mask,2b
0147 
0148 9:  ldq $1, 0($30)
0149     ldq $2, 8($30)
0150     ldq $0,16($30)
0151     ldq tmp1,24($30)
0152     DIV_ONLY(ldq tmp2,32($30))
0153     addq    $30,STACK,$30
0154     ret $31,($23),1
0155     .end    ufunction
0156 EXPORT_SYMBOL(ufunction)
0157 
0158 /*
0159  * Uhh.. Ugly signed division. I'd rather not have it at all, but
0160  * it's needed in some circumstances. There are different ways to
0161  * handle this, really. This does:
0162  *  -a / b = a / -b = -(a / b)
0163  *  -a % b = -(a % b)
0164  *  a % -b = a % b
0165  * which is probably not the best solution, but at least should
0166  * have the property that (x/y)*y + (x%y) = x.
0167  */
0168 .align 3
0169 .globl  sfunction
0170 .ent    sfunction
0171 sfunction:
0172     subq    $30,STACK,$30
0173     .frame  $30,STACK,$23
0174     .prologue 0
0175     bis $24,$25,$28
0176     SLONGIFY($28)
0177     bge $28,7b
0178     stq $24,0($30)
0179     subq    $31,$24,$28
0180     stq $25,8($30)
0181     cmovlt  $24,$28,$24 /* abs($24) */
0182     stq $23,16($30)
0183     subq    $31,$25,$28
0184     stq tmp1,24($30)
0185     cmovlt  $25,$28,$25 /* abs($25) */
0186     unop
0187     bsr $23,ufunction
0188     ldq $24,0($30)
0189     ldq $25,8($30)
0190     GETSIGN($28)
0191     subq    $31,$27,tmp1
0192     SLONGIFY($28)
0193     ldq $23,16($30)
0194     cmovlt  $28,tmp1,$27
0195     ldq tmp1,24($30)
0196     addq    $30,STACK,$30
0197     ret $31,($23),1
0198     .end    sfunction
0199 EXPORT_SYMBOL(sfunction)