Heavy computing with GLSL – Part 5: Emulated quadruple precision

It’s been a while since my last post about the wonders of modern GPU computing with GLSL but the following concept really took a lot of time to get it to work as intended.

Today I’m going to introduce emulated quadruple floating point precision (quad-single-precision) with GLSL. I will use the well-known mandelbrot set to demonstrate the concept. The sourcecode is available as usual.

Introduction

The last posts use double precision (hardware and emulated) to calculate mandelbrot sets down to [insert] units per pixel in the complex plane. To push that limit even further I will emulate quadruple precision (quad-precision) using four single precision variables (quad-single). The original concept and the Fortran/C++ sourcecode was developed by Yozo Hi, Xiaoye S. Li and David H. Bailey at Berkeley. It is available as the QD library. I just need to convert and modify this code for GLSL…

The Quad-Single Concept

The concept is basically the same I have described in on of my previous posts about double-single emulation. For quad-precision you just add two more floats to represent your actual value. On the contrary, arithmetic operations are much more difficult to perform, because you must take care of all the carry-over stuff. This results in a lot of difficult and expensive functions to perform just a simple addition. The technical details are described in this paper (PDF).

QT-Application

The Qt application features only minor changes. To improve precision on this end I use long doubles for all variables that eventually end up in the shader.

To make things easier in the shader it gets two vec2 elements instead of four floats with the glUniform2fv-function. That’s why we need a handle for this function:

1
2
3
4
5
6
// define prototype
typedef void (APIENTRYP PFNGLUNIFORM2FVPROC) (GLint location, GLsizei count, const GLfloat *value);
PFNGLUNIFORM2FVPROC glUniform2fv;
 
// get handle from OpenGL-context
glUniform2fv = (PFNGLUNIFORM2FVPROC) GLFrame->context()->getProcAddress("glUniform2fv");
// define prototype
typedef void (APIENTRYP PFNGLUNIFORM2FVPROC) (GLint location, GLsizei count, const GLfloat *value);
PFNGLUNIFORM2FVPROC glUniform2fv;

// get handle from OpenGL-context
glUniform2fv = (PFNGLUNIFORM2FVPROC) GLFrame->context()->getProcAddress("glUniform2fv");

It is used as follows:

1
2
3
4
5
float vec2[2];
 
vec2[0] = (float)xpos;
vec2[1] = xpos - (double)vec2[0];
glUniform2fv(glGetUniformLocation(ShaderProgram->programId(), "qs_cx"), 2, vec2);
float vec2[2];

vec2[0] = (float)xpos;
vec2[1] = xpos - (double)vec2[0];
glUniform2fv(glGetUniformLocation(ShaderProgram->programId(), "qs_cx"), 2, vec2);

The Shader

I guess the shader has gotten somewhat complex. Nevertheless you can have a look at it and maybe adapt or extend it to your own purpose. The variable names are the same as in the original c++ code. The comments behind the lines are the original code (useful for debugging/comparing).

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#version 120
 
// emulated quadruple precision GLSL library
// created by Henry thasler (thasler.org/blog)
// based on the QD library (http://crd-legacy.lbl.gov/~dhbailey/mpdist/)
 
uniform int iterations;
uniform float frame;
uniform float radius;
 
uniform vec2 qs_z;
uniform vec2 qs_w;
uniform vec2 qs_h;
uniform vec2 qs_cx;
uniform vec2 qs_cy;
 
// inline double quick_two_sum(double a, double b, double &err)
vec2 quick_2sum(float a, float b)
{
 float s = a + b;           // double s = a + b;
 return vec2(s, b-(s-a));   // err = b - (s - a);
}
 
/* Computes fl(a+b) and err(a+b).  */
// inline double two_sum(double a, double b, double &err)
vec2 two_sum(float a, float b)
{
float v,s,e;
 
 s = a+b;               // double s = a + b;
 v = s-a;               // double bb = s - a;
 e = (a-(s-v))+(b-v);   // err = (a - (s - bb)) + (b - bb);
 
 return vec2(s,e);
}
 
vec2 split(float a)
{
float t, hi;
 t = 8193. * a;
 hi = t - (t-a);
 return vec2(hi, a-hi);
}
 
vec3 three_sum(float a, float b, float c)
{
 vec2 tmp;
 vec3 res;// = vec3(0.);
 float t1, t2, t3;
  tmp = two_sum(a, b); // t1 = qd::two_sum(a, b, t2);
  t1 = tmp.x;
  t2 = tmp.y;
 
  tmp = two_sum(c, t1); // a  = qd::two_sum(c, t1, t3);
  res.x = tmp.x;
  t3 = tmp.y;
 
  tmp = two_sum(t2, t3); // b  = qd::two_sum(t2, t3, c);
  res.y = tmp.x;
  res.z = tmp.y;
 
return res;
}
 
//inline void three_sum2(double &a, double &b, double &c)
vec3 three_sum2(float a, float b, float c)
{
vec2 tmp;
vec3 res;// = vec3(0.);
float t1, t2, t3;   // double t1, t2, t3;
  tmp = two_sum(a, b); // t1 = qd::two_sum(a, b, t2);
  t1 = tmp.x;
  t2 = tmp.y;
 
  tmp = two_sum(c, t1); // a  = qd::two_sum(c, t1, t3);
  res.x = tmp.x;
  t3 = tmp.y;
 
  res.y = t2 + t3;  // b = t2 + t3;
  return res;
}
 
vec2 two_prod(float a, float b)
{
float p, e;
vec2 va, vb;
 
 p=a*b;
 va = split(a);
 vb = split(b);
 
 e = ((va.x*vb.x-p) + va.x*vb.y + va.y*vb.x) + va.y*vb.y;
 return vec2(p, e);
}
 
