Theoretica
Scientific Computing
Loading...
Searching...
No Matches
bits.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_BITS_H
7#define THEORETICA_BITS_H
8
9#include <cstdint>
10#include "./core_traits.h"
11
12
13namespace theoretica {
14
16namespace bits {
17
18
28 inline constexpr void mul_uint128(
29 uint64_t a, uint64_t b,
31
32 // Lowest and highest 32 bits of a and b
33 const uint64_t a_low = a & 0xffffffff;
34 const uint64_t a_high = a >> 32;
35
36 const uint64_t b_low = b & 0xffffffff;
37 const uint64_t b_high = b >> 32;
38
39 // Multiplication terms for (a_l + a_h) * (b_l + b_h)
40 const uint64_t m[4] = {
41 a_low * b_low,
42 a_low * b_high,
43 a_high * b_low,
45 };
46
47 // Multiplication carry for c_high
48 const uint64_t carry = (
49 (m[0] >> 32) +
50 (m[1] & 0xffffffff) +
51 (m[2] & 0xffffffff)) >> 32;
52
53 c_low = m[0] + (m[1] << 32) + (m[2] << 32);
54 c_high = m[3] + (m[1] >> 32) + (m[2] >> 32) + carry;
55 }
56
57
66 inline constexpr uint64_t mix_mum(uint64_t a, uint64_t b) {
67
68 uint64_t c_low = 0;
69 uint64_t c_high = 0;
70 mul_uint128(a, b, c_low, c_high);
71
72 return c_high ^ c_low;
73 }
74
75
81 template<typename UnsignedIntType>
82 inline constexpr UnsignedIntType
83 bit_rotate(UnsignedIntType x, unsigned int i) {
84
85 return (x << i) | (x >> ((sizeof(UnsignedIntType) * 8) - i));
86 }
87
88
94 template <
95 typename Vector, enable_vector<Vector> = true
96 >
97 inline constexpr void swap_bit_reverse(Vector& x, unsigned int m) {
98
99 if (x.size() < (uint64_t(1) << m)) {
100 TH_MATH_ERROR("swap_bit_reverse", x.size(), MathError::InvalidArgument);
101 return;
102 }
103
104 for (unsigned int i = 0; i < x.size(); i++) {
105
106 unsigned int j = 0;
107
108 for (unsigned int k = 0; k < m; k++)
109 j = (j << 1) | ((i >> k) & 0x01);
110
111 if (j > i)
112 std::swap(x[i], x[j]);
113 }
114 }
115
116}}
117
118#endif
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Fundamental type traits.
constexpr void swap_bit_reverse(Vector &x, unsigned int m)
Swap the elements of a vector pair-wise, by exchanging elements with indices related by bit reversion...
Definition bits.h:97
constexpr UnsignedIntType bit_rotate(UnsignedIntType x, unsigned int i)
Bit rotation of unsigned integer types using shifts.
Definition bits.h:83
constexpr uint64_t mix_mum(uint64_t a, uint64_t b)
MUM bit mixing function, computes the 128-bit product of a and b and the XOR of their high and low 64...
Definition bits.h:66
constexpr void mul_uint128(uint64_t a, uint64_t b, uint64_t &c_low, uint64_t &c_high)
Multiply two 64-bit unsigned integers and store the result in two 64-bit variables,...
Definition bits.h:28
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
@ InvalidArgument
Invalid argument.