| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* longlong.h -- based on code from gcc-2.95.3 | 
 | 2 |  | 
 | 3 |    definitions for mixed size 32/64 bit arithmetic. | 
 | 4 |    Copyright (C) 1991, 92, 94, 95, 96, 1997, 1998 Free Software Foundation, Inc. | 
 | 5 |  | 
 | 6 |    This definition file is free software; you can redistribute it | 
 | 7 |    and/or modify it under the terms of the GNU General Public | 
 | 8 |    License as published by the Free Software Foundation; either | 
 | 9 |    version 2, or (at your option) any later version. | 
 | 10 |  | 
 | 11 |    This definition file is distributed in the hope that it will be | 
 | 12 |    useful, but WITHOUT ANY WARRANTY; without even the implied | 
 | 13 |    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 
 | 14 |    See the GNU General Public License for more details. | 
 | 15 |  | 
 | 16 |    You should have received a copy of the GNU General Public License | 
 | 17 |    along with this program; if not, write to the Free Software | 
 | 18 |    Foundation, Inc., 59 Temple Place - Suite 330, | 
 | 19 |    Boston, MA 02111-1307, USA.  */ | 
 | 20 |  | 
 | 21 | /* Borrowed from GCC 2.95.3, I Molton 29/07/01 */ | 
 | 22 |  | 
 | 23 | #ifndef SI_TYPE_SIZE | 
 | 24 | #define SI_TYPE_SIZE 32 | 
 | 25 | #endif | 
 | 26 |  | 
 | 27 | #define __BITS4 (SI_TYPE_SIZE / 4) | 
 | 28 | #define __ll_B (1L << (SI_TYPE_SIZE / 2)) | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 29 | #define __ll_lowpart(t) ((u32) (t) % __ll_B) | 
 | 30 | #define __ll_highpart(t) ((u32) (t) / __ll_B) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  | 
 | 32 | /* Define auxiliary asm macros. | 
 | 33 |  | 
 | 34 |    1) umul_ppmm(high_prod, low_prod, multipler, multiplicand) | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 35 |    multiplies two u32 integers MULTIPLER and MULTIPLICAND, | 
 | 36 |    and generates a two-part u32 product in HIGH_PROD and | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |    LOW_PROD. | 
 | 38 |  | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 39 |    2) __umulsidi3(a,b) multiplies two u32 integers A and B, | 
 | 40 |    and returns a u64 product.  This is just a variant of umul_ppmm. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  | 
 | 42 |    3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator, | 
 | 43 |    denominator) divides a two-word unsigned integer, composed by the | 
 | 44 |    integers HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and | 
 | 45 |    places the quotient in QUOTIENT and the remainder in REMAINDER. | 
 | 46 |    HIGH_NUMERATOR must be less than DENOMINATOR for correct operation. | 
 | 47 |    If, in addition, the most significant bit of DENOMINATOR must be 1, | 
 | 48 |    then the pre-processor symbol UDIV_NEEDS_NORMALIZATION is defined to 1. | 
 | 49 |  | 
 | 50 |    4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator, | 
 | 51 |    denominator).  Like udiv_qrnnd but the numbers are signed.  The | 
 | 52 |    quotient is rounded towards 0. | 
 | 53 |  | 
 | 54 |    5) count_leading_zeros(count, x) counts the number of zero-bits from | 
 | 55 |    the msb to the first non-zero bit.  This is the number of steps X | 
 | 56 |    needs to be shifted left to set the msb.  Undefined for X == 0. | 
 | 57 |  | 
 | 58 |    6) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1, | 
 | 59 |    high_addend_2, low_addend_2) adds two two-word unsigned integers, | 
 | 60 |    composed by HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and | 
 | 61 |    LOW_ADDEND_2 respectively.  The result is placed in HIGH_SUM and | 
 | 62 |    LOW_SUM.  Overflow (i.e. carry out) is not stored anywhere, and is | 
 | 63 |    lost. | 
 | 64 |  | 
 | 65 |    7) sub_ddmmss(high_difference, low_difference, high_minuend, | 
 | 66 |    low_minuend, high_subtrahend, low_subtrahend) subtracts two | 
 | 67 |    two-word unsigned integers, composed by HIGH_MINUEND_1 and | 
 | 68 |    LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and LOW_SUBTRAHEND_2 | 
 | 69 |    respectively.  The result is placed in HIGH_DIFFERENCE and | 
 | 70 |    LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere, | 
 | 71 |    and is lost. | 
 | 72 |  | 
 | 73 |    If any of these macros are left undefined for a particular CPU, | 
 | 74 |    C macros are used.  */ | 
 | 75 |  | 
 | 76 | #if defined (__arm__) | 
 | 77 | #define add_ssaaaa(sh, sl, ah, al, bh, bl) \ | 
 | 78 |   __asm__ ("adds	%1, %4, %5					\n\ | 
 | 79 | 	adc	%0, %2, %3"						\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 80 | 	   : "=r" ((u32) (sh)),					\ | 
 | 81 | 	     "=&r" ((u32) (sl))					\ | 
 | 82 | 	   : "%r" ((u32) (ah)),					\ | 
 | 83 | 	     "rI" ((u32) (bh)),					\ | 
 | 84 | 	     "%r" ((u32) (al)),					\ | 
 | 85 | 	     "rI" ((u32) (bl))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #define sub_ddmmss(sh, sl, ah, al, bh, bl) \ | 
 | 87 |   __asm__ ("subs	%1, %4, %5					\n\ | 
 | 88 | 	sbc	%0, %2, %3"						\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 89 | 	   : "=r" ((u32) (sh)),					\ | 
 | 90 | 	     "=&r" ((u32) (sl))					\ | 
 | 91 | 	   : "r" ((u32) (ah)),					\ | 
 | 92 | 	     "rI" ((u32) (bh)),					\ | 
 | 93 | 	     "r" ((u32) (al)),					\ | 
 | 94 | 	     "rI" ((u32) (bl))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | #define umul_ppmm(xh, xl, a, b) \ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 96 | {register u32 __t0, __t1, __t2;					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 |   __asm__ ("%@ Inlined umul_ppmm					\n\ | 
 | 98 | 	mov	%2, %5, lsr #16						\n\ | 
 | 99 | 	mov	%0, %6, lsr #16						\n\ | 
 | 100 | 	bic	%3, %5, %2, lsl #16					\n\ | 
 | 101 | 	bic	%4, %6, %0, lsl #16					\n\ | 
 | 102 | 	mul	%1, %3, %4						\n\ | 
 | 103 | 	mul	%4, %2, %4						\n\ | 
 | 104 | 	mul	%3, %0, %3						\n\ | 
 | 105 | 	mul	%0, %2, %0						\n\ | 
 | 106 | 	adds	%3, %4, %3						\n\ | 
 | 107 | 	addcs	%0, %0, #65536						\n\ | 
 | 108 | 	adds	%1, %1, %3, lsl #16					\n\ | 
 | 109 | 	adc	%0, %0, %3, lsr #16"					\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 110 | 	   : "=&r" ((u32) (xh)),					\ | 
 | 111 | 	     "=r" ((u32) (xl)),					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | 	     "=&r" (__t0), "=&r" (__t1), "=r" (__t2)			\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 113 | 	   : "r" ((u32) (a)),					\ | 
 | 114 | 	     "r" ((u32) (b)));} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | #define UMUL_TIME 20 | 
 | 116 | #define UDIV_TIME 100 | 
