Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * arch/alpha/lib/ev6-divide.S
0004  *
0005  * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
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  * Much of the information about 21264 scheduling/coding comes from:
0049  *  Compiler Writer's Guide for the Alpha 21264
0050  *  abbreviated as 'CWG' in other comments here
0051  *  ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
0052  * Scheduling notation:
0053  *  E   - either cluster
0054  *  U   - upper subcluster; U0 - subcluster U0; U1 - subcluster U1
0055  *  L   - lower subcluster; L0 - subcluster L0; L1 - subcluster L1
0056  * Try not to change the actual algorithm if possible for consistency.
0057  */
0058 
0059 #include <asm/export.h>
0060 #define halt .long 0
0061 
0062 /*
0063  * Select function type and registers
0064  */
0065 #define mask    $0
0066 #define divisor $1
0067 #define compare $28
0068 #define tmp1    $3
0069 #define tmp2    $4
0070 
0071 #ifdef DIV
0072 #define DIV_ONLY(x,y...) x,##y
0073 #define MOD_ONLY(x,y...)
0074 #define func(x) __div##x
0075 #define modulus $2
0076 #define quotient $27
0077 #define GETSIGN(x) xor $24,$25,x
0078 #define STACK 48
0079 #else
0080 #define DIV_ONLY(x,y...)
0081 #define MOD_ONLY(x,y...) x,##y
0082 #define func(x) __rem##x
0083 #define modulus $27
0084 #define quotient $2
0085 #define GETSIGN(x) bis $24,$24,x
0086 #define STACK 32
0087 #endif
0088 
0089 /*
0090  * For 32-bit operations, we need to extend to 64-bit
0091  */
0092 #ifdef INTSIZE
0093 #define ufunction func(lu)
0094 #define sfunction func(l)
0095 #define LONGIFY(x) zapnot x,15,x
0096 #define SLONGIFY(x) addl x,0,x
0097 #else
0098 #define ufunction func(qu)
0099 #define sfunction func(q)
0100 #define LONGIFY(x)
0101 #define SLONGIFY(x)
0102 #endif
0103 
0104 .set noat
0105 .align  4
0106 .globl  ufunction
0107 .ent    ufunction
0108 ufunction:
0109     subq    $30,STACK,$30       # E :
0110     .frame  $30,STACK,$23
0111     .prologue 0
0112 
0113 7:  stq $1, 0($30)      # L :
0114     bis $25,$25,divisor     # E :
0115     stq $2, 8($30)      # L : L U L U
0116 
0117     bis $24,$24,modulus     # E :
0118     stq $0,16($30)      # L :
0119     bis $31,$31,quotient    # E :
0120     LONGIFY(divisor)        # E : U L L U
0121 
0122     stq tmp1,24($30)        # L :
0123     LONGIFY(modulus)        # E :
0124     bis $31,1,mask      # E :
0125     DIV_ONLY(stq tmp2,32($30))  # L : L U U L
0126 
0127     beq divisor, 9f         /* div by zero */
0128     /*
0129      * In spite of the DIV_ONLY being either a non-instruction
0130      * or an actual stq, the addition of the .align directive
0131      * below ensures that label 1 is going to be nicely aligned
0132      */
0133 
0134     .align  4
0135 #ifdef INTSIZE
0136     /*
0137      * shift divisor left, using 3-bit shifts for
0138      * 32-bit divides as we can't overflow. Three-bit
0139      * shifts will result in looping three times less
0140      * here, but can result in two loops more later.
0141      * Thus using a large shift isn't worth it (and
0142      * s8add pairs better than a sll..)
0143      */
0144 1:  cmpult  divisor,modulus,compare # E :
0145     s8addq  divisor,$31,divisor # E :
0146     s8addq  mask,$31,mask       # E :
0147     bne compare,1b      # U : U L U L
0148 #else
0149 1:  cmpult  divisor,modulus,compare # E :
0150     nop             # E :
0151     nop             # E :
0152     blt     divisor, 2f     # U : U L U L
0153 
0154     addq    divisor,divisor,divisor # E :
0155     addq    mask,mask,mask      # E :
0156     unop                # E :
0157     bne compare,1b      # U : U L U L
0158 #endif
0159 
0160     /* ok, start to go right again.. */
0161 2:
0162     /*
0163      * Keep things nicely bundled... use a nop instead of not
0164      * having an instruction for DIV_ONLY
0165      */
0166 #ifdef DIV
0167     DIV_ONLY(addq quotient,mask,tmp2) # E :
0168 #else
0169     nop             # E :
0170 #endif
0171     srl mask,1,mask     # U :
0172     cmpule  divisor,modulus,compare # E :
0173     subq    modulus,divisor,tmp1    # E :
0174 
0175 #ifdef DIV
0176     DIV_ONLY(cmovne compare,tmp2,quotient)  # E : Latency 2, extra map slot
0177     nop             # E : as part of the cmovne
0178     srl divisor,1,divisor   # U :
0179     nop             # E : L U L U
0180 
0181     nop             # E :
0182     cmovne  compare,tmp1,modulus    # E : Latency 2, extra map slot
0183     nop             # E : as part of the cmovne
0184     bne mask,2b         # U : U L U L
0185 #else
0186     srl divisor,1,divisor   # U :
0187     cmovne  compare,tmp1,modulus    # E : Latency 2, extra map slot
0188     nop             # E : as part of the cmovne
0189     bne mask,2b         # U : U L L U
0190 #endif
0191 
0192 9:  ldq $1, 0($30)      # L :
0193     ldq $2, 8($30)      # L :
0194     nop             # E :
0195     nop             # E : U U L L
0196 
0197     ldq $0,16($30)      # L :
0198     ldq tmp1,24($30)        # L :
0199     nop             # E :
0200     nop             # E :
0201 
0202 #ifdef DIV
0203     DIV_ONLY(ldq tmp2,32($30))  # L :
0204 #else
0205     nop             # E :
0206 #endif
0207     addq    $30,STACK,$30       # E :
0208     ret $31,($23),1     # L0 : L U U L
0209     .end    ufunction
0210 EXPORT_SYMBOL(ufunction)
0211 
0212 /*
0213  * Uhh.. Ugly signed division. I'd rather not have it at all, but
0214  * it's needed in some circumstances. There are different ways to
0215  * handle this, really. This does:
0216  *  -a / b = a / -b = -(a / b)
0217  *  -a % b = -(a % b)
0218  *  a % -b = a % b
0219  * which is probably not the best solution, but at least should
0220  * have the property that (x/y)*y + (x%y) = x.
0221  */
0222 .align 4
0223 .globl  sfunction
0224 .ent    sfunction
0225 sfunction:
0226     subq    $30,STACK,$30       # E :
0227     .frame  $30,STACK,$23
0228     .prologue 0
0229     bis $24,$25,$28     # E :
0230     SLONGIFY($28)           # E :
0231     bge $28,7b          # U :
0232 
0233     stq $24,0($30)      # L :
0234     subq    $31,$24,$28     # E :
0235     stq $25,8($30)      # L :
0236     nop             # E : U L U L
0237 
0238     cmovlt  $24,$28,$24 /* abs($24) */ # E : Latency 2, extra map slot
0239     nop             # E : as part of the cmov
0240     stq $23,16($30)     # L :
0241     subq    $31,$25,$28     # E : U L U L
0242 
0243     stq tmp1,24($30)        # L :
0244     cmovlt  $25,$28,$25 /* abs($25) */ # E : Latency 2, extra map slot
0245     nop             # E :
0246     bsr $23,ufunction       # L0: L U L U
0247 
0248     ldq $24,0($30)      # L :
0249     ldq $25,8($30)      # L :
0250     GETSIGN($28)            # E :
0251     subq    $31,$27,tmp1        # E : U U L L
0252 
0253     SLONGIFY($28)           # E :
0254     ldq $23,16($30)     # L :
0255     cmovlt  $28,tmp1,$27        # E : Latency 2, extra map slot
0256     nop             # E : U L L U : as part of the cmov
0257 
0258     ldq tmp1,24($30)        # L :
0259     nop             # E : as part of the cmov
0260     addq    $30,STACK,$30       # E :
0261     ret $31,($23),1     # L0 : L U U L
0262     .end    sfunction
0263 EXPORT_SYMBOL(sfunction)