vec4 renorm(float c0, float c1, float c2, float c3, float c4)
{
   float s0, s1, s2 = 0.0, s3 = 0.0;
   vec2 tmp;
 
  // if (QD_ISINF(c0)) return;
 
  tmp = quick_2sum(c3,c4); // s0 = qd::quick_two_sum(c3, c4, c4);
  s0 = tmp.x;
  c4 = tmp.y;
 
  tmp = quick_2sum(c2,s0); // s0 = qd::quick_two_sum(c2, s0, c3);
  s0 = tmp.x;
  c3 = tmp.y;
 
  tmp = quick_2sum(c1,s0); // s0 = qd::quick_two_sum(c1, s0, c2);
  s0 = tmp.x;
  c2 = tmp.y;
 
  tmp = quick_2sum(c0,s0); // c0 = qd::quick_two_sum(c0, s0, c1);
  c0 = tmp.x;
  c1 = tmp.y;
 
  s0 = c0;
  s1 = c1;
 
  tmp = quick_2sum(c0,c1); // s0 = qd::quick_two_sum(c0, c1, s1);
  s0 = tmp.x;
  s1 = tmp.y;
 
  if (s1 != 0.0) {
    tmp = quick_2sum(s1,c2); // s1 = qd::quick_two_sum(s1, c2, s2);
    s1 = tmp.x;
    s2 = tmp.y;
 
    if (s2 != 0.0) {
      tmp = quick_2sum(s2,c3); // s2 = qd::quick_two_sum(s2, c3, s3);
      s2 = tmp.x;
      s3 = tmp.y;
      if (s3 != 0.0)
        s3 += c4;
      else
        s2 += c4;
    } else {
      tmp = quick_2sum(s1,c3); // s1 = qd::quick_two_sum(s1, c3, s2);
      s1 = tmp.x;
      s2 = tmp.y;
      if (s2 != 0.0){
        tmp = quick_2sum(s2,c4); // s2 = qd::quick_two_sum(s2, c4, s3);
        s2 = tmp.x;
        s3 = tmp.y;}
      else{
        tmp = quick_2sum(s1,c4); // s1 = qd::quick_two_sum(s1, c4, s2);
        s1 = tmp.x;
        s2 = tmp.y;}
}
  } else {
    tmp = quick_2sum(s0,c2); // s0 = qd::quick_two_sum(s0, c2, s1);
    s0 = tmp.x;
    s1 = tmp.y;
    if (s1 != 0.0) {
      tmp = quick_2sum(s1,c3); // s1 = qd::quick_two_sum(s1, c3, s2);
      s1 = tmp.x;
      s2 = tmp.y;
      if (s2 != 0.0){
        tmp = quick_2sum(s2,c4); // s2 = qd::quick_two_sum(s2, c4, s3);
        s2 = tmp.x;
        s3 = tmp.y;}
      else{
        tmp = quick_2sum(s1,c4); // s1 = qd::quick_two_sum(s1, c4, s2);
        s1 = tmp.x;
        s2 = tmp.y;}
    } else {
      tmp = quick_2sum(s0,c3); // s0 = qd::quick_two_sum(s0, c3, s1);
      s0 = tmp.x;
      s1 = tmp.y;
      if (s1 != 0.0){
        tmp = quick_2sum(s1,c4); // s1 = qd::quick_two_sum(s1, c4, s2);
        s1 = tmp.x;
        s2 = tmp.y;}
      else{
        tmp = quick_2sum(s0,c4); // s0 = qd::quick_two_sum(s0, c4, s1);
        s0 = tmp.x;
        s1 = tmp.y;}
    }
  }
 
  return vec4(s0, s1, s2, s3);
 
}
 
vec4 renorm4(float c0, float c1, float c2, float c3)
{
  float s0, s1, s2 = 0.0, s3 = 0.0;
  vec2 tmp;
  // if (QD_ISINF(c0)) return;
 
  tmp = quick_2sum(c2,c3); // s0 = qd::quick_two_sum(c2, c3, c3);
  s0 = tmp.x;
  c3 = tmp.y;
 
  tmp = quick_2sum(c1,s0); // s0 = qd::quick_two_sum(c1, s0, c2);
  s0 = tmp.x;
  c2 = tmp.y;
 
  tmp = quick_2sum(c0,s0); // c0 = qd::quick_two_sum(c0, s0, c1);
  c0 = tmp.x;
  c1 = tmp.y;
 
  s0 = c0;
  s1 = c1;
  if (s1 != 0.0) {
    tmp = quick_2sum(s1,c2); // s1 = qd::quick_two_sum(s1, c2, s2);
    s1 = tmp.x;
    s2 = tmp.y;
 
    if (s2 != 0.0){
      tmp = quick_2sum(s2,c3); // s2 = qd::quick_two_sum(s2, c3, s3);
      s2 = tmp.x;
      s3 = tmp.y;}
    else{
      tmp = quick_2sum(s1,c3); // s1 = qd::quick_two_sum(s1, c3, s2);
      s1 = tmp.x;
      s2 = tmp.y;}
  } else {
      tmp = quick_2sum(s0,c2); // s0 = qd::quick_two_sum(s0, c2, s1);
      s0 = tmp.x;
      s1 = tmp.y;
    if (s1 != 0.0){
      tmp = quick_2sum(s1,c3); // s1 = qd::quick_two_sum(s1, c3, s2);
      s1 = tmp.x;
      s2 = tmp.y;}
    else{
      tmp = quick_2sum(s0,c3); // s0 = qd::quick_two_sum(s0, c3, s1);
      s0 = tmp.x;
      s1 = tmp.y;}
  }
 
 return vec4(s0, s1, s2, s3);
}
 
