/usr/include/zn_poly/wide_arith.h is in libzn-poly-dev 0.8-1.1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | /*
wide_arith.h: double-word arithmetic
Copyright (C) 2007, 2008, David Harvey
This file is part of the zn_poly library (version 0.8).
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
============================================================================
NOTE: this file includes code adapted from GMP 4.2.1 and NTL 5.4.1. Please
see the file COPYING for further information about licensing of this code.
*/
/*
This file defines macros:
ZNP_MUL_HI(hi, a, b)
computes the high word of a * b, where a and b are unsigned longs.
ZNP_MUL_WIDE(hi, lo, a, b)
computes the high and low words of a * b, where a and b are unsigned
longs.
ZNP_ADD_WIDE(s1, s0, a1, a0, b1, b0)
computes B*s1 + s0 = (B*a1 + a0) + (B*b1 + b0), where all the
variables are unsigned longs. Carry is discarded.
ZNP_SUB_WIDE(s1, s0, a1, a0, b1, b0)
computes B*s1 + s0 = (B*a1 + a0) - (B*b1 + b0), where all the
variables are unsigned longs. Borrow is discarded.
If using gcc, there are several inline assembly implementations.
*/
#ifndef ZNP_WIDE_ARITH_H
#define ZNP_WIDE_ARITH_H
#if ULONG_MAX == 4294967295U
#define UNSIGNED_LONG_BITS 32
#elif ULONG_MAX == 18446744073709551615U
#define UNSIGNED_LONG_BITS 64
#else
#error wide_arith requires that unsigned long is either 32 bits or 64 bits
#endif
#if (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)))
// To simplify things, we require gcc v2.7 or higher.
// ------ POWERPC ------
#if defined (_ARCH_PPC) || defined (__powerpc__) || defined (__POWERPC__) \
|| defined (__ppc__) || defined(__ppc64__) \
|| (defined (PPC) && ! defined (CPU_FAMILY)) /* gcc 2.7.x GNU&SysV */ \
|| (defined (PPC) && defined (CPU_FAMILY) /* VxWorks */ \
&& CPU_FAMILY == PPC)
#if (UNSIGNED_LONG_BITS == 32)
#define ZNP_MUL_HI(hi, a, b) \
__asm__ ("mulhwu %0,%1,%2" : "=r" (hi) : "%r" (a), "r" (b));
#define ZNP_ADD_WIDE(s1, s0, a1, a0, b1, b0) \
do { \
if (__builtin_constant_p (b1) && (b1) == 0) \
__asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (a1), "%r" (a0), "rI" (b0)); \
else if (__builtin_constant_p (b1) && (b1) == ~(unsigned long) 0) \
__asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (a1), "%r" (a0), "rI" (b0)); \
else \
__asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3" \
: "=r" (s1), "=&r" (s0) \
: "r" (a1), "r" (b1), "%r" (a0), "rI" (b0)); \
} while (0)
#define ZNP_SUB_WIDE(s1, s0, a1, a0, b1, b0) \
do { \
if (__builtin_constant_p (a1) && (a1) == 0) \
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (b1), "rI" (a0), "r" (b0)); \
else if (__builtin_constant_p (a1) && (a1) == ~(unsigned long) 0) \
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (b1), "rI" (a0), "r" (b0)); \
else if (__builtin_constant_p (b1) && (b1) == 0) \
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (a1), "rI" (a0), "r" (b0)); \
else if (__builtin_constant_p (b1) && (b1) == ~(unsigned long) 0) \
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (a1), "rI" (a0), "r" (b0)); \
else \
__asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2" \
: "=r" (s1), "=&r" (s0) \
: "r" (a1), "r" (b1), "rI" (a0), "r" (b0)); \
} while (0)
#elif (UNSIGNED_LONG_BITS == 64)
#define ZNP_MUL_HI(hi, a, b) \
__asm__ ("mulhdu %0,%1,%2" : "=r" (hi) : "%r" (a), "r" (b));
#define ZNP_ADD_WIDE(s1, s0, a1, a0, b1, b0) \
do { \
if (__builtin_constant_p (b1) && (b1) == 0) \
__asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (a1), "%r" (a0), "rI" (b0)); \
else if (__builtin_constant_p (b1) && (b1) == ~(unsigned long) 0) \
__asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (a1), "%r" (a0), "rI" (b0)); \
else \
__asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3" \
: "=r" (s1), "=&r" (s0) \
: "r" (a1), "r" (b1), "%r" (a0), "rI" (b0)); \
} while (0)
#define ZNP_SUB_WIDE(s1, s0, a1, a0, b1, b0) \
do { \
if (__builtin_constant_p (a1) && (a1) == 0) \
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (b1), "rI" (a0), "r" (b0)); \
else if (__builtin_constant_p (a1) && (a1) == ~(unsigned long) 0) \
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (b1), "rI" (a0), "r" (b0)); \
else if (__builtin_constant_p (b1) && (b1) == 0) \
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (a1), "rI" (a0), "r" (b0)); \
else if (__builtin_constant_p (b1) && (b1) == ~(unsigned long) 0) \
__asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \
: "=r" (s1), "=&r" (s0) : "r" (a1), "rI" (a0), "r" (b0)); \
else \
__asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2" \
: "=r" (s1), "=&r" (s0) \
: "r" (a1), "r" (b1), "rI" (a0), "r" (b0)); \
} while (0)
#endif
#endif
// ------ ALPHA ------
#if (defined (__alpha) && UNSIGNED_LONG_BITS == 64)
#define ZNP_MUL_HI(hi, a, b) \
__asm__ ("umulh %r1,%2,%0" : "=r" (hi) : "%rJ" (a), "rI" (b));
#endif
// ------ IA64 ------
#if (defined (__ia64) && UNSIGNED_LONG_BITS == 64)
#define ZNP_MUL_HI(hi, a, b) \
__asm__ ("xma.hu %0 = %1, %2, f0" : "=f" (hi) : "f" (a), "f" (b));
#endif
// ------ x86 ------
#if ((defined (__i386__) || defined (__i486__)) && UNSIGNED_LONG_BITS == 32)
#define ZNP_MUL_WIDE(hi, lo, a, b) \
__asm__ ("mull %3" : "=a" (lo), "=d" (hi) : "%0" (a), "rm" (b));
#define ZNP_ADD_WIDE(s1, s0, a1, a0, b1, b0) \
__asm__ ("addl %5,%k1\n\tadcl %3,%k0" \
: "=r" (s1), "=&r" (s0) \
: "0" ((unsigned long)(a1)), "g" ((unsigned long)(b1)), \
"%1" ((unsigned long)(a0)), "g" ((unsigned long)(b0)))
#define ZNP_SUB_WIDE(s1, s0, a1, a0, b1, b0) \
__asm__ ("subl %5,%k1\n\tsbbl %3,%k0" \
: "=r" (s1), "=&r" (s0) \
: "0" ((unsigned long)(a1)), "g" ((unsigned long)(b1)), \
"1" ((unsigned long)(a0)), "g" ((unsigned long)(b0)))
#endif
// ------ x86-64 ------
#if (defined (__x86_64__) && UNSIGNED_LONG_BITS == 64)
#define ZNP_MUL_WIDE(hi, lo, a, b) \
__asm__ ("mulq %3" : "=a" (lo), "=d" (hi) : "%0" (a), "rm" (b));
#define ZNP_ADD_WIDE(s1, s0, a1, a0, b1, b0) \
__asm__ ("addq %5,%q1\n\tadcq %3,%q0" \
: "=r" (s1), "=&r" (s0) \
: "0" ((unsigned long)(a1)), "rme" ((unsigned long)(b1)), \
"%1" ((unsigned long)(a0)), "rme" ((unsigned long)(b0)))
#define ZNP_SUB_WIDE(s1, s0, a1, a0, b1, b0) \
__asm__ ("subq %5,%q1\n\tsbbq %3,%q0" \
: "=r" (s1), "=&r" (s0) \
: "0" ((unsigned long)(a1)), "rme" ((unsigned long)(b1)), \
"1" ((unsigned long)(a0)), "rme" ((unsigned long)(b0)))
#endif
// ------ MIPS ------
#if (defined (__mips))
#if (UNSIGNED_LONG_BITS == 32)
#define ZNP_MUL_WIDE(hi, lo, a, b) \
do { \
uint64_t result; \
result = (uint64_t) a * b; \
hi = (uint32_t)(result >> 32); \
lo = (uint32_t) result; \
} while (0);
#elif (UNSIGNED_LONG_BITS == 64)
#define ZNP_MUL_WIDE(hi, lo, a, b) \
do { \
typedef unsigned int uint128_t __attribute__((mode(TI))); \
uint128_t result; \
result = (uint128_t) a * b; \
hi = (uint64_t)(result >> 64); \
lo = (uint64_t) result; \
} while (0);
#endif
#endif
// -------- SPARC --------
#if (defined (__sparc__) && UNSIGNED_LONG_BITS == 32)
#if (defined (__sparc_v9__) || defined (__sparcv9) || \
defined (__sparc_v8__) || defined (__sparcv8) || defined (__sparclite__))
#define ZNP_MUL_WIDE(hi, lo, a, b) \
__asm__ ("umul %2,%3,%1;rd %%y,%0" : "=r" (hi), "=r" (lo) : "r" (a), "r" (b));
#endif
#endif
#endif // __GNUC__
// -------- generic implementations --------
/*
If neither ZNP_MUL_HI nor ZNP_MUL_WIDE has an assembly implementation,
do it in C.
(algorithm is from GMP's longlong.h)
*/
#if !(defined(ZNP_MUL_HI) || defined(ZNP_MUL_WIDE))
#warning No assembly implementation of wide multiplication available for this \
machine; using generic C code instead.
#define ZNP_MUL_WIDE(hi, lo, a, b) \
do { \
unsigned long __a = (a); \
unsigned long __b = (b); \
unsigned long __mask = (1UL << (UNSIGNED_LONG_BITS/2)) - 1; \
\
unsigned long __a0 = __a & __mask; \
unsigned long __a1 = __a >> (UNSIGNED_LONG_BITS/2); \
unsigned long __b0 = __b & __mask; \
unsigned long __b1 = __b >> (UNSIGNED_LONG_BITS/2); \
\
unsigned long __p00 = __a0 * __b0; \
unsigned long __p01 = __a0 * __b1; \
unsigned long __p10 = __a1 * __b0; \
unsigned long __p11 = __a1 * __b1; \
\
__p01 += (__p00 >> (UNSIGNED_LONG_BITS/2)); /* no possible carry */ \
__p01 += __p10; \
if (__p01 < __p10) \
__p11 += (1UL << (UNSIGNED_LONG_BITS/2)); /* propagate carry */ \
\
(hi) = __p11 + (__p01 >> (UNSIGNED_LONG_BITS/2)); \
(lo) = (__p00 & __mask) + (__p01 << (UNSIGNED_LONG_BITS/2)); \
} while (0)
#endif
/*
If only one of ZNP_MUL_HI and ZNP_MUL_WIDE is defined, define the other one
in terms of that one.
*/
#if defined(ZNP_MUL_HI) && !defined(ZNP_MUL_WIDE)
#define ZNP_MUL_WIDE(hi, lo, a, b) \
do { \
unsigned long __a = (a), __b = (b); \
ZNP_MUL_HI((hi), __a, __b); \
(lo) = __a * __b; \
} while (0)
#endif
#if defined(ZNP_MUL_WIDE) && !defined(ZNP_MUL_HI)
#define ZNP_MUL_HI(hi, a, b) \
do { \
unsigned long __dummy; \
ZNP_MUL_WIDE((hi), __dummy, (a), (b)); \
} while (0)
#endif
#if !defined(ZNP_ADD_WIDE)
#define ZNP_ADD_WIDE(s1, s0, a1, a0, b1, b0) \
do { \
unsigned long __a0 = (a0); \
unsigned long __temp = __a0 + (b0); \
(s1) = (a1) + (b1) + (__temp < __a0); \
(s0) = __temp; \
} while (0)
#endif
#if !defined(ZNP_SUB_WIDE)
#define ZNP_SUB_WIDE(s1, s0, a1, a0, b1, b0) \
do { \
unsigned long __a0 = (a0); \
unsigned long __b0 = (b0); \
unsigned long __temp = __a0 - __b0; \
(s1) = (a1) - (b1) - (__a0 < __b0); \
(s0) = __temp; \
} while (0)
#endif
/*
Temporarily in zn_poly-0.8:
The following macros are for backward compatibility. The only program that
uses them is an older version of hypellfrob. That might co-exist
temporarily with zn_poly-0.8 in Sage 3.0, so we have these here for the
moment. todo: delete these macros in the next zn_poly version.
*/
#define MUL_WIDE ZNP_MUL_WIDE
#define MUL_HI ZNP_MUL_HI
#define ADD_WIDE ZNP_ADD_WIDE
#define SUB_WIDE ZNP_SUB_WIDE
#endif
// end of file ****************************************************************
|