| Russell King | 3ade2fe | 2005-06-20 16:45:32 +0100 | [diff] [blame] | 117 | #endif				/* __arm__ */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 |  | 
 | 119 | #define __umulsidi3(u, v) \ | 
 | 120 |   ({DIunion __w;							\ | 
 | 121 |     umul_ppmm (__w.s.high, __w.s.low, u, v);				\ | 
 | 122 |     __w.ll; }) | 
 | 123 |  | 
 | 124 | #define __udiv_qrnnd_c(q, r, n1, n0, d) \ | 
 | 125 |   do {									\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 126 |     u32 __d1, __d0, __q1, __q0;					\ | 
 | 127 |     u32 __r1, __r0, __m;						\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 |     __d1 = __ll_highpart (d);						\ | 
 | 129 |     __d0 = __ll_lowpart (d);						\ | 
 | 130 | 									\ | 
 | 131 |     __r1 = (n1) % __d1;							\ | 
 | 132 |     __q1 = (n1) / __d1;							\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 133 |     __m = (u32) __q1 * __d0;					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 |     __r1 = __r1 * __ll_B | __ll_highpart (n0);				\ | 
 | 135 |     if (__r1 < __m)							\ | 
 | 136 |       {									\ | 
 | 137 | 	__q1--, __r1 += (d);						\ | 
 | 138 | 	if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\ | 
 | 139 | 	  if (__r1 < __m)						\ | 
 | 140 | 	    __q1--, __r1 += (d);					\ | 
 | 141 |       }									\ | 
 | 142 |     __r1 -= __m;							\ | 
 | 143 | 									\ | 
 | 144 |     __r0 = __r1 % __d1;							\ | 
 | 145 |     __q0 = __r1 / __d1;							\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 146 |     __m = (u32) __q0 * __d0;					\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 |     __r0 = __r0 * __ll_B | __ll_lowpart (n0);				\ | 
 | 148 |     if (__r0 < __m)							\ | 
 | 149 |       {									\ | 
 | 150 | 	__q0--, __r0 += (d);						\ | 
 | 151 | 	if (__r0 >= (d))						\ | 
 | 152 | 	  if (__r0 < __m)						\ | 
 | 153 | 	    __q0--, __r0 += (d);					\ | 
 | 154 |       }									\ | 
 | 155 |     __r0 -= __m;							\ | 
 | 156 | 									\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 157 |     (q) = (u32) __q1 * __ll_B | __q0;				\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 |     (r) = __r0;								\ | 
 | 159 |   } while (0) | 
 | 160 |  | 
 | 161 | #define UDIV_NEEDS_NORMALIZATION 1 | 
 | 162 | #define udiv_qrnnd __udiv_qrnnd_c | 
 | 163 |  | 
 | 164 | #define count_leading_zeros(count, x) \ | 
 | 165 |   do {									\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 166 |     u32 __xr = (x);							\ | 
 | 167 |     u32 __a;							\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | 									\ | 
 | 169 |     if (SI_TYPE_SIZE <= 32)						\ | 
 | 170 |       {									\ | 
| Russell King | f29481c | 2005-06-20 15:49:59 +0100 | [diff] [blame] | 171 | 	__a = __xr < ((u32)1<<2*__BITS4)				\ | 
 | 172 | 	  ? (__xr < ((u32)1<<__BITS4) ? 0 : __BITS4)		\ | 
 | 173 | 	  : (__xr < ((u32)1<<3*__BITS4) ?  2*__BITS4 : 3*__BITS4);	\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 |       }									\ | 
 | 175 |     else								\ | 
 | 176 |       {									\ | 
 | 177 | 	for (__a = SI_TYPE_SIZE - 8; __a > 0; __a -= 8)			\ | 
 | 178 | 	  if (((__xr >> __a) & 0xff) != 0)				\ | 
 | 179 | 	    break;							\ | 
 | 180 |       }									\ | 
 | 181 | 									\ | 
 | 182 |     (count) = SI_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a);		\ | 
 | 183 |   } while (0) |