162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Divide a 64-bit unsigned number by a 32-bit unsigned number. 462306a36Sopenharmony_ci * This routine assumes that the top 32 bits of the dividend are 562306a36Sopenharmony_ci * non-zero to start with. 662306a36Sopenharmony_ci * On entry, r3 points to the dividend, which get overwritten with 762306a36Sopenharmony_ci * the 64-bit quotient, and r4 contains the divisor. 862306a36Sopenharmony_ci * On exit, r3 contains the remainder. 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * Copyright (C) 2002 Paul Mackerras, IBM Corp. 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci#include "ppc_asm.h" 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci .globl __div64_32 1562306a36Sopenharmony_ci__div64_32: 1662306a36Sopenharmony_ci lwz r5,0(r3) # get the dividend into r5/r6 1762306a36Sopenharmony_ci lwz r6,4(r3) 1862306a36Sopenharmony_ci cmplw r5,r4 1962306a36Sopenharmony_ci li r7,0 2062306a36Sopenharmony_ci li r8,0 2162306a36Sopenharmony_ci blt 1f 2262306a36Sopenharmony_ci divwu r7,r5,r4 # if dividend.hi >= divisor, 2362306a36Sopenharmony_ci mullw r0,r7,r4 # quotient.hi = dividend.hi / divisor 2462306a36Sopenharmony_ci subf. r5,r0,r5 # dividend.hi %= divisor 2562306a36Sopenharmony_ci beq 3f 2662306a36Sopenharmony_ci1: mr r11,r5 # here dividend.hi != 0 2762306a36Sopenharmony_ci andis. r0,r5,0xc000 2862306a36Sopenharmony_ci bne 2f 2962306a36Sopenharmony_ci cntlzw r0,r5 # we are shifting the dividend right 3062306a36Sopenharmony_ci li r10,-1 # to make it < 2^32, and shifting 3162306a36Sopenharmony_ci srw r10,r10,r0 # the divisor right the same amount, 3262306a36Sopenharmony_ci addc r9,r4,r10 # rounding up (so the estimate cannot 3362306a36Sopenharmony_ci andc r11,r6,r10 # ever be too large, only too small) 3462306a36Sopenharmony_ci andc r9,r9,r10 3562306a36Sopenharmony_ci addze r9,r9 3662306a36Sopenharmony_ci or r11,r5,r11 3762306a36Sopenharmony_ci rotlw r9,r9,r0 3862306a36Sopenharmony_ci rotlw r11,r11,r0 3962306a36Sopenharmony_ci divwu r11,r11,r9 # then we divide the shifted quantities 4062306a36Sopenharmony_ci2: mullw r10,r11,r4 # to get an estimate of the quotient, 4162306a36Sopenharmony_ci mulhwu r9,r11,r4 # multiply the estimate by the divisor, 4262306a36Sopenharmony_ci subfc r6,r10,r6 # take the product from the divisor, 4362306a36Sopenharmony_ci add r8,r8,r11 # and add the estimate to the accumulated 4462306a36Sopenharmony_ci subfe. r5,r9,r5 # quotient 4562306a36Sopenharmony_ci bne 1b 4662306a36Sopenharmony_ci3: cmplw r6,r4 4762306a36Sopenharmony_ci blt 4f 4862306a36Sopenharmony_ci divwu r0,r6,r4 # perform the remaining 32-bit division 4962306a36Sopenharmony_ci mullw r10,r0,r4 # and get the remainder 5062306a36Sopenharmony_ci add r8,r8,r0 5162306a36Sopenharmony_ci subf r6,r10,r6 5262306a36Sopenharmony_ci4: stw r7,0(r3) # return the quotient in *r3 5362306a36Sopenharmony_ci stw r8,4(r3) 5462306a36Sopenharmony_ci mr r3,r6 # return the remainder in r3 5562306a36Sopenharmony_ci blr 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci/* 5862306a36Sopenharmony_ci * Extended precision shifts. 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * Updated to be valid for shift counts from 0 to 63 inclusive. 6162306a36Sopenharmony_ci * -- Gabriel 6262306a36Sopenharmony_ci * 6362306a36Sopenharmony_ci * R3/R4 has 64 bit value 6462306a36Sopenharmony_ci * R5 has shift count 6562306a36Sopenharmony_ci * result in R3/R4 6662306a36Sopenharmony_ci * 6762306a36Sopenharmony_ci * ashrdi3: arithmetic right shift (sign propagation) 6862306a36Sopenharmony_ci * lshrdi3: logical right shift 6962306a36Sopenharmony_ci * ashldi3: left shift 7062306a36Sopenharmony_ci */ 7162306a36Sopenharmony_ci .globl __ashrdi3 7262306a36Sopenharmony_ci__ashrdi3: 7362306a36Sopenharmony_ci subfic r6,r5,32 7462306a36Sopenharmony_ci srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count 7562306a36Sopenharmony_ci addi r7,r5,32 # could be xori, or addi with -32 7662306a36Sopenharmony_ci slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count) 7762306a36Sopenharmony_ci rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0 7862306a36Sopenharmony_ci sraw r7,r3,r7 # t2 = MSW >> (count-32) 7962306a36Sopenharmony_ci or r4,r4,r6 # LSW |= t1 8062306a36Sopenharmony_ci slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2 8162306a36Sopenharmony_ci sraw r3,r3,r5 # MSW = MSW >> count 8262306a36Sopenharmony_ci or r4,r4,r7 # LSW |= t2 8362306a36Sopenharmony_ci blr 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci .globl __ashldi3 8662306a36Sopenharmony_ci__ashldi3: 8762306a36Sopenharmony_ci subfic r6,r5,32 8862306a36Sopenharmony_ci slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count 8962306a36Sopenharmony_ci addi r7,r5,32 # could be xori, or addi with -32 9062306a36Sopenharmony_ci srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count) 9162306a36Sopenharmony_ci slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32) 9262306a36Sopenharmony_ci or r3,r3,r6 # MSW |= t1 9362306a36Sopenharmony_ci slw r4,r4,r5 # LSW = LSW << count 9462306a36Sopenharmony_ci or r3,r3,r7 # MSW |= t2 9562306a36Sopenharmony_ci blr 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci .globl __lshrdi3 9862306a36Sopenharmony_ci__lshrdi3: 9962306a36Sopenharmony_ci subfic r6,r5,32 10062306a36Sopenharmony_ci srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count 10162306a36Sopenharmony_ci addi r7,r5,32 # could be xori, or addi with -32 10262306a36Sopenharmony_ci slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count) 10362306a36Sopenharmony_ci srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32) 10462306a36Sopenharmony_ci or r4,r4,r6 # LSW |= t1 10562306a36Sopenharmony_ci srw r3,r3,r5 # MSW = MSW >> count 10662306a36Sopenharmony_ci or r4,r4,r7 # LSW |= t2 10762306a36Sopenharmony_ci blr 108