vec3 quick_three_accum(float a, float b, float c)
{
  vec2 tmp;
  float s;
  bool za, zb;
 
  tmp = two_sum(b, c); // s = qd::two_sum(b, c, b);
  s = tmp.x;
  b = tmp.y;
 
  tmp = two_sum(a, s); // s = qd::two_sum(a, s, a);
  s = tmp.x;
  a = tmp.y;
 
  za = (a != 0.0);
  zb = (b != 0.0);
 
  if (za && zb)
    return vec3(a,b,s);
 
  if (!zb) {
    b = a;
    a = s;
  } else {
    a = s;
  }
 
  return vec3(a,b,0.);
}
 
// inline qd_real qd_real::ieee_add(const qd_real &a, const qd_real &b)
vec4 qs_ieee_add(vec4 _a, vec4 _b)
{
 vec2 tmp=vec2(0.);
 vec3 tmp3=vec3(0.);
 int i, j, k;
 float s, t;
 float u, v;   // double-length accumulator
 float x[4] = float[4](0.0, 0.0, 0.0, 0.0);
 float a[4], b[4];
 
  a[0] = _a.x;
  a[1] = _a.y;
  a[2] = _a.z;
  a[3] = _a.w;
 
  b[0] = _b.x;
  b[1] = _b.y;
  b[2] = _b.z;
  b[3] = _b.w;
 
  i = j = k = 0;
  if (abs(a[i]) > abs(b[j]))
    u = a[i++];
  else
    u = b[j++];
  if (abs(a[i]) > abs(b[j]))
    v = a[i++];
  else
    v = b[j++];
 
  tmp = quick_2sum(u,v); // u = qd::quick_two_sum(u, v, v);
  u = tmp.x;
  v = tmp.y;
 
  while (k < 4) {
    if (i >= 4 && j >= 4) {
      x[k] = u;
      if (k < 3)
        x[++k] = v;
      break;
    }
 
    if (i >= 4)
      t = b[j++];
    else if (j >= 4)
      t = a[i++];
    else if (abs(a[i]) > abs(b[j])) {
      t = a[i++];
    } else
      t = b[j++];
 
    tmp3 = quick_three_accum(u,v,t)  ; // s = qd::quick_three_accum(u, v, t);
    u = tmp3.x;
    v = tmp3.y;
    s = tmp3.z;
 
    if (s != 0.0) {
      x[k++] = s;
    }
  }
 
   // add the rest.
  for (k = i; k < 4; k++)
    x[3] += a[k];
  for (k = j; k < 4; k++)
    x[3] += b[k];
 
  // qd::renorm(x[0], x[1], x[2], x[3]);
  // return qd_real(x[0], x[1], x[2], x[3]);
  return renorm4(x[0], x[1], x[2], x[3]);
}
 
// inline qd_real qd_real::sloppy_add(const qd_real &a, const qd_real &b)
vec4 qs_sloppy_add(vec4 a, vec4 b)
{
 float s0, s1, s2, s3;
 float t0, t1, t2, t3;
 
 float v0, v1, v2, v3;
 float u0, u1, u2, u3;
 float w0, w1, w2, w3;
 
 vec2 tmp;
 vec3 tmp3;
 
  s0 = a.x + b.x;   // s0 = a[0] + b[0];
  s1 = a.y + b.y;   // s1 = a[1] + b[1];
  s2 = a.z + b.z;   // s2 = a[2] + b[2];
  s3 = a.w + b.w;   // s3 = a[3] + b[3];  
 
  v0 = s0 - a.x;    // v0 = s0 - a[0];
  v1 = s1 - a.y;    // v1 = s1 - a[1];
  v2 = s2 - a.z;    // v2 = s2 - a[2];
  v3 = s3 - a.w;    // v3 = s3 - a[3];
 
  u0 = s0 - v0;
  u1 = s1 - v1;
  u2 = s2 - v2;
  u3 = s3 - v3;
 
  w0 = a.x - u0;    // w0 = a[0] - u0;
  w1 = a.y - u1;    // w1 = a[1] - u1;
  w2 = a.z - u2;    // w2 = a[2] - u2;
  w3 = a.w - u3;    // w3 = a[3] - u3; 
 
  u0 = b.x - v0;    // u0 = b[0] - v0;
  u1 = b.y - v1;    // u1 = b[1] - v1;
  u2 = b.z - v2;    // u2 = b[2] - v2;
  u3 = b.w - v3;    // u3 = b[3] - v3;
 
  t0 = w0 + u0;
  t1 = w1 + u1;
  t2 = w2 + u2;
  t3 = w3 + u3;
 
  tmp = two_sum(s1, t0); // s1 = qd::two_sum(s1, t0, t0);
  s1 = tmp.x;
  t0 = tmp.y;
 
  tmp3 = three_sum(s2, t0, t1); // qd::three_sum(s2, t0, t1);
  s2 = tmp3.x;
  t0 = tmp3.y;
  t1 = tmp3.z;
 
  tmp3 = three_sum2(s3, t0, t2); // qd::three_sum2(s3, t0, t2);
  s3 = tmp3.x;
  t0 = tmp3.y;
  t2 = tmp3.z;
 
  t0 = t0 + t1 + t3;
 
  // qd::renorm(s0, s1, s2, s3, t0);
  return renorm(s0, s1, s2, s3, t0); // return qd_real(s0, s1, s2, s3);
}
 
vec4 qs_add(vec4 _a, vec4 _b)
{
  return qs_sloppy_add(_a, _b);
//  return qs_ieee_add(_a, _b);
}  
 
