-
Notifications
You must be signed in to change notification settings - Fork 2
/
arb-precision.glsl
124 lines (95 loc) · 2.06 KB
/
arb-precision.glsl
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
#ifdef GL_ES
precision highp int;
#endif
/* integers per arbitrary-precision number */
const int vals = {{vals}}; // ints per value
/* power of 10 one larger than maximum value per int
A value of 10000 seems to work the best
*/
const int limit = {{limit}};
const float limitFlt = float(limit);
int result[vals];
#define zero(x, len) for(int i=0;i<len;i++){x[i]=0;}
#define assign(x, y) for(int i=0;i<vals;i++){x[i]=y[i];}
#define negate(x) for(int i = 0; i < vals; i++) { x[i] = -x[i]; }
bool signp(int[vals] a) {
return (a[vals-1] >= 0);
}
int keepVal, carry;
void roundOff(int x) {
carry = x / limit;
keepVal = x - carry * limit;
}
void add(int[vals] a, int[vals] b) {
bool s1 = signp(a), s2 = signp(b);
carry = 0;
for(int i = 0; i < vals-1; i++) {
roundOff(a[i] + b[i] + carry);
if(keepVal < 0) {
keepVal += limit;
carry--;
}
result[i] = keepVal;
}
roundOff(a[vals-1] + b[vals-1] + carry);
result[vals-1] = keepVal;
if(s1 != s2 && !signp(result)) {
negate(result);
carry = 0;
for(int i = 0; i < vals; i++) {
roundOff(result[i] + carry);
if(keepVal < 0) {
keepVal += limit;
carry--;
}
result[i] = keepVal;
}
negate(result);
}
}
void mul(int[vals] a, int[vals] b) {
bool toNegate = false;
if(!signp(a)) {
negate(a);
toNegate = !toNegate;
}
if(!signp(b)) {
negate(b);
toNegate = !toNegate;
}
const int lenProd = (vals-1)*2+1;
int prod[lenProd];
zero(prod, lenProd);
for(int i = 0; i < vals; i++) {
for(int j = 0; j < vals; j++) {
prod[i+j] += a[i] * b[j];
}
}
carry = 0;
const int clip = lenProd - vals;
for(int i = 0; i < clip; i++) {
roundOff(prod[i] + carry);
prod[i] = keepVal;
}
if(prod[clip-1] >= limit/2) {
carry++;
}
for(int i = clip; i < lenProd; i++) {
roundOff(prod[i] + carry);
prod[i] = keepVal;
}
for(int i = 0; i < lenProd - clip; i++) {
result[i] = prod[i+clip];
}
if(toNegate) {
negate(result);
}
}
void loadFloat(float f) {
for(int i = vals - 1; i >= 0; i--) {
int fCurr = int(f);
result[i] = fCurr;
f -= float(fCurr);
f *= limitFlt;
}
}