-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuint128_t.cu
134 lines (106 loc) · 3.17 KB
/
uint128_t.cu
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
// BACHELOR ARBEIT ABGABE
//
// Created by Yorrick on 22.05.2018.
//
// This is a PSEUDO uint128_t!
// It won't work as a correct int or decimal representation as I only care about the binary representation in the CARF context.
//
// For the version used to create the published benchmarks, checkout original commit.
#include <string>
#include <vector>
#include <sstream> //istringstream
#include <iostream> // cout
#include <fstream> // ifstream
#include <cstring>
#include <bitset>
using namespace std;
struct uint128_t {
uint64_t LEFT;
uint64_t RIGHT;
__host__ __device__ uint128_t() {
LEFT = 0;
RIGHT = 0;
}
__host__ __device__ uint128_t(uint64_t right) {
LEFT = 0;
RIGHT = right;
}
__host__ __device__ uint128_t(uint64_t left, uint64_t right) {
LEFT = left;
RIGHT = right;
}
/**
* First version, used in the published benchmarks.
* a = a>>3;
*/
__host__ __device__ uint128_t operator<<(const size_t &n) const {
if(n >= 128){
return uint128_t{0, 0};
}
if(n > 64){
return uint128_t{(RIGHT<<(n-64)), 0};
}
if(n == 64){
return uint128_t{RIGHT, 0};
}
return uint128_t{(LEFT << n) | (RIGHT >> (64 - n)), RIGHT << n};
}
/**
* First version, used in the published benchmarks.
*/
__host__ __device__ uint128_t operator>>(const size_t &n) const {
if(n >= 128){
return uint128_t{0, 0};
}
if(n > 64){
return uint128_t{0, (LEFT>>(n-64))};
}
if(n == 64){
return uint128_t{0, LEFT};
}
return uint128_t{(LEFT >> n), (LEFT << (64 - n) | (RIGHT >> n))};
}
__host__ __device__ uint128_t operator|(const uint128_t &n) const {
return uint128_t{(n.LEFT | LEFT), (n.RIGHT | RIGHT)};
}
__host__ __device__ uint128_t operator&(const uint128_t &n) const {
return uint128_t{(n.LEFT & LEFT), (n.RIGHT & RIGHT)};
}
__host__ __device__ uint128_t operator^(const uint128_t &n) const {
return uint128_t{(n.LEFT^LEFT),(n.RIGHT^RIGHT)};
}
__host__ __device__ bool operator!=(const uint128_t &n) const {
return !(LEFT==n.LEFT and RIGHT==n.RIGHT);
}
__host__ __device__ bool operator==(const uint64_t &n) const {
return (LEFT == 0 and RIGHT == n);
}
__host__ __device__ bool operator==(const uint128_t &n) const {
return (LEFT == n.LEFT and RIGHT == n.RIGHT);
}
__host__ __device__ bool operator<(const uint64_t &n) const {
return(LEFT > 0 or RIGHT < n);
}
__host__ __device__ bool operator>(const uint64_t &n) const {
return (LEFT > 0 or RIGHT > n);
}
/**
bitArray getBits(){
transform in bitrepresentation
*/
__device__ void printC(){
printf("%llu , %llu \n", LEFT, RIGHT);
}
__host__ void print() {
if(LEFT==0){
cout << RIGHT;
} else {
cout << LEFT << "," << RIGHT;
}
}
__host__ void printBits(){
std::bitset<64> le = LEFT;
std::bitset<64> ri = RIGHT;
cout << le <<","<< ri << endl;
}
};