vec4 qs_mul(vec4 a, vec4 b)
{
  float p0, p1, p2, p3, p4, p5;
  float q0, q1, q2, q3, q4, q5;
  float t0, t1;
  float s0, s1, s2;
  vec2 tmp;
  vec3 tmp3;
 
  tmp = two_prod(a.x, b.x); // p0 = qd::two_prod(a[0], b[0], q0);
  p0 = tmp.x;
  q0 = tmp.y;
 
  tmp = two_prod(a.x, b.y); // p1 = qd::two_prod(a[0], b[1], q1);
  p1 = tmp.x;
  q1 = tmp.y;
 
  tmp = two_prod(a.y, b.x); // p2 = qd::two_prod(a[1], b[0], q2);
  p2 = tmp.x;
  q2 = tmp.y;
 
  tmp = two_prod(a.x, b.z); // p3 = qd::two_prod(a[0], b[2], q3);
  p3 = tmp.x;
  q3 = tmp.y;
 
  tmp = two_prod(a.y, b.y); // p4 = qd::two_prod(a[1], b[1], q4);
  p4 = tmp.x;
  q4 = tmp.y;
 
  tmp = two_prod(a.z, b.x); // p5 = qd::two_prod(a[2], b[0], q5);
  p5 = tmp.x;
  q5 = tmp.y;
 
  /* Start Accumulation */
  tmp3 = three_sum(p1, p2, q0); // qd::three_sum(p1, p2, q0);
  p1 = tmp3.x;
  p2 = tmp3.y;
  q0 = tmp3.z;
 
  /* Six-Three Sum  of p2, q1, q2, p3, p4, p5. */
  tmp3 = three_sum(p2, q1, q2); // qd::three_sum(p2, q1, q2);
  p2 = tmp3.x;
  q1 = tmp3.y;
  q2 = tmp3.z;
 
  tmp3 = three_sum(p3, p4, p5); // qd::three_sum(p3, p4, p5);
  p3 = tmp3.x;
  p4 = tmp3.y;
  p5 = tmp3.z;
 
  /* compute (s0, s1, s2) = (p2, q1, q2) + (p3, p4, p5). */
  tmp = two_sum(p2, p3); // s0 = qd::two_sum(p2, p3, t0);
  s0 = tmp.x;
  t0 = tmp.y;
 
  tmp = two_sum(q1, p4); // s1 = qd::two_sum(q1, p4, t1);
  s1 = tmp.x;
  t1 = tmp.y;
 
  s2 = q2 + p5;
  tmp = two_sum(s1, t0); // s1 = qd::two_sum(s1, t0, t0);
  s1 = tmp.x;
  t0 = tmp.y;
  s2 += (t0 + t1);
 
  /* O(eps^3) order terms */
  s1 += a.x*b.w + a.y*b.z + a.z*b.y + a.w*b.x + q0 + q3 + q4 + q5;
 
  return renorm(p0, p1, s0, s1, s2); // qd::renorm(p0, p1, s0, s1, s2);
}
 
float ds_compare(vec2 dsa, vec2 dsb)
{
 if (dsa.x < dsb.x) return -1.;
 else if (dsa.x == dsb.x)
    {
    if (dsa.y < dsb.y) return -1.;
    else if (dsa.y == dsb.y) return 0.;
    else return 1.;
    }
 else return 1.;
}
 
float qs_compare(vec4 qsa, vec4 qsb)
{
 if(ds_compare(qsa.xy, qsb.xy)<0.) return -1.; // if (dsa.x < dsb.x) return -1.;
 else if (ds_compare(qsa.xy, qsb.xy) == 0.) // else if (dsa.x == dsb.x)
    {
    if(ds_compare(qsa.zw, qsb.zw)<0.) return -1.; // if (dsa.y < dsb.y) return -1.;
    else if (ds_compare(qsa.zw, qsb.zw) == 0.) return 0.;// else if (dsa.y == dsb.y) return 0.;
    else return 1.;
    }
 else return 1.;
}
 
float qs_mandel(void)
{
 vec4 qs_tx = vec4(gl_TexCoord[0].x, vec3(0.));     // get position of current pixel
 vec4 qs_ty = vec4(gl_TexCoord[0].y, vec3(0.));
 
 // initialize complex variable with respect to current position, zoom, ...
 vec4 cx = qs_add(qs_add(vec4(qs_cx,0.,0.),qs_mul(qs_tx,vec4(qs_z,0.,0.))),vec4(qs_w,0.,0.));
 vec4 cy = qs_add(qs_add(vec4(qs_cy,0.,0.),qs_mul(qs_ty,vec4(qs_z,0.,0.))),vec4(qs_h,0.,0.));  
 
 vec4 tmp;
 vec4 zx = cx;
 vec4 zy = cy;
 vec4 two = vec4(2.0, vec3(0.)); 
 
 vec4 e_radius = vec4(radius*radius, vec3(0.));   // no sqrt available so compare with radius^2 = 2^2 = 2*2 = 4
 
  for(int n=0; n<iterations; n++)
    {
    tmp = zx;
    zx = qs_add(qs_add(qs_mul(zx, zx), -qs_mul(zy, zy)), cx);
    zy = qs_add(qs_mul(qs_mul(zy, tmp), two), cy);
 
    if( qs_compare(qs_add(qs_mul(zx, zx), qs_mul(zy, zy)), e_radius)>0.)
        {
        return(float(n) + 1. - log(log(length(vec2(zx.x, zy.x))))/log(2.)); // http://linas.org/art-gallery/escape/escape.html
        }
    }
  return 0.;
}
 
