Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve float precision #117

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ast.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let stringToJumpKeyword = function

type Expr =
| Int of int * string
| Float of float * string
| Float of decimal * string
| Var of Ident
| Op of string
| FunCall of Expr * Expr list
Expand Down
2 changes: 1 addition & 1 deletion src/parse.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module private ParseImpl =
let conv s =
let ok, res = System.Int32.TryParse(s : string)
if ok then Ast.Int (res, "")
else Ast.Float (try float s, "" with _ -> failwith ("invalid number: " + s))
else Ast.Float (try decimal s, "" with _ -> failwith ("invalid number: " + s))
regex r .>> ws |>> conv

let anyNumber =
Expand Down
19 changes: 12 additions & 7 deletions src/printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System
open Ast
open Options.Globals
open System.Text.RegularExpressions

module private PrinterImpl =

Expand Down Expand Up @@ -46,13 +47,17 @@ module private PrinterImpl =
List.map toS li |> String.concat ","

let floatToS f =
let si = if f < 0. then "-" else ""
let test = out "%g" (abs f)
// display "3." instead of "3"
if fst (System.Int32.TryParse test) then (out "%g." f)
// display ".5" instead of "0.5"
else if test.[0] = '0' then si + (test.[1..])
else si + test
let si = if f < 0.M then "-" else ""
let f = abs (float f)
let str = f.ToString("g", System.Globalization.CultureInfo.InvariantCulture)
let str = if str.EndsWith(".0") then str.[0..str.Length-2] else str

if Regex.Match(str, "^\\d+$").Success then
si + str + "." // display "3." instead of "3"
elif str.[0] = '0' && str.Length > 2 then
si + (str.[1..]) // display ".5" instead of "0.5"
else
si + str

let rec exprToS exp = exprToSLevel 0 exp

Expand Down
32 changes: 16 additions & 16 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let rec private simplifyExpr env = function
| FunCall(Op ",", [e1; FunCall(Op ",", [e2; e3])]) ->
FunCall(Op ",", [simplifyExpr env (FunCall(Op ",", [e1; e2])); e3])

| FunCall(Op "-", [x; Float (f, s)]) when f < 0. ->
| FunCall(Op "-", [x; Float (f, s)]) when f < 0.M ->
FunCall(Op "+", [x; Float (-f, s)]) |> simplifyExpr env
| FunCall(Op "-", [x; Int (i, s)]) when i < 0 ->
FunCall(Op "+", [x; Int (-i, s)]) |> simplifyExpr env
Expand All @@ -92,15 +92,15 @@ let rec private simplifyExpr env = function
| FunCall(Op "!=", [Float (i1,_); Float (i2,_)]) -> bool(i1 <> i2)

// Stupid simplifications (they can be useful to simplify rewritten code)
| FunCall(Op "/", [e; Float (1.,_)]) -> e
| FunCall(Op "*", [e; Float (1.,_)]) -> e
| FunCall(Op "*", [Float (1.,_); e]) -> e
| FunCall(Op "*", [_; Float (0.,_) as e]) -> e
| FunCall(Op "*", [Float (0.,_) as e; _]) -> e
| FunCall(Op "+", [e; Float (0.,_)]) -> e
| FunCall(Op "+", [Float (0.,_); e]) -> e
| FunCall(Op "-", [e; Float (0.,_)]) -> e
| FunCall(Op "-", [Float (0.,_); e]) -> FunCall(Op "-", [e])
| FunCall(Op "/", [e; Float (1.M,_)]) -> e
| FunCall(Op "*", [e; Float (1.M,_)]) -> e
| FunCall(Op "*", [Float (1.M,_); e]) -> e
| FunCall(Op "*", [_; Float (0.M,_) as e]) -> e
| FunCall(Op "*", [Float (0.M,_) as e; _]) -> e
| FunCall(Op "+", [e; Float (0.M,_)]) -> e
| FunCall(Op "+", [Float (0.M,_); e]) -> e
| FunCall(Op "-", [e; Float (0.M,_)]) -> e
| FunCall(Op "-", [Float (0.M,_); e]) -> FunCall(Op "-", [e])

// No simplification when numbers have different suffixes
| FunCall(_, [Int (_, su1); Int (_, su2)]) as e when su1 <> su2 -> e
Expand All @@ -112,7 +112,7 @@ let rec private simplifyExpr env = function
| FunCall(Op "/", [Int (i1, su); Int (i2, _)]) -> Int (i1 / i2, su)
| FunCall(Op "%", [Int (i1, su); Int (i2, _)]) -> Int (i1 % i2, su)

