Skip to content

Commit 6d39bca

Browse files
committed
High precision mode working! But crashes for minimally nested functions...
1 parent 2895bc0 commit 6d39bca

5 files changed

Lines changed: 49 additions & 49 deletions

File tree

src/glsl_transpiled/glsl_big_number.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ number abs_sum(number a, number b) {
7878
number c = null_number();
7979
uint carry = 0u;
8080
for (int i = 0; i < NUMBER_OF_LIMBS; ++i) {
81-
uvec2 res = sum_with_carry(sum_with_carry(a.limb[i], b.limb[i]).x, carry);
82-
uint sum = res.x;
83-
carry = res.y;
84-
c.limb[i] = sum;
81+
uvec2 step1 = sum_with_carry(a.limb[i], b.limb[i]);
82+
uvec2 step2 = sum_with_carry(step1.x, carry);
83+
84+
c.limb[i] = step2.x;
85+
carry = step1.y + step2.y;
8586
}
8687
return c;
8788
}
@@ -247,7 +248,7 @@ uint get_half(number a, int index) {
247248
return 0u;
248249
}
249250
uint l = a.limb[index / 2];
250-
return uint(index % 2 == 0) * hi(l) + uint(index % 2 == 1) * lo(l);
251+
return uint(index % 2 == 0) * lo(l) + uint(index % 2 == 1) * hi(l);
251252
}
252253