void main()
{
  float n = qs_mandel(); 
 
  gl_FragColor = vec4((-cos(0.025*n)+1.0)/2.0,
                      (-cos(0.08*n)+1.0)/2.0,
                      (-cos(0.12*n)+1.0)/2.0,
                       1.0);
}
#version 120

// emulated quadruple precision GLSL library
// created by Henry thasler (thasler.org/blog)
// based on the QD library (http://crd-legacy.lbl.gov/~dhbailey/mpdist/)

uniform int iterations;
uniform float frame;
uniform float radius;

uniform vec2 qs_z;
uniform vec2 qs_w;
uniform vec2 qs_h;
uniform vec2 qs_cx;
uniform vec2 qs_cy;

// inline double quick_two_sum(double a, double b, double &err)
vec2 quick_2sum(float a, float b)
{
 float s = a + b;			// double s = a + b;
 return vec2(s, b-(s-a));	// err = b - (s - a);
}

/* Computes fl(a+b) and err(a+b).  */
// inline double two_sum(double a, double b, double &err)
vec2 two_sum(float a, float b)
{
float v,s,e;

 s = a+b;				// double s = a + b;
 v = s-a;				// double bb = s - a;
 e = (a-(s-v))+(b-v);	// err = (a - (s - bb)) + (b - bb);

 return vec2(s,e);
}

vec2 split(float a)
{
float t, hi;
 t = 8193. * a;
 hi = t - (t-a);
 return vec2(hi, a-hi);
}

vec3 three_sum(float a, float b, float c)
{
 vec2 tmp;
 vec3 res;// = vec3(0.);
 float t1, t2, t3;
  tmp = two_sum(a, b); // t1 = qd::two_sum(a, b, t2);
  t1 = tmp.x;
  t2 = tmp.y;

  tmp = two_sum(c, t1); // a  = qd::two_sum(c, t1, t3);
  res.x = tmp.x;
  t3 = tmp.y;

  tmp = two_sum(t2, t3); // b  = qd::two_sum(t2, t3, c);
  res.y = tmp.x;
  res.z = tmp.y;

return res;
}

//inline void three_sum2(double &a, double &b, double &c)
vec3 three_sum2(float a, float b, float c)
{
vec2 tmp;
vec3 res;// = vec3(0.);
float t1, t2, t3;	// double t1, t2, t3;
  tmp = two_sum(a, b); // t1 = qd::two_sum(a, b, t2);
  t1 = tmp.x;
  t2 = tmp.y;

  tmp = two_sum(c, t1); // a  = qd::two_sum(c, t1, t3);
  res.x = tmp.x;
  t3 = tmp.y;

  res.y = t2 + t3;	// b = t2 + t3;
  return res;
}

vec2 two_prod(float a, float b)
{
float p, e;
vec2 va, vb;

 p=a*b;
 va = split(a);
 vb = split(b);

 e = ((va.x*vb.x-p) + va.x*vb.y + va.y*vb.x) + va.y*vb.y;
 return vec2(p, e);
}

vec4 renorm(float c0, float c1, float c2, float c3, float c4)
{
   float s0, s1, s2 = 0.0, s3 = 0.0;
   vec2 tmp;

  // if (QD_ISINF(c0)) return;

  tmp = quick_2sum(c3,c4); // s0 = qd::quick_two_sum(c3, c4, c4);
  s0 = tmp.x;
  c4 = tmp.y;

  tmp = quick_2sum(c2,s0); // s0 = qd::quick_two_sum(c2, s0, c3);
  s0 = tmp.x;
  c3 = tmp.y;

  tmp = quick_2sum(c1,s0); // s0 = qd::quick_two_sum(c1, s0, c2);
  s0 = tmp.x;
  c2 = tmp.y;

  tmp = quick_2sum(c0,s0); // c0 = qd::quick_two_sum(c0, s0, c1);
  c0 = tmp.x;
  c1 = tmp.y;

  s0 = c0;
  s1 = c1;

  tmp = quick_2sum(c0,c1); // s0 = qd::quick_two_sum(c0, c1, s1);
  s0 = tmp.x;
  s1 = tmp.y;

  if (s1 != 0.0) {
    tmp = quick_2sum(s1,c2); // s1 = qd::quick_two_sum(s1, c2, s2);
	s1 = tmp.x;
	s2 = tmp.y;

    if (s2 != 0.0) {
      tmp = quick_2sum(s2,c3); // s2 = qd::quick_two_sum(s2, c3, s3);
	  s2 = tmp.x;
	  s3 = tmp.y;
      if (s3 != 0.0)
        s3 += c4;
      else
        s2 += c4;
    } else {
      tmp = quick_2sum(s1,c3); // s1 = qd::quick_two_sum(s1, c3, s2);
	  s1 = tmp.x;
	  s2 = tmp.y;
      if (s2 != 0.0){
        tmp = quick_2sum(s2,c4); // s2 = qd::quick_two_sum(s2, c4, s3);
		s2 = tmp.x;
		s3 = tmp.y;}
      else{
        tmp = quick_2sum(s1,c4); // s1 = qd::quick_two_sum(s1, c4, s2);
    	s1 = tmp.x;
		s2 = tmp.y;}
}
  } else {
    tmp = quick_2sum(s0,c2); // s0 = qd::quick_two_sum(s0, c2, s1);
  	s0 = tmp.x;
	s1 = tmp.y;
    if (s1 != 0.0) {
      tmp = quick_2sum(s1,c3); // s1 = qd::quick_two_sum(s1, c3, s2);
	  s1 = tmp.x;
	  s2 = tmp.y;
      if (s2 != 0.0){
        tmp = quick_2sum(s2,c4); // s2 = qd::quick_two_sum(s2, c4, s3);
		s2 = tmp.x;
		s3 = tmp.y;}
      else{
        tmp = quick_2sum(s1,c4); // s1 = qd::quick_two_sum(s1, c4, s2);
		s1 = tmp.x;
		s2 = tmp.y;}
    } else {
      tmp = quick_2sum(s0,c3); // s0 = qd::quick_two_sum(s0, c3, s1);
      s0 = tmp.x;
	  s1 = tmp.y;
      if (s1 != 0.0){
        tmp = quick_2sum(s1,c4); // s1 = qd::quick_two_sum(s1, c4, s2);
		s1 = tmp.x;
		s2 = tmp.y;}
      else{
        tmp = quick_2sum(s0,c4); // s0 = qd::quick_two_sum(s0, c4, s1);
		s0 = tmp.x;
		s1 = tmp.y;}
    }
  }

  return vec4(s0, s1, s2, s3);

}