| FunCall(Op "-", [Float (0.0,su)]) -> Float (0.0, su)
| FunCall(Op "-", [Float (0.M,su)]) -> Float (0.M, su)
| FunCall(Op "-", [Float (f1,su)]) -> Float (-f1, su)
| FunCall(Op "-", [Float (i1,su); Float (i2,_)]) -> Float (i1 - i2, su)
| FunCall(Op "+", [Float (i1,su); Float (i2,_)]) -> Float (i1 + i2, su)
Expand All @@ -123,12 +123,12 @@ let rec private simplifyExpr env = function
else div

// iq's smoothstep trick: http://www.pouet.net/topic.php?which=6751&page=1#c295695
| FunCall(Var var, [Float (0.,_); Float (1.,_); _]) as e when var.Name = "smoothstep" -> e
| FunCall(Var var, [Float (0.M,_); Float (1.M,_); _]) as e when var.Name = "smoothstep" -> e
| FunCall(Var var, [a; b; x]) when var.Name = "smoothstep" && options.smoothstepTrick ->
let sub1 = FunCall(Op "-", [x; a])
let sub2 = FunCall(Op "-", [b; a])
let div = FunCall(Op "/", [sub1; sub2]) |> mapExpr env
FunCall(Var (Ident "smoothstep"), [Float (0.,""); Float (1.,""); div])
FunCall(Var (Ident "smoothstep"), [Float (0.M,""); Float (1.M,""); div])

| Dot(e, field) when options.canonicalFieldNames <> "" -> Dot(e, renameField field)

Expand All @@ -138,9 +138,9 @@ let rec private simplifyExpr env = function
| _ -> e

// pi is acos(-1), pi/2 is acos(0)
| Float(f, _) when f = 3.141592653589793 -> FunCall(Var (Ident "acos"), [Float (-1., "")])
| Float(f, _) when f = 6.283185307179586 -> FunCall(Op "*", [Float (2., ""); FunCall(Var (Ident "acos"), [Float (-1., "")])])
| Float(f, _) when f = 1.5707963267948966 -> FunCall(Var (Ident "acos"), [Float (0., "")])
| Float(f, _) when float f = 3.141592653589793 -> FunCall(Var (Ident "acos"), [Float (-1.M, "")])
| Float(f, _) when float f = 6.283185307179586 -> FunCall(Op "*", [Float (2.M, ""); FunCall(Var (Ident "acos"), [Float (-1.M, "")])])
| Float(f, _) when float f = 1.5707963267948966 -> FunCall(Var (Ident "acos"), [Float (0.M, "")])

| e -> e

Expand Down
1 change: 1 addition & 0 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
--no-renaming --format c-array -o tests/unit/operators.expected tests/unit/operators.frag
--no-renaming --format c-array -o tests/unit/minus-zero.expected tests/unit/minus-zero.frag
--no-renaming --format indented -o tests/unit/inline.expected tests/unit/inline.frag
--no-renaming --format c-array -o tests/unit/float.frag.expected tests/unit/float.frag

# Partial renaming tests

Expand Down
1 change: 0 additions & 1 deletion tests/real/disco.expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
*/

var disco_frag = `uniform vec2 resolution;uniform float time;uniform sampler2D tex0,tex1,tex2,tex3;vec4 s(vec2 px,float z){float l=3.1415,k=time*sign(z),x=px.x*320.*.0065*z,y=px.y*240.*.006*z,c=sqrt(x*x+y*y);if(c>1.)return vec4(0.);else{float u=-.4*sign(z)+sin(k*.05),v=sqrt(1.-x*x-y*y),q=y*sin(u)-v*cos(u);y=y*cos(u)+v*sin(u);v=acos(y);u=acos(x/sin(v))/(2.*l)*120.*sign(q)-k;v=v*60./l;q=cos(floor(v/l));c=pow(abs(cos(u)*sin(v)),.2)*.1/(q+sin(float(int((u+l/2.)/l))+k*.6+cos(q*25.)))*pow(1.-c,.9);vec4 res;if(c<0.)res=vec4(-c/2.*abs(cos(k*.1)),0.,-c*2.*abs(sin(k*.04)),1.);else res=vec4(c,c*2.,c*2.,1.);return res;}}void main(){vec2 p=-1.+2.*gl_FragCoord.xy/resolution.xy;vec4 c=vec4(0.);for(int i=80;i>0;i--)c+=s(p,1.-float(i)/80.)*(.008-float(i)*5e-05);vec4 d=s(p,1.);gl_FragColor=(d.w==0.?s(p,-.2)*.02:d)+sqrt(c);}`

