| 1 |
/* -*- linux-c -*- */ |
| 2 |
/* Math functions |
| 3 |
* Copyright (C) 2005 Red Hat Inc. |
| 4 |
* Portions (C) Free Software Foundation, Inc. |
| 5 |
* |
| 6 |
* This file is part of systemtap, and is free software. You can |
| 7 |
* redistribute it and/or modify it under the terms of the GNU General |
| 8 |
* Public License (GPL); either version 2, or (at your option) any |
| 9 |
* later version. |
| 10 |
*/ |
| 11 |
|
| 12 |
#ifndef _STAPLINUX_ARITH_C_ |
| 13 |
#define _STAPLINUX_ARITH_C_ |
| 14 |
|
| 15 |
/** @file arith. |
| 16 |
* @brief Implements various arithmetic-related helper functions |
| 17 |
*/ |
| 18 |
|
| 19 |
|
| 20 |
/* 64-bit division for 64-bit cpus and i386 */ |
| 21 |
/* Other 32-bit cpus will need to modify this file. */ |
| 22 |
|
| 23 |
#if defined (__i386__) || defined(__arm__) || \ |
| 24 |
(defined(__powerpc__) && !defined(__powerpc64__)) || \ |
| 25 |
(defined(__mips__) && !defined(__mips64)) |
| 26 |
static long long _div64 (long long u, long long v); |
| 27 |
static long long _mod64 (long long u, long long v); |
| 28 |
#endif |
| 29 |
|
| 30 |
/* 31 bit s390 suupport is not yet included, it may never be. |
| 31 |
#ifdef __s390__ |
| 32 |
static long long _div64 (long long u, long long v); |
| 33 |
static long long _mod64 (long long u, long long v); |
| 34 |
#endif |
| 35 |
*/ |
| 36 |
|
| 37 |
/** Divide x by y. In case of division-by-zero, |
| 38 |
* set context error string, and return 0 |
| 39 |
*/ |
| 40 |
static int64_t _stp_div64 (const char **error, int64_t x, int64_t y) |
| 41 |
{ |
| 42 |
// check for division-by-zero |
| 43 |
if (unlikely (y == 0)) { |
| 44 |
if (error) *error = "division by 0"; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
if (unlikely (y == -1)) |
| 49 |
return -x; |
| 50 |
|
| 51 |
#if defined (__LP64__) |
| 52 |
return x/y; |
| 53 |
#else |
| 54 |
if (likely ((x >= LONG_MIN && x <= LONG_MAX) && |
| 55 |
(y >= LONG_MIN && y <= LONG_MAX))) { |
| 56 |
return (long)x / (long)y; |
| 57 |
} else |
| 58 |
return _div64 (x, y); |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** Modulo x by y. In case of division-by-zero, |
| 64 |
* set context error string, and return any 0 |
| 65 |
*/ |
| 66 |
static int64_t _stp_mod64 (const char **error, int64_t x, int64_t y) |
| 67 |
{ |
| 68 |
// check for division-by-zero |
| 69 |
if (unlikely (y == 0)) { |
| 70 |
if (error) *error = "division by 0"; |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
if (unlikely (y == 1 || y == -1)) |
| 75 |
return 0; |
| 76 |
|
| 77 |
#if defined (__LP64__) |
| 78 |
return x%y; |
| 79 |
#else |
| 80 |
if (likely ((x >= LONG_MIN && x <= LONG_MAX) && |
| 81 |
(y >= LONG_MIN && y <= LONG_MAX))) { |
| 82 |
return (long)x % (long)y; |
| 83 |
} else |
| 84 |
return _mod64 (x, y); |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** Return a random integer between 0 and n - 1. |
| 90 |
* @param n how far from zero to go. Make it positive but less than a million or so. |
| 91 |
*/ |
| 92 |
static unsigned long _stp_random_u (unsigned long n) |
| 93 |
{ |
| 94 |
static unsigned long seed; |
| 95 |
static int initialized_p = 0; |
| 96 |
|
| 97 |
if (unlikely (! initialized_p)) { |
| 98 |
seed = (unsigned long) jiffies; |
| 99 |
initialized_p = 1; |
| 100 |
} |
| 101 |
|
| 102 |
/* from glibc rand man page */ |
| 103 |
seed = seed * 1103515245 + 12345; |
| 104 |
|
| 105 |
return (n == 0 ? 0 : seed % n); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** Return a random integer between -n and n. |
| 110 |
* @param n how far from zero to go. Make it positive but less than a million or so. |
| 111 |
*/ |
| 112 |
static int _stp_random_pm (unsigned n) |
| 113 |
{ |
| 114 |
return -(int)n + (int)_stp_random_u (2*n + 1); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#if defined (__i386__) || defined (__arm__) || \ |
| 120 |
(defined(__powerpc__) && !defined(__powerpc64__)) || \ |
| 121 |
(defined(__mips__) && !defined(__mips64)) |
| 122 |
|
| 123 |
/* 64-bit division functions extracted from libgcc */ |
| 124 |
typedef long long DWtype; |
| 125 |
typedef unsigned long long UDWtype; |
| 126 |
typedef unsigned long UWtype; |
| 127 |
typedef long Wtype; |
| 128 |
typedef unsigned int USItype; |
| 129 |
typedef unsigned int UQItype __attribute__ ((mode (QI))); |
| 130 |
typedef unsigned int UDItype __attribute__ ((mode (DI))); |
| 131 |
|
| 132 |
#ifdef _BIG_ENDIAN |
| 133 |
struct DWstruct {Wtype high, low;}; |
| 134 |
#else |
| 135 |
struct DWstruct {Wtype low, high;}; |
| 136 |
#endif |
| 137 |
|
| 138 |
#define __CLOBBER_CC : "cc" |
| 139 |
|
| 140 |
#define W_TYPE_SIZE 32 |
| 141 |
|
| 142 |
#define __BITS4 (W_TYPE_SIZE / 4) |
| 143 |
#define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2)) |
| 144 |
#define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1)) |
| 145 |
#define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2)) |
| 146 |
|
| 147 |
typedef union |
| 148 |
{ |
| 149 |
struct DWstruct s; |
| 150 |
DWtype ll; |
| 151 |
} DWunion; |
| 152 |
|
| 153 |
|
| 154 |
#if defined (__i386__) |
| 155 |
/* these are the i386 versions of these macros from gcc/longlong.h */ |
| 156 |
|
| 157 |
#define umul_ppmm(w1, w0, u, v) \ |
| 158 |
__asm__ ("mull %3" \ |
| 159 |
: "=a" ((USItype) (w0)), \ |
| 160 |
"=d" ((USItype) (w1)) \ |
| 161 |
: "%0" ((USItype) (u)), \ |
| 162 |
"rm" ((USItype) (v))) |
| 163 |
|
| 164 |
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \ |
| 165 |
__asm__ ("subl %5,%1\n\tsbbl %3,%0" \ |
| 166 |
: "=r" ((USItype) (sh)), \ |
| 167 |
"=&r" ((USItype) (sl)) \ |
| 168 |
: "0" ((USItype) (ah)), \ |
| 169 |
"g" ((USItype) (bh)), \ |
| 170 |
"1" ((USItype) (al)), \ |
| 171 |
"g" ((USItype) (bl))) |
| 172 |
|
| 173 |
#define udiv_qrnnd(q, r, n1, n0, dv) \ |
| 174 |
__asm__ ("divl %4" \ |
| 175 |
: "=a" ((USItype) (q)), \ |
| 176 |
"=d" ((USItype) (r)) \ |
| 177 |
: "0" ((USItype) (n0)), \ |
| 178 |
"1" ((USItype) (n1)), \ |
| 179 |
"rm" ((USItype) (dv))) |
| 180 |
|
| 181 |
#define count_leading_zeros(count, x) \ |
| 182 |
do { \ |
| 183 |
USItype __cbtmp; \ |
| 184 |
__asm__ ("bsrl %1,%0" \ |
| 185 |
: "=r" (__cbtmp) : "rm" ((USItype) (x))); \ |
| 186 |
(count) = __cbtmp ^ 31; \ |
| 187 |
} while (0) |
| 188 |
|
| 189 |
#elif defined (__powerpc__) |
| 190 |
/* these are the ppc versions of these macros from gcc/longlong.h */ |
| 191 |
|
| 192 |
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \ |
| 193 |
do { \ |
| 194 |
if (__builtin_constant_p (ah) && (ah) == 0) \ |
| 195 |
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2" \ |
| 196 |
: "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\ |
| 197 |
else if (__builtin_constant_p (ah) && (ah) == ~(USItype) 0) \ |
| 198 |
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2" \ |
| 199 |
: "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\ |
| 200 |
else if (__builtin_constant_p (bh) && (bh) == 0) \ |
| 201 |
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \ |
| 202 |
: "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\ |
| 203 |
else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0) \ |
| 204 |
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \ |
| 205 |
: "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\ |
| 206 |
else \ |
| 207 |
__asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2" \ |
| 208 |
: "=r" (sh), "=&r" (sl) \ |
| 209 |
: "r" (ah), "r" (bh), "rI" (al), "r" (bl)); \ |
| 210 |
} while (0) |
| 211 |
|
| 212 |
#define count_leading_zeros(count, x) \ |
| 213 |
__asm__ ("{cntlz|cntlzw} %0,%1" : "=r" (count) : "r" (x)) |
| 214 |
#define COUNT_LEADING_ZEROS_0 32 |
| 215 |
|
| 216 |
#define umul_ppmm(ph, pl, m0, m1) \ |
| 217 |
do { \ |
| 218 |
USItype __m0 = (m0), __m1 = (m1); \ |
| 219 |
__asm__ ("mulhwu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \ |
| 220 |
(pl) = __m0 * __m1; \ |
| 221 |
} while (0) |
| 222 |
|
| 223 |
#elif defined (__arm__) |
| 224 |
|
| 225 |
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \ |
| 226 |
__asm__ ("subs %1, %4, %5\n\tsbc %0, %2, %3" \ |
| 227 |
: "=r" ((USItype) (sh)), \ |
| 228 |
"=&r" ((USItype) (sl)) \ |
| 229 |
: "r" ((USItype) (ah)), \ |
| 230 |
"rI" ((USItype) (bh)), \ |
| 231 |
"r" ((USItype) (al)), \ |
| 232 |
"rI" ((USItype) (bl)) __CLOBBER_CC) |
| 233 |
#define umul_ppmm(xh, xl, a, b) \ |
| 234 |
{register USItype __t0, __t1, __t2; \ |
| 235 |
__asm__ ("%@ Inlined umul_ppmm\n" \ |
| 236 |
" mov %2, %5, lsr #16\n" \ |
| 237 |
" mov %0, %6, lsr #16\n" \ |
| 238 |
" bic %3, %5, %2, lsl #16\n" \ |
| 239 |
" bic %4, %6, %0, lsl #16\n" \ |
| 240 |
" mul %1, %3, %4\n" \ |
| 241 |
" mul %4, %2, %4\n" \ |
| 242 |
" mul %3, %0, %3\n" \ |
| 243 |
" mul %0, %2, %0\n" \ |
| 244 |
" adds %3, %4, %3\n" \ |
| 245 |
" addcs %0, %0, #65536\n" \ |
| 246 |
" adds %1, %1, %3, lsl #16\n" \ |
| 247 |
" adc %0, %0, %3, lsr #16" \ |
| 248 |
: "=&r" ((USItype) (xh)), \ |
| 249 |
"=r" ((USItype) (xl)), \ |
| 250 |
"=&r" (__t0), "=&r" (__t1), "=r" (__t2) \ |
| 251 |
: "r" ((USItype) (a)), \ |
| 252 |
"r" ((USItype) (b)) __CLOBBER_CC );} |
| 253 |
|
| 254 |
#elif (defined(__mips) && __mips >= 3) && W_TYPE_SIZE == 64 |
| 255 |
#if (__GNUC__ >= 5) || (__GNUC__ >= 4 && __GNUC_MINOR__ >= 4) |
| 256 |
#define umul_ppmm(w1, w0, u, v) \ |
| 257 |
do { \ |
| 258 |
typedef unsigned int __ll_UTItype __attribute__((mode(TI))); \ |
| 259 |
__ll_UTItype __ll = (__ll_UTItype)(u) * (v); \ |
| 260 |
w1 = __ll >> 64; \ |
| 261 |
w0 = __ll; \ |
| 262 |
} while (0) |
| 263 |
#elif __GNUC__ > 2 || __GNUC_MINOR__ >= 7 |
| 264 |
#define umul_ppmm(w1, w0, u, v) \ |
| 265 |
__asm__ ("dmultu %2,%3" \ |
| 266 |
: "=l" ((UDItype)(w0)), \ |
| 267 |
"=h" ((UDItype)(w1)) \ |
| 268 |
: "d" ((UDItype)(u)), \ |
| 269 |
"d" ((UDItype)(v))) |
| 270 |
#else |
| 271 |
#define umul_ppmm(w1, w0, u, v) \ |
| 272 |
__asm__ ("dmultu %2,%3\n" \ |
| 273 |
"mflo %0\n" \ |
| 274 |
"mfhi %1" \ |
| 275 |
: "=d" ((UDItype)(w0)), \ |
| 276 |
"=d" ((UDItype)(w1)) \ |
| 277 |
: "d" ((UDItype)(u)), \ |
| 278 |
"d" ((UDItype)(v))) |
| 279 |
#endif |
| 280 |
/* This comes from lib/mpi/longlong.h */ |
| 281 |
#elif defined(__mips__) && W_TYPE_SIZE == 32 |
| 282 |
#if (__GNUC__ >= 5) || (__GNUC__ >= 4 && __GNUC_MINOR__ >= 4) |
| 283 |
#define umul_ppmm(w1, w0, u, v) \ |
| 284 |
do { \ |
| 285 |
UDItype __ll = (UDItype)(u) * (v); \ |
| 286 |
w1 = __ll >> 32; \ |
| 287 |
w0 = __ll; \ |
| 288 |
} while (0) |
| 289 |
#elif __GNUC__ > 2 || __GNUC_MINOR__ >= 7 |
| 290 |
#define umul_ppmm(w1, w0, u, v) \ |
| 291 |
__asm__ ("multu %2,%3" \ |
| 292 |
: "=l" ((USItype)(w0)), \ |
| 293 |
"=h" ((USItype)(w1)) \ |
| 294 |
: "d" ((USItype)(u)), \ |
| 295 |
"d" ((USItype)(v))) |
| 296 |
#else |
| 297 |
#define umul_ppmm(w1, w0, u, v) \ |
| 298 |
__asm__ ("multu %2,%3\n" \ |
| 299 |
"mflo %0\n" \ |
| 300 |
"mfhi %1" \ |
| 301 |
: "=d" ((USItype)(w0)), \ |
| 302 |
"=d" ((USItype)(w1)) \ |
| 303 |
: "d" ((USItype)(u)), \ |
| 304 |
"d" ((USItype)(v))) |
| 305 |
#endif |
| 306 |
|
| 307 |
#if !defined (sub_ddmmss) |
| 308 |
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \ |
| 309 |
do { \ |
| 310 |
UWtype __x; \ |
| 311 |
__x = (al) - (bl); \ |
| 312 |
(sh) = (ah) - (bh) - (__x > (al)); \ |
| 313 |
(sl) = __x; \ |
| 314 |
} while (0) |
| 315 |
#endif |
| 316 |
|
| 317 |
#elif (defined(__mips) && __mips >= 3) && W_TYPE_SIZE == 64 |
| 318 |
#if (__GNUC__ >= 5) || (__GNUC__ >= 4 && __GNUC_MINOR__ >= 4) |
| 319 |
#define umul_ppmm(w1, w0, u, v) \ |
| 320 |
do { \ |
| 321 |
typedef unsigned int __ll_UTItype __attribute__((mode(TI))); \ |
| 322 |
__ll_UTItype __ll = (__ll_UTItype)(u) * (v); \ |
| 323 |
w1 = __ll >> 64; \ |
| 324 |
w0 = __ll; \ |
| 325 |
} while (0) |
| 326 |
#elif __GNUC__ > 2 || __GNUC_MINOR__ >= 7 |
| 327 |
#define umul_ppmm(w1, w0, u, v) \ |
| 328 |
__asm__ ("dmultu %2,%3" \ |
| 329 |
: "=l" ((UDItype)(w0)), \ |
| 330 |
"=h" ((UDItype)(w1)) \ |
| 331 |
: "d" ((UDItype)(u)), \ |
| 332 |
"d" ((UDItype)(v))) |
| 333 |
#else |
| 334 |
#define umul_ppmm(w1, w0, u, v) \ |
| 335 |
__asm__ ("dmultu %2,%3\n" \ |
| 336 |
"mflo %0\n" \ |
| 337 |
"mfhi %1" \ |
| 338 |
: "=d" ((UDItype)(w0)), \ |
| 339 |
"=d" ((UDItype)(w1)) \ |
| 340 |
: "d" ((UDItype)(u)), \ |
| 341 |
"d" ((UDItype)(v))) |
| 342 |
#endif |
| 343 |
#endif |
| 344 |
|
| 345 |
#define __udiv_qrnnd_c(q, r, n1, n0, d) \ |
| 346 |
do { \ |
| 347 |
UWtype __d1, __d0, __q1, __q0; \ |
| 348 |
UWtype __r1, __r0, __m; \ |
| 349 |
__d1 = __ll_highpart (d); \ |
| 350 |
__d0 = __ll_lowpart (d); \ |
| 351 |
\ |
| 352 |
__r1 = (n1) % __d1; \ |
| 353 |
__q1 = (n1) / __d1; \ |
| 354 |
__m = (UWtype) __q1 * __d0; \ |
| 355 |
__r1 = __r1 * __ll_B | __ll_highpart (n0); \ |
| 356 |
if (__r1 < __m) \ |
| 357 |
{ \ |
| 358 |
__q1--, __r1 += (d); \ |
| 359 |
if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\ |
| 360 |
if (__r1 < __m) \ |
| 361 |
__q1--, __r1 += (d); \ |
| 362 |
} \ |
| 363 |
__r1 -= __m; \ |
| 364 |
\ |
| 365 |
__r0 = __r1 % __d1; \ |
| 366 |
__q0 = __r1 / __d1; \ |
| 367 |
__m = (UWtype) __q0 * __d0; \ |
| 368 |
__r0 = __r0 * __ll_B | __ll_lowpart (n0); \ |
| 369 |
if (__r0 < __m) \ |
| 370 |
{ \ |
| 371 |
__q0--, __r0 += (d); \ |
| 372 |
if (__r0 >= (d)) \ |
| 373 |
if (__r0 < __m) \ |
| 374 |
__q0--, __r0 += (d); \ |
| 375 |
} \ |
| 376 |
__r0 -= __m; \ |
| 377 |
\ |
| 378 |
(q) = (UWtype) __q1 * __ll_B | __q0; \ |
| 379 |
(r) = __r0; \ |
| 380 |
} while (0) |
| 381 |
|
| 382 |
#if !defined (udiv_qrnnd) |
| 383 |
#define UDIV_NEEDS_NORMALIZATION 1 |
| 384 |
#define udiv_qrnnd __udiv_qrnnd_c |
| 385 |
#else |
| 386 |
#define UDIV_NEEDS_NORMALIZATION 0 |
| 387 |
#endif |
| 388 |
|
| 389 |
#if !defined (count_leading_zeros) |
| 390 |
static const UQItype _stp_clz_tab[256] = |
| 391 |
{ |
| 392 |
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
| 393 |
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
| 394 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 395 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 396 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 397 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 398 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 399 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 |
| 400 |
}; |
| 401 |
|
| 402 |
#define count_leading_zeros(count, x) \ |
| 403 |
do { \ |
| 404 |
UWtype __xr = (x); \ |
| 405 |
UWtype __a; \ |
| 406 |
\ |
| 407 |
if (W_TYPE_SIZE <= 32) \ |
| 408 |
{ \ |
| 409 |
__a = __xr < ((UWtype)1<<2*__BITS4) \ |
| 410 |
? (__xr < ((UWtype)1<<__BITS4) ? 0 : __BITS4) \ |
| 411 |
: (__xr < ((UWtype)1<<3*__BITS4) ? 2*__BITS4 : 3*__BITS4); \ |
| 412 |
} \ |
| 413 |
else \ |
| 414 |
{ \ |
| 415 |
for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8) \ |
| 416 |
if (((__xr >> __a) & 0xff) != 0) \ |
| 417 |
break; \ |
| 418 |
} \ |
| 419 |
\ |
| 420 |
(count) = W_TYPE_SIZE - (_stp_clz_tab[__xr >> __a] + __a); \ |
| 421 |
} while (0) |
| 422 |
#define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE |
| 423 |
#endif |
| 424 |
|
| 425 |
static UDWtype |
| 426 |
_stp_udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp) |
| 427 |
{ |
| 428 |
const DWunion nn = {.ll = n}; |
| 429 |
const DWunion dd = {.ll = d}; |
| 430 |
DWunion ww, rr; |
| 431 |
UWtype d0, d1, n0, n1, n2; |
| 432 |
UWtype q0, q1; |
| 433 |
UWtype b, bm; |
| 434 |
|
| 435 |
d0 = dd.s.low; |
| 436 |
d1 = dd.s.high; |
| 437 |
n0 = nn.s.low; |
| 438 |
n1 = nn.s.high; |
| 439 |
|
| 440 |
#if !UDIV_NEEDS_NORMALIZATION |
| 441 |
if (d1 == 0) |
| 442 |
{ |
| 443 |
if (d0 > n1) |
| 444 |
{ |
| 445 |
/* 0q = nn / 0D */ |
| 446 |
|
| 447 |
udiv_qrnnd (q0, n0, n1, n0, d0); |
| 448 |
q1 = 0; |
| 449 |
|
| 450 |
/* Remainder in n0. */ |
| 451 |
} |
| 452 |
else |
| 453 |
{ |
| 454 |
/* qq = NN / 0d */ |
| 455 |
|
| 456 |
if (d0 == 0) |
| 457 |
d0 = 1 / d0; /* Divide intentionally by zero. */ |
| 458 |
|
| 459 |
udiv_qrnnd (q1, n1, 0, n1, d0); |
| 460 |
udiv_qrnnd (q0, n0, n1, n0, d0); |
| 461 |
|
| 462 |
/* Remainder in n0. */ |
| 463 |
} |
| 464 |
|
| 465 |
if (rp != 0) |
| 466 |
{ |
| 467 |
rr.s.low = n0; |
| 468 |
rr.s.high = 0; |
| 469 |
*rp = rr.ll; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
#else /* UDIV_NEEDS_NORMALIZATION */ |
| 474 |
|
| 475 |
if (d1 == 0) |
| 476 |
{ |
| 477 |
if (d0 > n1) |
| 478 |
{ |
| 479 |
/* 0q = nn / 0D */ |
| 480 |
|
| 481 |
count_leading_zeros (bm, d0); |
| 482 |
|
| 483 |
if (bm != 0) |
| 484 |
{ |
| 485 |
/* Normalize, i.e. make the most significant bit of the |
| 486 |
denominator set. */ |
| 487 |
|
| 488 |
d0 = d0 << bm; |
| 489 |
n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm)); |
| 490 |
n0 = n0 << bm; |
| 491 |
} |
| 492 |
|
| 493 |
udiv_qrnnd (q0, n0, n1, n0, d0); |
| 494 |
q1 = 0; |
| 495 |
|
| 496 |
/* Remainder in n0 >> bm. */ |
| 497 |
} |
| 498 |
else |
| 499 |
{ |
| 500 |
/* qq = NN / 0d */ |
| 501 |
|
| 502 |
if (d0 == 0) |
| 503 |
d0 = 1 / d0; /* Divide intentionally by zero. */ |
| 504 |
|
| 505 |
count_leading_zeros (bm, d0); |
| 506 |
|
| 507 |
if (bm == 0) |
| 508 |
{ |
| 509 |
/* From (n1 >= d0) /\ (the most significant bit of d0 is set), |
| 510 |
conclude (the most significant bit of n1 is set) /\ (the |
| 511 |
leading quotient digit q1 = 1). |
| 512 |
|
| 513 |
This special case is necessary, not an optimization. |
| 514 |
(Shifts counts of W_TYPE_SIZE are undefined.) */ |
| 515 |
|
| 516 |
n1 -= d0; |
| 517 |
q1 = 1; |
| 518 |
} |
| 519 |
else |
| 520 |
{ |
| 521 |
/* Normalize. */ |
| 522 |
|
| 523 |
b = W_TYPE_SIZE - bm; |
| 524 |
|
| 525 |
d0 = d0 << bm; |
| 526 |
n2 = n1 >> b; |
| 527 |
n1 = (n1 << bm) | (n0 >> b); |
| 528 |
n0 = n0 << bm; |
| 529 |
|
| 530 |
udiv_qrnnd (q1, n1, n2, n1, d0); |
| 531 |
} |
| 532 |
|
| 533 |
/* n1 != d0... */ |
| 534 |
|
| 535 |
udiv_qrnnd (q0, n0, n1, n0, d0); |
| 536 |
|
| 537 |
/* Remainder in n0 >> bm. */ |
| 538 |
} |
| 539 |
|
| 540 |
if (rp != 0) |
| 541 |
{ |
| 542 |
rr.s.low = n0 >> bm; |
| 543 |
rr.s.high = 0; |
| 544 |
*rp = rr.ll; |
| 545 |
} |
| 546 |
} |
| 547 |
#endif /* UDIV_NEEDS_NORMALIZATION */ |
| 548 |
|
| 549 |
else |
| 550 |
{ |
| 551 |
if (d1 > n1) |
| 552 |
{ |
| 553 |
/* 00 = nn / DD */ |
| 554 |
|
| 555 |
q0 = 0; |
| 556 |
q1 = 0; |
| 557 |
|
| 558 |
/* Remainder in n1n0. */ |
| 559 |
if (rp != 0) |
| 560 |
{ |
| 561 |
rr.s.low = n0; |
| 562 |
rr.s.high = n1; |
| 563 |
*rp = rr.ll; |
| 564 |
} |
| 565 |
} |
| 566 |
else |
| 567 |
{ |
| 568 |
/* 0q = NN / dd */ |
| 569 |
|
| 570 |
count_leading_zeros (bm, d1); |
| 571 |
if (bm == 0) |
| 572 |
{ |
| 573 |
/* From (n1 >= d1) /\ (the most significant bit of d1 is set), |
| 574 |
conclude (the most significant bit of n1 is set) /\ (the |
| 575 |
quotient digit q0 = 0 or 1). |
| 576 |
|
| 577 |
This special case is necessary, not an optimization. */ |
| 578 |
|
| 579 |
/* The condition on the next line takes advantage of that |
| 580 |
n1 >= d1 (true due to program flow). */ |
| 581 |
if (n1 > d1 || n0 >= d0) |
| 582 |
{ |
| 583 |
q0 = 1; |
| 584 |
sub_ddmmss (n1, n0, n1, n0, d1, d0); |
| 585 |
} |
| 586 |
else |
| 587 |
q0 = 0; |
| 588 |
|
| 589 |
q1 = 0; |
| 590 |
|
| 591 |
if (rp != 0) |
| 592 |
{ |
| 593 |
rr.s.low = n0; |
| 594 |
rr.s.high = n1; |
| 595 |
*rp = rr.ll; |
| 596 |
} |
| 597 |
} |
| 598 |
else |
| 599 |
{ |
| 600 |
UWtype m1, m0; |
| 601 |
/* Normalize. */ |
| 602 |
|
| 603 |
b = W_TYPE_SIZE - bm; |
| 604 |
|
| 605 |
d1 = (d1 << bm) | (d0 >> b); |
| 606 |
d0 = d0 << bm; |
| 607 |
n2 = n1 >> b; |
| 608 |
n1 = (n1 << bm) | (n0 >> b); |
| 609 |
n0 = n0 << bm; |
| 610 |
|
| 611 |
udiv_qrnnd (q0, n1, n2, n1, d1); |
| 612 |
umul_ppmm (m1, m0, q0, d0); |
| 613 |
|
| 614 |
if (m1 > n1 || (m1 == n1 && m0 > n0)) |
| 615 |
{ |
| 616 |
q0--; |
| 617 |
sub_ddmmss (m1, m0, m1, m0, d1, d0); |
| 618 |
} |
| 619 |
|
| 620 |
q1 = 0; |
| 621 |
|
| 622 |
/* Remainder in (n1n0 - m1m0) >> bm. */ |
| 623 |
if (rp != 0) |
| 624 |
{ |
| 625 |
sub_ddmmss (n1, n0, n1, n0, m1, m0); |
| 626 |
rr.s.low = (n1 << b) | (n0 >> bm); |
| 627 |
rr.s.high = n1 >> bm; |
| 628 |
*rp = rr.ll; |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
ww.s.low = q0; ww.s.high = q1; |
| 635 |
return ww.ll; |
| 636 |
} |
| 637 |
|
| 638 |
static long long _div64 (long long u, long long v) |
| 639 |
{ |
| 640 |
long c = 0; |
| 641 |
DWunion uu = {.ll = u}; |
| 642 |
DWunion vv = {.ll = v}; |
| 643 |
DWtype w; |
| 644 |
|
| 645 |
if (uu.s.high < 0) |
| 646 |
c = ~c, |
| 647 |
uu.ll = -uu.ll; |
| 648 |
if (vv.s.high < 0) |
| 649 |
c = ~c, |
| 650 |
vv.ll = -vv.ll; |
| 651 |
|
| 652 |
w = _stp_udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0); |
| 653 |
if (c) |
| 654 |
w = -w; |
| 655 |
|
| 656 |
return w; |
| 657 |
} |
| 658 |
|
| 659 |
static long long _mod64 (long long u, long long v) |
| 660 |
{ |
| 661 |
long c = 0; |
| 662 |
DWunion uu = {.ll = u}; |
| 663 |
DWunion vv = {.ll = v}; |
| 664 |
DWtype w; |
| 665 |
|
| 666 |
if (uu.s.high < 0) |
| 667 |
c = ~c, |
| 668 |
uu.ll = -uu.ll; |
| 669 |
if (vv.s.high < 0) |
| 670 |
vv.ll = -vv.ll; |
| 671 |
|
| 672 |
(void) _stp_udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w); |
| 673 |
if (c) |
| 674 |
w = -w; |
| 675 |
|
| 676 |
return w; |
| 677 |
} |
| 678 |
|
| 679 |
#endif /* __i386__ || __arm__ || (__powerpc__ && !__powerpc64__) */ |
| 680 |
|
| 681 |
#endif /* _STAPLINUX_ARITH_C_ */ |