vec4 renorm4(float c0, float c1, float c2, float c3)
{
  float s0, s1, s2 = 0.0, s3 = 0.0;
  vec2 tmp;
  // if (QD_ISINF(c0)) return;

  tmp = quick_2sum(c2,c3); // s0 = qd::quick_two_sum(c2, c3, c3);
  s0 = tmp.x;
  c3 = tmp.y;

  tmp = quick_2sum(c1,s0); // s0 = qd::quick_two_sum(c1, s0, c2);
  s0 = tmp.x;
  c2 = tmp.y;

  tmp = quick_2sum(c0,s0); // c0 = qd::quick_two_sum(c0, s0, c1);
  c0 = tmp.x;
  c1 = tmp.y;

  s0 = c0;
  s1 = c1;
  if (s1 != 0.0) {
	tmp = quick_2sum(s1,c2); // s1 = qd::quick_two_sum(s1, c2, s2);
	s1 = tmp.x;
	s2 = tmp.y;

    if (s2 != 0.0){
	  tmp = quick_2sum(s2,c3); // s2 = qd::quick_two_sum(s2, c3, s3);
	  s2 = tmp.x;
	  s3 = tmp.y;}
    else{
	  tmp = quick_2sum(s1,c3); // s1 = qd::quick_two_sum(s1, c3, s2);
	  s1 = tmp.x;
	  s2 = tmp.y;}
  } else {
	  tmp = quick_2sum(s0,c2); // s0 = qd::quick_two_sum(s0, c2, s1);
	  s0 = tmp.x;
	  s1 = tmp.y;
    if (s1 != 0.0){
	  tmp = quick_2sum(s1,c3); // s1 = qd::quick_two_sum(s1, c3, s2);
	  s1 = tmp.x;
	  s2 = tmp.y;}
    else{
	  tmp = quick_2sum(s0,c3); // s0 = qd::quick_two_sum(s0, c3, s1);
	  s0 = tmp.x;
	  s1 = tmp.y;}
  }

 return vec4(s0, s1, s2, s3);
}

vec3 quick_three_accum(float a, float b, float c)
{
  vec2 tmp;
  float s;
  bool za, zb;

  tmp = two_sum(b, c); // s = qd::two_sum(b, c, b);
  s = tmp.x;
  b = tmp.y;

  tmp = two_sum(a, s); // s = qd::two_sum(a, s, a);
  s = tmp.x;
  a = tmp.y;

  za = (a != 0.0);
  zb = (b != 0.0);

  if (za && zb)
    return vec3(a,b,s);

  if (!zb) {
    b = a;
    a = s;
  } else {
    a = s;
  }

  return vec3(a,b,0.);
}

// inline qd_real qd_real::ieee_add(const qd_real &a, const qd_real &b)
vec4 qs_ieee_add(vec4 _a, vec4 _b)
{
 vec2 tmp=vec2(0.);
 vec3 tmp3=vec3(0.);
 int i, j, k;
 float s, t;
 float u, v;   // double-length accumulator
 float x[4] = float[4](0.0, 0.0, 0.0, 0.0);
 float a[4], b[4];

  a[0] = _a.x;
  a[1] = _a.y;
  a[2] = _a.z;
  a[3] = _a.w;

  b[0] = _b.x;
  b[1] = _b.y;
  b[2] = _b.z;
  b[3] = _b.w;

  i = j = k = 0;
  if (abs(a[i]) > abs(b[j]))
    u = a[i++];
  else
    u = b[j++];
  if (abs(a[i]) > abs(b[j]))
    v = a[i++];
  else
    v = b[j++];

  tmp = quick_2sum(u,v); // u = qd::quick_two_sum(u, v, v);
  u = tmp.x;
  v = tmp.y;

  while (k < 4) {
    if (i >= 4 && j >= 4) {
      x[k] = u;
      if (k < 3)
        x[++k] = v;
      break;
    }

    if (i >= 4)
      t = b[j++];
    else if (j >= 4)
      t = a[i++];
    else if (abs(a[i]) > abs(b[j])) {
      t = a[i++];
    } else
      t = b[j++];

    tmp3 = quick_three_accum(u,v,t)  ; // s = qd::quick_three_accum(u, v, t);
	u = tmp3.x;
	v = tmp3.y;
	s = tmp3.z;

    if (s != 0.0) {
      x[k++] = s;
    }
  }

   // add the rest.
  for (k = i; k < 4; k++)
    x[3] += a[k];
  for (k = j; k < 4; k++)
    x[3] += b[k];

  // qd::renorm(x[0], x[1], x[2], x[3]);
  // return qd_real(x[0], x[1], x[2], x[3]);
  return renorm4(x[0], x[1], x[2], x[3]);
}