5 changes: 2 additions & 3 deletions tests/real/kinder_painter.expected
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@
"vec4 ref;"
"vec2 p=-1.+2.*gl_FragCoord.xy/resolution.xy;"
"p*=vec2(resolution.x/resolution.y,1.);"
"fpar00[0]=vec4(1.2*sin(2.07342*time),0.,1.8*sin(2.45041*time+1.),1);"
"fpar00[1]=vec4(1.5*sin(1.94776*time+4.),sin(1.8221*time+1.9),1.8*sin(1.8221*time),1);"
"fpar00[0]=vec4(1.2*sin(2.073423*time),0.,1.8*sin(2.450409*time+1.),1);"
"fpar00[1]=vec4(1.5*sin(1.947761*time+4.),sin(1.822099*time+1.9),1.8*sin(1.822099*time),1);"
"fpar00[2]=vec4(-1.2,0.,0.,.4);"
"fpar00[3]=vec4(1.2,0.,0.,.4);"
"fpar00[4]=vec4(0.,1.,0.,2.);"
Expand Down Expand Up @@ -183,4 +183,3 @@
"col=mix(col,col2,.5-.5*ref.w);"
"gl_FragColor=col;"
"}",

7 changes: 3 additions & 4 deletions tests/real/leizex.expected
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _leizex_frag:
db 'f=f*f*(3.-2.*f);'
db 'int n=ip.x+ip.y*57+113*ip.z+sem;'
db 'float res=mix(mix(mix(coolfFunc3d2(n+0),coolfFunc3d2(n+1),f.x),mix(coolfFunc3d2(n+57),coolfFunc3d2(n+58),f.x),f.y),mix(mix(coolfFunc3d2(n+113),coolfFunc3d2(n+114),f.x),mix(coolfFunc3d2(n+170),coolfFunc3d2(n+171),f.x),f.y),f.z);'
db 'return 1.-res*9.31323e-10;'
db 'return 1.-res*(1./1073741824.);'
db '}'
db 'vec2 celular(in vec3 x)'
db '{'
Expand All @@ -29,7 +29,7 @@ _leizex_frag:
db 'for(int i=-1;i<=1;i++)'
db '{'
db 'int nn=ip.x+i+57*(ip.y+j)+113*(ip.z+k);'
db 'vec3 di=vec3(float(i),float(j),float(k))-f+vec3(coolfFunc3d2(nn),coolfFunc3d2(nn+1217),coolfFunc3d2(nn+2513))/2.14748e+09;'
db 'vec3 di=vec3(float(i),float(j),float(k))-f+vec3(coolfFunc3d2(nn),coolfFunc3d2(nn+1217),coolfFunc3d2(nn+2513))/2147483647.;'
db 'float d2=dot(di,di);'
db 'if(d2<dmin.x)'
db 'dmin.y=dmin.x,dmin.x=d2;'
Expand Down Expand Up @@ -67,7 +67,7 @@ _leizex_frag:
db 'float r2=s.x*s.x*.32+s.y*s.y;'
db 'vec2 d=s*(7.-sqrt(37.5-11.5*r2))/(r2+1.);'
db 'vec3 rayTar=vec3(0.,1.5,2.);'
db 'rayPos=rayTar+vec3(-sin(6.28319*ftime/20.),.75*cos(6.28319*ftime/20.+.5),-cos(6.28319*ftime/20.));'
db 'rayPos=rayTar+vec3(-sin(6.2831853*ftime/20.),.75*cos(6.2831853*ftime/20.+.5),-cos(6.2831853*ftime/20.));'
db 'rayTar+=.075*vec3(noise3f(vec3(2.*ftime,0.,.5),0),noise3f(vec3(2.*ftime,.1,.4),7),noise3f(vec3(2.*ftime,.2,.3),9));'
db 'float roll=.1*noise3f(vec3(2.*ftime,0.,0.),13);'
db 'vec3 up=vec3(0.,cos(roll),sin(roll)),dd=normalize(rayTar-rayPos),rr=normalize(cross(dd,up)),uu=normalize(cross(rr,dd));'
Expand Down Expand Up @@ -105,4 +105,3 @@ _leizex_frag:
db 'col*=.5+.5*(1.-p.x)*(1.+p.x);'
db 'gl_FragColor=vec4(col,1.);'
db '}', 0

