COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
mod_arith_64bit.h
Go to the documentation of this file.
1/* Copyright (C) 2010 The Trustees of Indiana University. */
2/* */
3/* Use, modification and distribution is subject to the Boost Software */
4/* License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at */
5/* http://www.boost.org/LICENSE_1_0.txt) */
6/* */
7/* Authors: Jeremiah Willcock */
8/* Andrew Lumsdaine */
9
10#ifndef MOD_ARITH_64BIT_H
11#define MOD_ARITH_64BIT_H
12
13#include <stdint.h>
14#include <assert.h>
15
16/* Various modular arithmetic operations for modulus 2^31-1 (0x7FFFFFFF).
17 * These may need to be tweaked to get acceptable performance on some platforms
18 * (especially ones without conditional moves). */
19
20static inline uint_fast32_t mod_add(uint_fast32_t a, uint_fast32_t b) {
21 assert (a <= 0x7FFFFFFE);
22 assert (b <= 0x7FFFFFFE);
23 return (a + b) % 0x7FFFFFFF;
24}
25
26static inline uint_fast32_t mod_mul(uint_fast32_t a, uint_fast32_t b) {
27 assert (a <= 0x7FFFFFFE);
28 assert (b <= 0x7FFFFFFE);
29 return (uint_fast32_t)((uint_fast64_t)a * b % 0x7FFFFFFF);
30}
31
32static inline uint_fast32_t mod_mac(uint_fast32_t sum, uint_fast32_t a, uint_fast32_t b) {
33 assert (sum <= 0x7FFFFFFE);
34 assert (a <= 0x7FFFFFFE);
35 assert (b <= 0x7FFFFFFE);
36 return (uint_fast32_t)(((uint_fast64_t)a * b + sum) % 0x7FFFFFFF);
37}
38
40 assert (sum <= 0x7FFFFFFE);
41 assert (a <= 0x7FFFFFFE);
42 assert (b <= 0x7FFFFFFE);
43 assert (c <= 0x7FFFFFFE);
44 assert (d <= 0x7FFFFFFE);
45 return (uint_fast32_t)(((uint_fast64_t)a * b + (uint_fast64_t)c * d + sum) % 0x7FFFFFFF);
46}
47
49 assert (sum <= 0x7FFFFFFE);
50 assert (a <= 0x7FFFFFFE);
51 assert (b <= 0x7FFFFFFE);
52 assert (c <= 0x7FFFFFFE);
53 assert (d <= 0x7FFFFFFE);
54 assert (e <= 0x7FFFFFFE);
55 assert (f <= 0x7FFFFFFE);
56 return (uint_fast32_t)(((uint_fast64_t)a * b + (uint_fast64_t)c * d + (uint_fast64_t)e * f + sum) % 0x7FFFFFFF);
57}
58
60 assert (sum <= 0x7FFFFFFE);
61 assert (a <= 0x7FFFFFFE);
62 assert (b <= 0x7FFFFFFE);
63 assert (c <= 0x7FFFFFFE);
64 assert (d <= 0x7FFFFFFE);
65 assert (e <= 0x7FFFFFFE);
66 assert (f <= 0x7FFFFFFE);
67 assert (g <= 0x7FFFFFFE);
68 assert (h <= 0x7FFFFFFE);
69 return (uint_fast32_t)(((uint_fast64_t)a * b + (uint_fast64_t)c * d + (uint_fast64_t)e * f + (uint_fast64_t)g * h + sum) % 0x7FFFFFFF);
70}
71
72/* The two constants x and y are special cases because they are easier to
73 * multiply by on 32-bit systems. They are used as multipliers in the random
74 * number generator. The techniques for fast multiplication by these
75 * particular values are in L'Ecuyer's papers; we don't use them yet. */
76
77static inline uint_fast32_t mod_mul_x(uint_fast32_t a) {
78 return mod_mul(a, 107374182);
79}
80
81static inline uint_fast32_t mod_mul_y(uint_fast32_t a) {
82 return mod_mul(a, 104480);
83}
84
86 return mod_mac(sum, a, 104480);
87}
88
89#endif /* MOD_ARITH_64BIT_H */
uint_fast32_t mod_mul_x(uint_fast32_t a)
uint_fast32_t mod_mac_y(uint_fast32_t sum, uint_fast32_t a)
uint_fast32_t mod_mul_y(uint_fast32_t a)
uint64_t uint_fast64_t
Definition stdint.h:111
uint32_t uint_fast32_t
Definition stdint.h:110