// inline qd_real qd_real::sloppy_add(const qd_real &a, const qd_real &b)
vec4 qs_sloppy_add(vec4 a, vec4 b)
{
 float s0, s1, s2, s3;
 float t0, t1, t2, t3;

 float v0, v1, v2, v3;
 float u0, u1, u2, u3;
 float w0, w1, w2, w3;

 vec2 tmp;
 vec3 tmp3;

  s0 = a.x + b.x;	// s0 = a[0] + b[0];
  s1 = a.y + b.y;	// s1 = a[1] + b[1];
  s2 = a.z + b.z;	// s2 = a[2] + b[2];
  s3 = a.w + b.w;	// s3 = a[3] + b[3];  

  v0 = s0 - a.x;	// v0 = s0 - a[0];
  v1 = s1 - a.y;	// v1 = s1 - a[1];
  v2 = s2 - a.z;	// v2 = s2 - a[2];
  v3 = s3 - a.w;	// v3 = s3 - a[3];

  u0 = s0 - v0;
  u1 = s1 - v1;
  u2 = s2 - v2;
  u3 = s3 - v3;

  w0 = a.x - u0;	// w0 = a[0] - u0;
  w1 = a.y - u1;	// w1 = a[1] - u1;
  w2 = a.z - u2;	// w2 = a[2] - u2;
  w3 = a.w - u3;	// w3 = a[3] - u3; 

  u0 = b.x - v0;	// u0 = b[0] - v0;
  u1 = b.y - v1;	// u1 = b[1] - v1;
  u2 = b.z - v2;	// u2 = b[2] - v2;
  u3 = b.w - v3;	// u3 = b[3] - v3;

  t0 = w0 + u0;
  t1 = w1 + u1;
  t2 = w2 + u2;
  t3 = w3 + u3;

  tmp = two_sum(s1, t0); // s1 = qd::two_sum(s1, t0, t0);
  s1 = tmp.x;
  t0 = tmp.y;

  tmp3 = three_sum(s2, t0, t1); // qd::three_sum(s2, t0, t1);
  s2 = tmp3.x;
  t0 = tmp3.y;
  t1 = tmp3.z;

  tmp3 = three_sum2(s3, t0, t2); // qd::three_sum2(s3, t0, t2);
  s3 = tmp3.x;
  t0 = tmp3.y;
  t2 = tmp3.z;

  t0 = t0 + t1 + t3;

  // qd::renorm(s0, s1, s2, s3, t0);
  return renorm(s0, s1, s2, s3, t0); // return qd_real(s0, s1, s2, s3);
}

vec4 qs_add(vec4 _a, vec4 _b)
{
  return qs_sloppy_add(_a, _b);
//  return qs_ieee_add(_a, _b);
}  

vec4 qs_mul(vec4 a, vec4 b)
{
  float p0, p1, p2, p3, p4, p5;
  float q0, q1, q2, q3, q4, q5;
  float t0, t1;
  float s0, s1, s2;
  vec2 tmp;
  vec3 tmp3;

  tmp = two_prod(a.x, b.x); // p0 = qd::two_prod(a[0], b[0], q0);
  p0 = tmp.x;
  q0 = tmp.y;

  tmp = two_prod(a.x, b.y); // p1 = qd::two_prod(a[0], b[1], q1);
  p1 = tmp.x;
  q1 = tmp.y;

  tmp = two_prod(a.y, b.x); // p2 = qd::two_prod(a[1], b[0], q2);
  p2 = tmp.x;
  q2 = tmp.y;

  tmp = two_prod(a.x, b.z); // p3 = qd::two_prod(a[0], b[2], q3);
  p3 = tmp.x;
  q3 = tmp.y;

  tmp = two_prod(a.y, b.y); // p4 = qd::two_prod(a[1], b[1], q4);
  p4 = tmp.x;
  q4 = tmp.y;

  tmp = two_prod(a.z, b.x); // p5 = qd::two_prod(a[2], b[0], q5);
  p5 = tmp.x;
  q5 = tmp.y;

  /* Start Accumulation */
  tmp3 = three_sum(p1, p2, q0); // qd::three_sum(p1, p2, q0);
  p1 = tmp3.x;
  p2 = tmp3.y;
  q0 = tmp3.z;

  /* Six-Three Sum  of p2, q1, q2, p3, p4, p5. */
  tmp3 = three_sum(p2, q1, q2); // qd::three_sum(p2, q1, q2);
  p2 = tmp3.x;
  q1 = tmp3.y;
  q2 = tmp3.z;

  tmp3 = three_sum(p3, p4, p5); // qd::three_sum(p3, p4, p5);
  p3 = tmp3.x;
  p4 = tmp3.y;
  p5 = tmp3.z;

  /* compute (s0, s1, s2) = (p2, q1, q2) + (p3, p4, p5). */
  tmp = two_sum(p2, p3); // s0 = qd::two_sum(p2, p3, t0);
  s0 = tmp.x;
  t0 = tmp.y;

  tmp = two_sum(q1, p4); // s1 = qd::two_sum(q1, p4, t1);
  s1 = tmp.x;
  t1 = tmp.y;

  s2 = q2 + p5;
  tmp = two_sum(s1, t0); // s1 = qd::two_sum(s1, t0, t0);
  s1 = tmp.x;
  t0 = tmp.y;
  s2 += (t0 + t1);

  /* O(eps^3) order terms */
  s1 += a.x*b.w + a.y*b.z + a.z*b.y + a.w*b.x + q0 + q3 + q4 + q5;

  return renorm(p0, p1, s0, s1, s2); // qd::renorm(p0, p1, s0, s1, s2);
}

