/*1* arch/s390/lib/div64.c2*3* __div64_32 implementation for 31 bit.4*5* Copyright (C) IBM Corp. 20066* Author(s): Martin Schwidefsky ([email protected]),7*/89#include <linux/types.h>10#include <linux/module.h>1112#ifdef CONFIG_MARCH_G51314/*15* Function to divide an unsigned 64 bit integer by an unsigned16* 31 bit integer using signed 64/32 bit division.17*/18static uint32_t __div64_31(uint64_t *n, uint32_t base)19{20register uint32_t reg2 asm("2");21register uint32_t reg3 asm("3");22uint32_t *words = (uint32_t *) n;23uint32_t tmp;2425/* Special case base==1, remainder = 0, quotient = n */26if (base == 1)27return 0;28/*29* Special case base==0 will cause a fixed point divide exception30* on the dr instruction and may not happen anyway. For the31* following calculation we can assume base > 1. The first32* signed 64 / 32 bit division with an upper half of 0 will33* give the correct upper half of the 64 bit quotient.34*/35reg2 = 0UL;36reg3 = words[0];37asm volatile(38" dr %0,%2\n"39: "+d" (reg2), "+d" (reg3) : "d" (base) : "cc" );40words[0] = reg3;41reg3 = words[1];42/*43* To get the lower half of the 64 bit quotient and the 32 bit44* remainder we have to use a little trick. Since we only have45* a signed division the quotient can get too big. To avoid this46* the 64 bit dividend is halved, then the signed division will47* work. Afterwards the quotient and the remainder are doubled.48* If the last bit of the dividend has been one the remainder49* is increased by one then checked against the base. If the50* remainder has overflown subtract base and increase the51* quotient. Simple, no ?52*/53asm volatile(54" nr %2,%1\n"55" srdl %0,1\n"56" dr %0,%3\n"57" alr %0,%0\n"58" alr %1,%1\n"59" alr %0,%2\n"60" clr %0,%3\n"61" jl 0f\n"62" slr %0,%3\n"63" ahi %1,1\n"64"0:\n"65: "+d" (reg2), "+d" (reg3), "=d" (tmp)66: "d" (base), "2" (1UL) : "cc" );67words[1] = reg3;68return reg2;69}7071/*72* Function to divide an unsigned 64 bit integer by an unsigned73* 32 bit integer using the unsigned 64/31 bit division.74*/75uint32_t __div64_32(uint64_t *n, uint32_t base)76{77uint32_t r;7879/*80* If the most significant bit of base is set, divide n by81* (base/2). That allows to use 64/31 bit division and gives a82* good approximation of the result: n = (base/2)*q + r. The83* result needs to be corrected with two simple transformations.84* If base is already < 2^31-1 __div64_31 can be used directly.85*/86r = __div64_31(n, ((signed) base < 0) ? (base/2) : base);87if ((signed) base < 0) {88uint64_t q = *n;89/*90* First transformation:91* n = (base/2)*q + r92* = ((base/2)*2)*(q/2) + ((q&1) ? (base/2) : 0) + r93* Since r < (base/2), r + (base/2) < base.94* With q1 = (q/2) and r1 = r + ((q&1) ? (base/2) : 0)95* n = ((base/2)*2)*q1 + r1 with r1 < base.96*/97if (q & 1)98r += base/2;99q >>= 1;100/*101* Second transformation. ((base/2)*2) could have lost the102* last bit.103* n = ((base/2)*2)*q1 + r1104* = base*q1 - ((base&1) ? q1 : 0) + r1105*/106if (base & 1) {107int64_t rx = r - q;108/*109* base is >= 2^31. The worst case for the while110* loop is n=2^64-1 base=2^31+1. That gives a111* maximum for q=(2^64-1)/2^31 = 0x1ffffffff. Since112* base >= 2^31 the loop is finished after a maximum113* of three iterations.114*/115while (rx < 0) {116rx += base;117q--;118}119r = rx;120}121*n = q;122}123return r;124}125126#else /* MARCH_G5 */127128uint32_t __div64_32(uint64_t *n, uint32_t base)129{130register uint32_t reg2 asm("2");131register uint32_t reg3 asm("3");132uint32_t *words = (uint32_t *) n;133134reg2 = 0UL;135reg3 = words[0];136asm volatile(137" dlr %0,%2\n"138: "+d" (reg2), "+d" (reg3) : "d" (base) : "cc" );139words[0] = reg3;140reg3 = words[1];141asm volatile(142" dlr %0,%2\n"143: "+d" (reg2), "+d" (reg3) : "d" (base) : "cc" );144words[1] = reg3;145return reg2;146}147148#endif /* MARCH_G5 */149150151