2 changes: 1 addition & 1 deletion tests/real/lunaquatic.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const char *lunaquatic_frag =
"{"
"int x=int(v.x*40.+v.y*6400.);"
"x=x<<13^x;"
"return 1.-float(x*(x*x*15731+789221)+1376312589&2147483647)/1.07374e+09;"
"return 1.-float(x*(x*x*15731+789221)+1376312589&2147483647)/1073741824.;"
"}"
"float p(float z)"
"{"
Expand Down
2 changes: 1 addition & 1 deletion tests/real/oscars_chair.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const char *oscars_chair_frag =
"void main()"
"{"
"if(gl_TexCoord[0].y==0)"
"gl_FragColor.xyz=vec3(gl_TexCoord[0].xx,fract(sin(float((int(gl_FragCoord.x)*19349663^int(gl_FragCoord.y)*83492791)%38069))*43758.5));"
"gl_FragColor.xyz=vec3(gl_TexCoord[0].xx,fract(sin(float((int(gl_FragCoord.x)*19349663^int(gl_FragCoord.y)*83492791)%38069))*43758.545));"
"else"
" if(gl_TexCoord[0].y==1)"
"{"
Expand Down
2 changes: 1 addition & 1 deletion tests/real/the_real_party_is_in_your_pocket.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ const char *the_real_party_is_in_your_pocket_frag =
"float y;"
"float m()"
"{"
"return fract(sin(y++)*43758.5);"
"return fract(sin(y++)*43758.5453123);"
"}"
"vec3 h(vec3 l,vec3 v)"
"{"
Expand Down
6 changes: 3 additions & 3 deletions tests/real/yx_long_way_from_home.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const char *yx_long_way_from_home_frag =
"}"
"vec2 s(const float v)"
"{"
"return fract(sin(vec2(v,v+1.))*vec2(43758.5));"
"return fract(sin(vec2(v,v+1.))*vec2(43758.5453123));"
"}"
"int f;"
"vec3 e(vec3 v)"
Expand All @@ -48,7 +48,7 @@ const char *yx_long_way_from_home_frag =
"}"
"float m(vec3 m)"
"{"
"float x=1e+09;"
"float x=1000000000.;"
"x=min(x,length(m-vec3(0,5,0))-2.5);"
"vec3 i=m;"
"i.x=abs(i.x-1.1)-.2;"
Expand Down Expand Up @@ -192,7 +192,7 @@ const char *yx_long_way_from_home_frag =
"void main()"
"{"
"vec2 v=gl_FragCoord.xy/iResolution.xy-.5;"
"float y=iTime+(v.x+iResolution.x*v.y)*1.51269;"
"float y=iTime+(v.x+iResolution.x*v.y)*1.51269341231;"
"i=s(y);"
"v+=(i-.5)/iResolution.xy;"
"v.x*=iResolution.x/iResolution.y;"
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/float.frag
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
void precision() {
float a = 0.00000012345;
float b = 1.234567891234;
float c = 1234567.891234;
}

void main()
{
float f1 = 1.5;
Expand All @@ -10,8 +16,5 @@ float f7 = 2E-9;
float f8 = 2E+6;
float f9 = 2e10;

float n = 1. / (f1 + f2 + f3 + f4 + f5);
float o = 1. / (f6 * f8 * f7 * f9);

gl_FragColor=vec4(n,o,n,0.);
gl_FragColor=vec4(0.);
}
16 changes: 8 additions & 8 deletions tests/unit/float.frag.expected
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* File generated with Shader Minifier 1.1.6
* http://www.ctrl-alt-test.fr
*/
#ifndef FLOAT_FRAG_EXPECTED_
# define FLOAT_FRAG_EXPECTED_

const char *float_frag =
// tests/unit/float.frag
"void precision()"
"{"
"float a=1.2345e-07,b=1.234567891234,c=1234567.891234;"
"}"
"void main()"
"{"
"float e=1.5,r=3e-07,o=.42,a=0.,l=0.,g=-.002,i=2e-09,v=2e+06,c=2e+10,d=1./(e+r+o+a+l),f=1./(g*v*i*c);"
"gl_FragColor=vec4(d,f,d,0.);"
"}";

#endif // FLOAT_FRAG_EXPECTED_
"float f1=1.5,f2=3e-07,f3=.42,f4=0.,f5=0.,f6=-.002,f7=2e-09,f8=2000000.,f9=20000000000.;"
"gl_FragColor=vec4(0.);"
"}",
3 changes: 1 addition & 2 deletions tests/unit/operators.expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
"void main()"
"{"
"int a=11,b=7,c=2;"
"float f=2.62146;"
"float f=2.621464704;"
"f=.7;"
"f=1.4/3.;"
"float g=mod(8.,3.);"
"}",