float ds_compare(vec2 dsa, vec2 dsb)
{
 if (dsa.x < dsb.x) return -1.;
 else if (dsa.x == dsb.x)
	{
	if (dsa.y < dsb.y) return -1.;
	else if (dsa.y == dsb.y) return 0.;
	else return 1.;
	}
 else return 1.;
}

float qs_compare(vec4 qsa, vec4 qsb)
{
 if(ds_compare(qsa.xy, qsb.xy)<0.) return -1.; // if (dsa.x < dsb.x) return -1.;
 else if (ds_compare(qsa.xy, qsb.xy) == 0.) // else if (dsa.x == dsb.x)
	{
	if(ds_compare(qsa.zw, qsb.zw)<0.) return -1.; // if (dsa.y < dsb.y) return -1.;
	else if (ds_compare(qsa.zw, qsb.zw) == 0.) return 0.;// else if (dsa.y == dsb.y) return 0.;
	else return 1.;
	}
 else return 1.;
}

float qs_mandel(void)
{
 vec4 qs_tx = vec4(gl_TexCoord[0].x, vec3(0.));		// get position of current pixel
 vec4 qs_ty = vec4(gl_TexCoord[0].y, vec3(0.));

 // initialize complex variable with respect to current position, zoom, ...
 vec4 cx = qs_add(qs_add(vec4(qs_cx,0.,0.),qs_mul(qs_tx,vec4(qs_z,0.,0.))),vec4(qs_w,0.,0.));
 vec4 cy = qs_add(qs_add(vec4(qs_cy,0.,0.),qs_mul(qs_ty,vec4(qs_z,0.,0.))),vec4(qs_h,0.,0.));  

 vec4 tmp;
 vec4 zx = cx;
 vec4 zy = cy;
 vec4 two = vec4(2.0, vec3(0.)); 

 vec4 e_radius = vec4(radius*radius, vec3(0.));   // no sqrt available so compare with radius^2 = 2^2 = 2*2 = 4

  for(int n=0; n<iterations; n++)
	{
	tmp = zx;
	zx = qs_add(qs_add(qs_mul(zx, zx), -qs_mul(zy, zy)), cx);
	zy = qs_add(qs_mul(qs_mul(zy, tmp), two), cy);

	if( qs_compare(qs_add(qs_mul(zx, zx), qs_mul(zy, zy)), e_radius)>0.)
		{
		return(float(n) + 1. - log(log(length(vec2(zx.x, zy.x))))/log(2.));	// http://linas.org/art-gallery/escape/escape.html
		}
	}
  return 0.;
}

void main()
{
  float n = qs_mandel(); 

  gl_FragColor = vec4((-cos(0.025*n)+1.0)/2.0,
					  (-cos(0.08*n)+1.0)/2.0,
					  (-cos(0.12*n)+1.0)/2.0,
				       1.0);
}

Please note that there are two methods in the qs_add-function to add two quad-singles: “sloppy_add”, which is faster and less accurate and “ieee_add” (nice and slow).  You can use either of them.

Result

It actually works! New worlds of our mandelbrot lay ahead. Undiscovered features can be made visible with just a blink of our GPU eye.

Performance

Quad-Single works fine but is really slooooooooow… See for yourself:

CPU: Intel i5-2400 @ 3.1 GHz
GPU: ATI HD4870

Compared with emulated double precision (51 FPS) and hardware accelerated double precision (154 FPS) the emulated quad-precision (6 FPS) is taking it’s time.

Limitations

As you may have noticed if you actually tried this demo, zooming and scrolling beyond zoom levels of 48 is a bit inaccurate. This is due to the limited precision of the variables that the main (Qt) program hands over to the shader. It uses double precision (more precisely: emulated double) and is limited to a minimal step width that is well above the quad single precision of the shader. This is an issue I’m going to solve in another post (hopefully…).

Conclusion

Quadruple precision is – in terms of computational resources – very expensive to implement. It is suitable for real-time applications if you stick with simple calculations.

Eric Bainville has written some code for fp128 (128-bit fixed point numbers). Maybe I can try this with GLSL in the future and see how it performs.

The reduced computing precision (only long doubles are generally available in Qt) on the Qt side is currently limiting the possibilities of the shader performance. An equal or higher precision as in the shader is required to explore the full depth of the emulated quadruple precision in GLSL. Maybe the quad-single concept can be extended to quad-double.

Sourcecode

Download: GLSL_QuadSingleMandel.zip

This entry was posted in GLSL, OpenGL, Programming and tagged , , . Bookmark the permalink.

3 Responses to Heavy computing with GLSL – Part 5: Emulated quadruple precision

  1. human says:

    Nice! How do you explain the fact that there is only a 5-fold difference between single and double-single, but there is an 8-fold difference between double-single and quad-single? Is it because the single-precision speed is underestimated (e.g. because there is constant overhead) or is it because the quad-single version uses too many registers?

    Also, what is the license for your code?

    • Henry says:

      Actually, it’s both. With single precision you usually get several hundred FPS (2-3ms/frame) which means the GPU is bored and the CPU/memory feeding the shader is the limiting factor (lets say its 1ms). This overhead is insignificant regarding the fact that it takes like 120ms to render with quad precision.

      The other factor is the complexity of the shader. Single uses 36 lines, double-single uses 138 lines and quad-single about 550 including comments/blank lines. Add the number of function calls, variables and calculations for each shader run and you might understand why it is so slow.

      There are no decent (or functioning on my GPU) GLSL profilers that I know of. So finding the bottlenecks and optimizing is somewhat difficult.

      The code is free but you must give me credits. You can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

  2. Adam says:

    Hi,
    Thx for interesting article and code.
    Double double precision arithmetic library for Cuda now available
    http://forums.nvidia.com/index.php?showtopic=218452

    Regards

Leave a Reply