Skip to content

Commit 489fb91

Browse files
committed
webgl fix
1 parent faf6f98 commit 489fb91

File tree

8 files changed

+574
-29
lines changed

8 files changed

+574
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ IEEE arithmetic and can produce artifacts, but CPU arithmetic is robust and brow
1414
### Benchmark
1515
![](https://i.imgur.com/dXeSYKO.png)
1616

17-
You can check [quality/performance](https://munrocket.github.io/double.js/test/bench/bench.html) and [correctness](https://munrocket.github.io/double.js/test/e2e.html) of double.js library in your browser.
17+
You can check [quality / performance](https://munrocket.github.io/double.js/test/bench/bench.html) and [correctness](https://munrocket.github.io/double.js/test/e2e.html) of double.js library in your browser.
1818

1919
### Usage
2020
Include double.js script to webpage or install npm package. Here some basic examples

dist/double.esm.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function oneSqr(a) {
1919
let hl = al * ah;
2020
return { hi: t, lo: ah * ah - t + hl + hl + al * al };
2121
}
22-
export class Double {
22+
class Double {
2323
constructor(obj) {
2424
if (obj instanceof Double) {
2525
this.hi = obj.hi;
@@ -515,4 +515,5 @@ export class Double {
515515
return Double.pow2n(Double.clone(this), exp);
516516
}
517517
}
518-
export default { Double };
518+
export default Double;
519+
export { Double };

dist/double.js

Lines changed: 521 additions & 1 deletion
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"watch": "onchange 'src/*.ts' -- npm run build",
1010
"build": "npm run build-esm && npm run build-browser && npm run build-wasm",
1111
"build-esm": "esbuild ./src/double.ts --outfile=./dist/double.esm.js",
12-
"build-browser": "esbuild ./src/double.ts --bundle --minify --outfile=./dist/double.js",
12+
"build-browser": "esbuild ./src/double.ts --bundle --outfile=./dist/double.js",
1313
"build-wasm": "asc --extension=.as test/bench/bench.as wasm/double.as -b test/bench/bench.wasm -O3",
1414
"test": "node -r esm ./test/test.js",
1515
"ci": "npm run start & sleep 3 && npm run e2e && npm run cov",

src/double.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function oneSqr(a: float) {
3030

3131
/* Main class for double-word arithmetic */
3232

33-
export class Double {
33+
class Double {
3434

3535
hi: float;
3636
lo: float;
@@ -412,4 +412,5 @@ export class Double {
412412
pown(exp: float): Double { return Double.pow2n(Double.clone(this), exp); }
413413
}
414414

415-
export default { Double };
415+
export default Double;
416+
export { Double };

webgl/double.glsl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ vec2 add22(vec2 X, vec2 Y) {
3030
}
3131

3232
vec2 sub22(vec2 X, vec2 Y) {
33-
return add22(X, -Y);
33+
vec2 S = twoSum(X[0], -Y[0]);
34+
vec2 T = twoSum(X[1], -Y[1]);
35+
vec2 V = fastTwoSum(S[0], add(S[1], T[0]));
36+
return fastTwoSum(V[0], add(T[1], V[1]));
3437
}
3538

3639
vec2 mul22(vec2 X, vec2 Y) {
@@ -41,8 +44,8 @@ vec2 mul22(vec2 X, vec2 Y) {
4144
}
4245

4346
vec2 div22(vec2 X, vec2 Y) {
44-
float s = X[0] / Y[0];
47+
float s = div(X[0], Y[0]);
4548
vec2 T = twoProd(s, Y[0]);
46-
float e = ((((X[0] - T[0]) - T[1]) + X[1]) - s * Y[1]) / Y[0];
47-
return fastTwoSum(s, e);
48-
}
49+
float c = add(sub(sub(X[0], T[0]), T[1]), X[1]);
50+
return fastTwoSum(s, div(sub(c, mul(s, Y[1])), Y[0]));
51+
}

webgl/test.html

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
// Algorithm using fma for faster dd multimplication
1818
// More information: https://github.com/munrocket/double.js
1919
20+
// MIT License. © 2022 munrocket
21+
//
22+
// Emulated double precision lib with mandelbrot set
23+
// Bottom is always pixelated, top is smooth with double
24+
// Algorithm using fma for faster dd multimplication
25+
// Additional information: https://github.com/munrocket/double.js
26+
2027
float add(float a, float b) { return (b != 0.) ? a + b : a; }
2128
float sub(float a, float b) { return (b != 0.) ? a - b : a; }
2229
float mul(float a, float b) { return (b != 1.) ? a * b : a; }
@@ -47,7 +54,10 @@
4754
}
4855
4956
vec2 sub22(vec2 X, vec2 Y) {
50-
return add22(X, -Y);
57+
vec2 S = twoSum(X[0], -Y[0]);
58+
vec2 T = twoSum(X[1], -Y[1]);
59+
vec2 V = fastTwoSum(S[0], add(S[1], T[0]));
60+
return fastTwoSum(V[0], add(T[1], V[1]));
5161
}
5262
5363
vec2 mul22(vec2 X, vec2 Y) {
@@ -58,14 +68,16 @@
5868
}
5969
6070
vec2 div22(vec2 X, vec2 Y) {
61-
float s = X[0] / Y[0];
71+
float s = div(X[0], Y[0]);
6272
vec2 T = twoProd(s, Y[0]);
63-
float e = ((((X[0] - T[0]) - T[1]) + X[1]) - s * Y[1]) / Y[0];
64-
return fastTwoSum(s, e);
73+
float c = add(sub(sub(X[0], T[0]), T[1]), X[1]);
74+
return fastTwoSum(s, div(sub(c, mul(s, Y[1])), Y[0]));
6575
}
6676
6777
//////// end of the library, begin mandelbrot example /////////
6878
79+
//////// end of the library, begin mandelbrot example /////////
80+
6981
vec4 dcadd(vec4 a, vec4 b){
7082
return vec4(add22(a.xy, b.xy), add22(a.zw, b.zw));
7183
}
@@ -87,38 +99,43 @@
8799
return vec4(mul22(c.xy, k), mul22(c.zw, k));
88100
}
89101
90-
const float zoom = 7.4e5;
102+
#define zoom 7.4e5
91103
const vec4 p0 = vec4(-0.2351255, -1e-8, 0.8272157, -1e-9);
92-
const vec4 color = vec4(1, 2, 2.9,0);
93104
vec2 d(float a) { return vec2(a, 0); }
94105
vec4 dc(vec2 c) { return vec4(c.x, 0, c.y, 0); }
106+
vec4 col(float i) {
107+
return .1+.9*cos(log(i*i)*vec4(1, 2, 2.9,0));
108+
}
95109
96110
vec4 single_fractal(vec4 o) {
97111
vec2 c = iResolution.xy, z = gl_FragCoord.xy;
98-
c = z = (z-.5*c)/c.y/zoom + p0.xz;
112+
c = z = (z-.5*c)/(zoom)/c.y + p0.xz;
99113
for (float i; i < 1.; i += 1e-3) {
100-
dot(z = mat2(z,-z.y,z.x)*z + c, z) > 4. ? o = cos(color*log(i*i)) : o;
114+
if(dot(z = mat2(z,-z.y,z.x)*z + c, z) > 4.)
115+
o = col(i);
101116
}
102117
return o;
103118
}
104119
105120
vec4 double_fractal(vec4 o) {
106121
vec2 c0 = iResolution.xy, z0 = gl_FragCoord.xy;
107-
vec4 z, c = z = dcadd(
108-
fcmul(dcsub(dc(z0), dc(.5*c0)), div22(d(1.), twoProd(c0.y, zoom))), p0);
122+
vec4 z, c = z = dcadd(fcmul(dcsub(dc(z0), dc(.5*c0)),
123+
div22(d(1.), twoProd(c0.y, zoom))), p0);
109124
for (float i; i < 1.; i += 1e-3) {
110-
dclen(z = dcadd(dcmul(z, z), c)).x > 4. ? o = cos(color*log(i*i)) : o;
125+
if(dclen(z = dcadd(dcmul(z, z), c)).x > 4.)
126+
o = col(i);
111127
}
112128
return o;
113129
}
114130
115131
void mainImage( out vec4 o, in vec2 fc ) {
116-
if (fc.y / iResolution.y + fc.x / iResolution.x < 1.) {
132+
vec2 uv = fc / iResolution.xy;
133+
if (uv.x + uv.y < 1.) {
117134
o = single_fractal(o);
118135
} else {
119136
o = double_fractal(o);
120137
}
121-
o += .02;
138+
o -= smoothstep(.0025, .001, abs(uv.x+uv.y-1.))
122139
}
123140
`);
124141
</script>

webgpu/double.wgsl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ fn add22(X: vec2<f32>, Y: vec2<f32>) -> vec2<f32> {
2828
return fastTwoSum(V[0], add(T[1], V[1]));
2929
}
3030

31-
fn sub22(X: vec2<f32>, Y: vec2<f32>) -> vec2<f32> {
32-
return add22(X, -Y);
31+
fn add22(X: vec2<f32>, Y: vec2<f32>) -> vec2<f32> {
32+
let S = twoSum(X[0], -Y[0]);
33+
let T = twoSum(X[1], -Y[1]);
34+
let V = fastTwoSum(S[0], add(S[1], T[0]));
35+
return fastTwoSum(V[0], add(T[1], V[1]));
3336
}
3437

3538
fn mul22(X: vec2<f32>, Y: vec2<f32>) -> vec2<f32> {
@@ -41,6 +44,6 @@ fn mul22(X: vec2<f32>, Y: vec2<f32>) -> vec2<f32> {
4144
fn div22(X: vec2<f32>, Y: vec2<f32>) -> vec2<f32> {
4245
let s = div(X[0], Y[0]);
4346
let T = twoProd(s, Y[0]);
44-
let e = div(sub(add(sub(sub(X[0], T[0]), T[1]), X[1]), mul(s, Y[1])), Y[0]);
45-
return fastTwoSum(s, e);
47+
let c = add(sub(sub(X[0], T[0]), T[1]), X[1]);
48+
return fastTwoSum(s, div(sub(c, mul(s, Y[1])), Y[0]));
4649
}

0 commit comments

Comments
 (0)