Skip to content

Commit 97af65c

Browse files
committed
Feature: Floats less than zero can now be simplied using the 'e' format.
E.g. 0.00001 to 1e-5
1 parent b2e437c commit 97af65c

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

ShaderShrinker/Shrinker.Lexer/FloatToken.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,30 @@ public IToken Simplify()
6060

6161
Content = Content.TrimEnd('0');
6262

63+
// Any decimal places?
6364
if (!Content.EndsWith("."))
6465
{
65-
// Remove leading zeroes.
66+
// Yes - Remove leading zeroes.
6667
Content = Content.TrimStart('0');
68+
69+
// Can we simplify using the 'e' format?
70+
if (Content.StartsWith("."))
71+
{
72+
var eFormat = Content.Substring(1);
73+
var zeros = 0;
74+
while (eFormat.Length > 0 && eFormat[0] == '0')
75+
{
76+
zeros++;
77+
eFormat = eFormat.Substring(1);
78+
}
79+
80+
if (eFormat.Length > 0 && zeros > 0)
81+
{
82+
eFormat = $"{eFormat}e-{zeros + 1}";
83+
if (eFormat.Length < Content.Length)
84+
Content = eFormat;
85+
}
86+
}
6787
}
6888
else
6989
{

ShaderShrinker/UnitTests/ShrinkerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ public void CheckDeclarationIsNotJoinedWithDefinitionIfDeepWithinIfPragma()
383383

384384
[Test, Sequential]
385385
public void CheckSimplifyingFloats(
386-
[Values("10.0", "1.1", "0.10", "0.0000", "-0.09", "100.0", "100.1", "1100000.", "1.23f", "-0.1f", ".0f", "0.f", "10.00F", "102.", "001.1", "3.141592653589793238462643383279502884197", "-3.141592653589793238462643383279502884197", "1.541182543454656e-4", "1e10", "0011.0", "-0011.0", "00.0")] string code,
387-
[Values("10.", "1.1", ".1", "0.", "-.09", "1e2", "100.1", "11e5", "1.23", "-.1", "0.", "0.", "10.", "102.", "1.1", "3.1415926", "-3.1415926", "1.5411e-4", "1e10", "11.", "-11.", "0.")] string expectedOutput)
386+
[Values("10.0", "1.1", "0.10", "0.0000", "-0.09", "100.0", "100.1", "1100000.", "1.23f", "-0.1f", ".0f", "0.f", "10.00F", "102.", "001.1", "3.141592653589793238462643383279502884197", "-3.141592653589793238462643383279502884197", "1.541182543454656e-4", "1e10", "0011.0", "-0011.0", "00.0", "0.00001")] string code,
387+
[Values("10.", "1.1", ".1", "0.", "-.09", "1e2", "100.1", "11e5", "1.23", "-.1", "0.", "0.", "10.", "102.", "1.1", "3.1415926", "-3.1415926", "1.5411e-4", "1e10", "11.", "-11.", "0.", "1e-5")] string expectedOutput)
388388
{
389389
var lexer = new Lexer();
390390
lexer.Load(code);

ShaderShrinker/UnitTests/TestFiles/SimplifiedReference/PowerStone.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Processed by 'GLSL Shader Shrinker' (Shrunk by 10 characters)
1+
// Processed by 'GLSL Shader Shrinker' (Shrunk by 13 characters)
22
// (https://github.com/deanthecoder/GLSLShaderShrinker)
33

44
#define Z0 min(iTime, 0.)
@@ -97,13 +97,13 @@ Hit map(vec3 p) {
9797
d2 = min(d2, sdOcta(p, .3) - .005);
9898
}
9999

100-
g += .00008 / (.001 + d2 * d2);
100+
g += 8e-5 / (.001 + d2 * d2);
101101
minH(Hit(d2, 4));
102102
p = op;
103103
p.x = abs(p.x);
104104
p.y += cos(p.x + t) * .05;
105105
d2 = length(p.yz) - .01;
106-
g += .0005 / (.001 + d2 * d2);
106+
g += 5e-4 / (.001 + d2 * d2);
107107
minH(Hit(min(d, d2), 2));
108108
return h;
109109
}

0 commit comments

Comments
 (0)