253254
void set_half(number& a, int index, uint val) {
@@ -329,7 +330,8 @@ number hp_div(number n, number d) {
329330

330331
if (shift > 0) {
331332
uint carry_shift = 0u;
332-
for (int i = 0; i < 25; ++i) {
333+
const int MAX_HALVES = NUMBER_OF_LIMBS * 2 + FRACTIONAL_SIZE * 2 + 1;
334+
for (int i = 0; i < MAX_HALVES; ++i) {
333335
uint val = (u_halves[i] << shift) | carry_shift;
334336
carry_shift = u_halves[i] >> (16 - shift);
335337
u_halves[i] = val & 0xFFFFu;
@@ -355,14 +357,8 @@ number hp_div(number n, number d) {
355357
q_hat = 0xFFFFu;
356358
r_hat = u_jn1 + v_n1;
357359
} else {
358-
if (v_n1 == 0) {
359-
q_hat = (1 << 30 + 1);
360-
r_hat = 1;
361-
}
362-
else {
363-
q_hat = dividend / (v_n1);
364-
r_hat = dividend % v_n1;
365-
}
360+
q_hat = dividend / v_n1;
361+
r_hat = dividend % v_n1;
366362
}
367363

368364
while (r_hat < 0x10000u && (q_hat * v_n2) > ((r_hat << 16) | u_jn2)) {
@@ -510,7 +506,7 @@ number hp_sqrt(number x) {
510506

511507
number n_k_next = null_number();
512508

513-
for (int i = 0; i < (NUMBER_OF_LIMBS / 23 * 32 + 2); ++i) {
509+
for (int i = 0; i < ((NUMBER_OF_LIMBS * 32)/ 23 + 2); ++i) {
514510
number div_term = hp_div(x, n_k);
515511
n_k_next = hp_add(n_k, div_term);
516512
n_k_next = shift_right(n_k_next, 1);

src/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,23 @@ void main_loop_step(AppContext* ctx) {
194194
init_imgui_loop();
195195

196196
if(ctx->view_state->wants_high_precision){
197+
const float hp_width = 800;//ctx->view_state->width;
198+
const float hp_height = 600;//ctx->view_state->height;
197199
//NUMBER_OF_LIMBS = 8;
198200
if (ctx->view_state->hp_fbo == 0) {
199201
glGenFramebuffers(1, &ctx->view_state->hp_fbo);
200202
glGenTextures(1, &ctx->view_state->hp_texture);
201203

202204
glBindTexture(GL_TEXTURE_2D, ctx->view_state->hp_texture);
203-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ctx->view_state->width/2, ctx->view_state->height/2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
205+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, hp_width, hp_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
204206
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
205207
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
206208

207209
glBindFramebuffer(GL_FRAMEBUFFER, ctx->view_state->hp_fbo);
208210
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ctx->view_state->hp_texture, 0);
209211
}
210212
glBindFramebuffer(GL_FRAMEBUFFER, ctx->view_state->hp_fbo);
211-
glViewport(0, 0, ctx->view_state->width/2, ctx->view_state->height/2);
213+
glViewport(0, 0, hp_width, hp_height);
212214
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
213215
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
214216
glDisable(GL_DEPTH_TEST);

src/shaders/embedded_shaders.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,17 @@ uvec2 sum_with_carry(uint a, uint b){
105105
res.y = uint(a > res.x || b > res.x);
106106
return res;
107107
}
108-
number abs_sum(number a, number b){
109-
number c = REAL_ZERO;
110-
uint carry = 0u;
111-
for(int i = 0; i < NUMBER_OF_LIMBS; ++i){
112-
uvec2 res = sum_with_carry(sum_with_carry(a.limb[i],b.limb[i]).x,carry);
113-
uint sum = res.x;
114-
carry = res.y;
115-
c.limb[i] = sum;
116-
}
117-
return c;
108+
number abs_sum(number a, number b) {
109+
number c = null_number();
110+
uint carry = 0u;
111+
for (int i = 0; i < NUMBER_OF_LIMBS; ++i) {
112+
uvec2 step1 = sum_with_carry(a.limb[i], b.limb[i]);
113+
uvec2 step2 = sum_with_carry(step1.x, carry);
114+
115+
c.limb[i] = step2.x;
116+
carry = step1.y + step2.y;
117+
}
118+
return c;
118119
}
119120
number abs_hp_sub(number a, number b) {
120121
number c = REAL_ZERO;
@@ -278,7 +279,7 @@ uint get_half(number a, int index){
278279
return 0u;
279280
}
280281
uint l = a.limb[index / 2];
281-
return uint(index % 2 == 0) * hi(l) + uint(index%2==1) * lo(l);
282+
return uint(index % 2 == 0) * lo(l) + uint(index%2==1) * hi(l);
282283
}
283284
284285
void set_half(inout number a, int index, uint val) {
@@ -359,7 +360,8 @@ number hp_div(number n, number d) {
359360
360361
if (shift > 0) {
361362
uint carry_shift = 0u;
362-
for (int i = 0; i < 25; ++i) {
363+
const int MAX_HALVES = int(NUMBER_OF_LIMBS*2 + FRACTIONAL_SIZE * 2 + 1);
364+
for (int i = 0; i < MAX_HALVES; ++i) {
363365
uint val = (u_halves[i] << shift) | carry_shift;
364366
carry_shift = u_halves[i] >> (16 - shift);
365367
u_halves[i] = val & 0xFFFFu;
@@ -376,14 +378,14 @@ number hp_div(number n, number d) {
376378
for (int j = m; j >= 0; --j) {
377379
uint u_jn = u_halves[j + n_len];
378380
uint u_jn1 = u_halves[j + n_len - 1];
379-
uint u_jn2 = (j + n_len >= 2) ? u_halves[j + n_len - 2] : 0u;
381+
uint u_jn2 = (j + n_len >= 2) ? u_halves[j + n_len - 2] : 0u)shdr" R"shdr(;
380382
381383
uint dividend = (u_jn << 16) | u_jn1;
382384
uint q_hat, r_hat;
383385
384386
if (u_jn == v_n1) {
385387
q_hat = 0xFFFFu;
386-
r_hat = u_jn1)shdr" R"shdr( + v_n1;
388+
r_hat = u_jn1 + v_n1;
387389
} else {
388390
q_hat = dividend / v_n1;
389391
r_hat = dividend % v_n1;
@@ -560,7 +562,6 @@ number hp_log(number x){
560562
if (k > 0) m = shift_left(x,-k);
561563
else m = x;
562564
563-
number REAL_ONE = REAL_ONE;
564565
number z = hp_div(hp_sub(m,REAL_ONE),hp_add(m,REAL_ONE));
565566
number z_squared = hp_mult(z,z);
566567
@@ -595,7 +596,7 @@ number hp_sqrt(number x){
595596
596597
number n_k_next = REAL_ZERO;
597598
598-
for(int i = 0; i < (NUMBER_OF_LIMBS / 23 + 2); ++i){
599+
for(int i = 0; i < ((NUMBER_OF_LIMBS *32)/ 23 + 2); ++i){
599600
number div_term = hp_div(x,n_k);
600601
n_k_next = hp_add(n_k,div_term);
601602
n_k_next = shift_right(n_k_next,1);
@@ -699,13 +700,13 @@ number hp_cos(number x) {
699700
term = hp_mult(term, x_sq);
700701
701702
uint divisor = uint(2 * i - 1) * uint(2 * i);
702-
term = div_uint(term, divisor);
703+
term = div_uint(term, div)shdr" R"shdr(isor);
703704
704705
if (is_zero(term)) break;
705706
706707
if (i % 2 == 1) {
707708
sum = hp_sub(sum, term);
708-
})shdr" R"shdr( else {
709+
} else {
709710
sum = hp_add(sum, term);
710711
}
711712
}

src/shaders/high_precision_functions.frag

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ uvec2 sum_with_carry(uint a, uint b){
3434
res.y = uint(a > res.x || b > res.x);
3535
return res;
3636
}
37-
number abs_sum(number a, number b){
38-
number c = REAL_ZERO;
39-
uint carry = 0u;
40-
for(int i = 0; i < NUMBER_OF_LIMBS; ++i){
41-
uvec2 res = sum_with_carry(sum_with_carry(a.limb[i],b.limb[i]).x,carry);
42-
uint sum = res.x;
43-
carry = res.y;
44-
c.limb[i] = sum;
45-
}
46-
return c;
37+
number abs_sum(number a, number b) {
38+
number c = null_number();
39+
uint carry = 0u;
40+
for (int i = 0; i < NUMBER_OF_LIMBS; ++i) {
41+
uvec2 step1 = sum_with_carry(a.limb[i], b.limb[i]);
42+
uvec2 step2 = sum_with_carry(step1.x, carry);
43+
44+
c.limb[i] = step2.x;
45+
carry = step1.y + step2.y;
46+
}
47+
return c;
4748
}
4849
number abs_hp_sub(number a, number b) {
4950
number c = REAL_ZERO;
@@ -207,7 +208,7 @@ uint get_half(number a, int index){
207208
return 0u;
208209
}
209210
uint l = a.limb[index / 2];
210-
return uint(index % 2 == 0) * hi(l) + uint(index%2==1) * lo(l);
211+
return uint(index % 2 == 0) * lo(l) + uint(index%2==1) * hi(l);
211212
}
212213

213214
void set_half(inout number a, int index, uint val) {
@@ -288,7 +289,8 @@ number hp_div(number n, number d) {
288289

289290
if (shift > 0) {
290291
uint carry_shift = 0u;
291-
for (int i = 0; i < 25; ++i) {
292+
const int MAX_HALVES = int(NUMBER_OF_LIMBS*2 + FRACTIONAL_SIZE * 2 + 1);
293+
for (int i = 0; i < MAX_HALVES; ++i) {
292294
uint val = (u_halves[i] << shift) | carry_shift;
293295
carry_shift = u_halves[i] >> (16 - shift);
294296
u_halves[i] = val & 0xFFFFu;
@@ -489,7 +491,6 @@ number hp_log(number x){
489491
if (k > 0) m = shift_left(x,-k);
490492
else m = x;
491493

492-
number REAL_ONE = REAL_ONE;
493494
number z = hp_div(hp_sub(m,REAL_ONE),hp_add(m,REAL_ONE));
494495
number z_squared = hp_mult(z,z);
495496

@@ -524,7 +525,7 @@ number hp_sqrt(number x){
524525

525526
number n_k_next = REAL_ZERO;
526527

527-
for(int i = 0; i < (NUMBER_OF_LIMBS / 23 * 32 + 2); ++i){
528+
for(int i = 0; i < ((NUMBER_OF_LIMBS *32)/ 23 + 2); ++i){
528529
number div_term = hp_div(x,n_k);
529530
n_k_next = hp_add(n_k,div_term);
530531
n_k_next = shift_right(n_k_next,1);

src/shaders/high_precision_functions_optimized.frag

Whitespace-only changes.

0 commit comments

Comments
 (0)