Skip to content

Commit

Permalink
[math] Add float overloads.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Aug 2, 2024
1 parent dc74032 commit 3412e3e
Show file tree
Hide file tree
Showing 19 changed files with 669 additions and 512 deletions.
40 changes: 21 additions & 19 deletions AST.fu
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public enum FuId
FloatType,
DoubleType,
FloatIntType,
FloatingType,
BoolType,
StringClass,
StringPtrType,
Expand Down Expand Up @@ -1554,7 +1555,7 @@ public class FuSystem : FuScope
FuRangeType# UIntType = FuRangeType.New(0, int.MaxValue);
internal FuIntegerType# LongType = new FuIntegerType { Id = FuId.LongType, Name = "long" };
internal FuRangeType# ByteType = FuRangeType.New(0, 0xff);
FuFloatingType# FloatType = new FuFloatingType { Id = FuId.FloatType, Name = "float" };
internal FuFloatingType# FloatType = new FuFloatingType { Id = FuId.FloatType, Name = "float" };
internal FuFloatingType# DoubleType = new FuFloatingType { Id = FuId.DoubleType, Name = "double" };
internal FuRangeType# CharType = FuRangeType.New(-0x80, 0xffff);
internal FuEnum# BoolType = new FuEnum { Id = FuId.BoolType, Name = "bool" };
Expand Down Expand Up @@ -1828,31 +1829,32 @@ public class FuSystem : FuScope
Add(jsonElementClass);

FuFloatingType# floatIntType = new FuFloatingType { Id = FuId.FloatIntType, Name = "float" };
FuFloatingType# floatingType = new FuFloatingType { Id = FuId.FloatingType, Name = "float" };
FuClass# mathClass = FuClass.New(FuCallType.Static, FuId.None, "Math");
mathClass.Add(FuMethodGroup.New(
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, IntType, FuId.MathAbs, "Abs", false, FuVar.New(LongType, "a")),
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, FloatType, FuId.MathAbs, "Abs", false, FuVar.New(DoubleType, "a"))));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Acos", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Asin", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Atan", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Atan2", FuVar.New(DoubleType, "y"), FuVar.New(DoubleType, "x"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Cbrt", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Acos", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Asin", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Atan", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Atan2", FuVar.New(DoubleType, "y"), FuVar.New(DoubleType, "x"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Cbrt", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatIntType, FuId.MathCeiling, "Ceiling", FuVar.New(DoubleType, "a"));
mathClass.Add(FuMethodGroup.New(
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, IntType, FuId.MathClamp, "Clamp", false, FuVar.New(LongType, "value"), FuVar.New(LongType, "min"), FuVar.New(LongType, "max")),
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, FloatType, FuId.MathClamp, "Clamp", false, FuVar.New(DoubleType, "value"), FuVar.New(DoubleType, "min"), FuVar.New(DoubleType, "max"))));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Cos", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Cosh", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Cos", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Cosh", FuVar.New(DoubleType, "a"));
mathClass.Add(NewConstDouble("E", Math.E));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Exp", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Exp", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatIntType, FuId.MathMethod, "Floor", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathFusedMultiplyAdd, "FusedMultiplyAdd", FuVar.New(DoubleType, "x"), FuVar.New(DoubleType, "y"), FuVar.New(DoubleType, "z"));
mathClass.AddStaticMethod(floatingType, FuId.MathFusedMultiplyAdd, "FusedMultiplyAdd", FuVar.New(DoubleType, "x"), FuVar.New(DoubleType, "y"), FuVar.New(DoubleType, "z"));
mathClass.AddStaticMethod(BoolType, FuId.MathIsFinite, "IsFinite", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(BoolType, FuId.MathIsInfinity, "IsInfinity", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(BoolType, FuId.MathIsNaN, "IsNaN", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Log", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathLog2, "Log2", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Log10", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Log", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathLog2, "Log2", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Log10", FuVar.New(DoubleType, "a"));
mathClass.Add(FuMethodGroup.New(
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, IntType, FuId.MathMaxInt, "Max", false, FuVar.New(LongType, "a"), FuVar.New(LongType, "b")),
FuMethod.New(null, FuVisibility.Public, FuCallType.Static, FloatType, FuId.MathMaxDouble, "Max", false, FuVar.New(DoubleType, "a"), FuVar.New(DoubleType, "b"))));
Expand All @@ -1863,13 +1865,13 @@ public class FuSystem : FuScope
mathClass.Add(FuStaticProperty.New(FloatType, FuId.MathNegativeInfinity, "NegativeInfinity"));
mathClass.Add(NewConstDouble("PI", Math.PI));
mathClass.Add(FuStaticProperty.New(FloatType, FuId.MathPositiveInfinity, "PositiveInfinity"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Pow", FuVar.New(DoubleType, "x"), FuVar.New(DoubleType, "y"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Pow", FuVar.New(DoubleType, "x"), FuVar.New(DoubleType, "y"));
mathClass.AddStaticMethod(floatIntType, FuId.MathRound, "Round", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Sin", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Sinh", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Sqrt", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Tan", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(FloatType, FuId.MathMethod, "Tanh", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Sin", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Sinh", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Sqrt", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Tan", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatingType, FuId.MathMethod, "Tanh", FuVar.New(DoubleType, "a"));
mathClass.AddStaticMethod(floatIntType, FuId.MathTruncate, "Truncate", FuVar.New(DoubleType, "a"));
Add(mathClass);

Expand Down
19 changes: 16 additions & 3 deletions GenBase.fu
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,19 @@ public abstract class GenBase : FuVisitor
WriteCall(method, arg0, arg1);
}

protected void WriteInParentheses!(List<FuExpr#> args)
{
WriteChar('(');
bool first = true;
foreach (FuExpr arg in args) {
if (!first)
Write(", ");
arg.Accept(this, FuPriority.Argument);
first = false;
}
WriteChar(')');
}

protected virtual void WriteSelectValues!(FuType type, FuSelectExpr expr)
{
WriteCoerced(type, expr.OnTrue, FuPriority.Select);
Expand Down Expand Up @@ -720,7 +733,7 @@ public abstract class GenBase : FuVisitor
}
}

protected void WriteArgs!(FuMethod method, List<FuExpr#> args)
protected void WriteCoercedArgs!(FuMethod method, List<FuExpr#> args)
{
FuVar param = method.FirstParameter();
bool first = true;
Expand All @@ -733,10 +746,10 @@ public abstract class GenBase : FuVisitor
}
}

protected void WriteArgsInParentheses!(FuMethod method, List<FuExpr#> args)
protected void WriteCoercedArgsInParentheses!(FuMethod method, List<FuExpr#> args)
{
WriteChar('(');
WriteArgs(method, args);
WriteCoercedArgs(method, args);
WriteChar(')');
}

Expand Down
39 changes: 23 additions & 16 deletions GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,15 @@ public class GenC : GenCCpp
Write(", ");
}

void WriteMathFloating!(string function, List<FuExpr#> args)
{
IncludeMath();
WriteLowercase(function);
if (!args.Any(arg => arg.Type.Id == FuId.DoubleType))
WriteChar('f');
WriteInParentheses(args);
}

protected override void WriteCallExpr!(FuExpr? obj, FuMethod method, List<FuExpr#> args, FuPriority parent)
{
switch (method.Id) {
Expand Down Expand Up @@ -2418,12 +2427,8 @@ public class GenC : GenCCpp
WriteCall("g_match_info_fetch", obj, args[0]);
break;
case FuId.MathMethod:
case FuId.MathIsFinite:
case FuId.MathIsNaN:
case FuId.MathLog2:
IncludeMath();
WriteLowercase(method.Name);
WriteArgsInParentheses(method, args);
WriteMathFloating(method.Name, args);
break;
case FuId.MathAbs:
switch (args[0].Type.Id) {
Expand All @@ -2445,32 +2450,34 @@ public class GenC : GenCCpp
}
break;
case FuId.MathCeiling:
IncludeMath();
WriteCall("ceil", args[0]);
WriteMathFloating("ceil", args);
break;
case FuId.MathFusedMultiplyAdd:
WriteMathFloating("fma", args);
break;
case FuId.MathIsFinite:
IncludeMath();
WriteCall("fma", args[0], args[1], args[2]);
WriteCall("isfinite", args[0]);
break;
case FuId.MathIsInfinity:
IncludeMath();
WriteCall("isinf", args[0]);
break;
case FuId.MathMaxDouble:
case FuId.MathIsNaN:
IncludeMath();
WriteCall("fmax", args[0], args[1]);
WriteCall("isnan", args[0]);
break;
case FuId.MathMaxDouble:
WriteMathFloating("fmax", args);
break;
case FuId.MathMinDouble:
IncludeMath();
WriteCall("fmin", args[0], args[1]);
WriteMathFloating("fmin", args);
break;
case FuId.MathRound:
IncludeMath();
WriteCall("round", args[0]);
WriteMathFloating("round", args);
break;
case FuId.MathTruncate:
IncludeMath();
WriteCall("trunc", args[0]);
WriteMathFloating("trunc", args);
break;
default:
NotSupported(obj, method.Name);
Expand Down
2 changes: 1 addition & 1 deletion GenCl.fu
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public class GenCl : GenC
case FuId.MathMinInt:
case FuId.MathRound:
WriteLowercase(method.Name);
WriteArgsInParentheses(method, args);
WriteInParentheses(args);
break;
case FuId.MathAbs:
if (args[0].Type is FuFloatingType)
Expand Down
6 changes: 3 additions & 3 deletions GenCpp.fu
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ public class GenCpp : GenCCpp
WriteChar(')');
}
else
WriteArgsInParentheses(method, args);
WriteCoercedArgsInParentheses(method, args);
}

void WriteAllAnyContains!(string function, FuExpr obj, List<FuExpr#> args)
Expand Down Expand Up @@ -752,7 +752,7 @@ public class GenCpp : GenCCpp
}
}
WriteName(method);
WriteArgsInParentheses(method, args);
WriteCoercedArgsInParentheses(method, args);
break;
case FuId.EnumFromInt:
WriteStaticCast(method.Type, args[0]);
Expand Down Expand Up @@ -1200,7 +1200,7 @@ public class GenCpp : GenCCpp
IncludeMath();
Write("std::");
WriteLowercase(method.Name);
WriteArgsInParentheses(method, args);
WriteInParentheses(args);
break;
case FuId.MathCeiling:
IncludeMath();
Expand Down
34 changes: 21 additions & 13 deletions GenCs.fu
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public class GenCs : GenTyped
Write("Array.Copy(");
obj.Accept(this, FuPriority.Argument);
Write(", ");
WriteArgs(method, args);
WriteCoercedArgs(method, args);
WriteChar(')');
break;
case FuId.ArrayFillAll:
Expand Down Expand Up @@ -567,7 +567,7 @@ public class GenCs : GenTyped
break;
case FuId.ListSortPart:
WritePostfix(obj, ".Sort(");
WriteArgs(method, args);
WriteCoercedArgs(method, args);
Write(", null)");
break;
case FuId.DictionaryAdd:
Expand Down Expand Up @@ -620,7 +620,7 @@ public class GenCs : GenTyped
obj.Accept(this, FuPriority.Primary);
WriteChar('.');
Write(method.Name);
WriteArgsInParentheses(method, args);
WriteInParentheses(args);
break;
case FuId.UTF8GetByteCount:
Include("System.Text");
Expand All @@ -640,12 +640,12 @@ public class GenCs : GenTyped
case FuId.UTF8GetString:
Include("System.Text");
Write("Encoding.UTF8.GetString");
WriteArgsInParentheses(method, args);
WriteInParentheses(args);
break;
case FuId.RegexCompile:
Include("System.Text.RegularExpressions");
Write("new Regex");
WriteArgsInParentheses(method, args);
WriteInParentheses(args);
break;
case FuId.RegexEscape:
case FuId.RegexIsMatchStr:
Expand All @@ -654,14 +654,14 @@ public class GenCs : GenTyped
obj.Accept(this, FuPriority.Primary);
WriteChar('.');
Write(method.Name);
WriteArgsInParentheses(method, args);
WriteInParentheses(args);
break;
case FuId.MatchFindStr:
Include("System.Text.RegularExpressions");
WriteChar('(');
obj.Accept(this, FuPriority.Assign);
Write(" = Regex.Match");
WriteArgsInParentheses(method, args);
WriteInParentheses(args);
Write(").Success");
break;
case FuId.MatchFindRegex:
Expand Down Expand Up @@ -714,21 +714,29 @@ public class GenCs : GenTyped
WritePostfix(obj, ".EnumerateArray().ToList()");
break;
case FuId.MathMethod:
case FuId.MathAbs:
case FuId.MathCeiling:
case FuId.MathClamp:
case FuId.MathFusedMultiplyAdd:
case FuId.MathLog2:
case FuId.MathRound:
case FuId.MathTruncate:
Include("System");
Write("Math");
if (!args.Any(arg => arg.Type.Id == FuId.DoubleType))
WriteChar('F');
WriteChar('.');
Write(method.Name);
WriteInParentheses(args);
break;
case FuId.MathAbs:
case FuId.MathClamp:
case FuId.MathMaxInt:
case FuId.MathMaxDouble:
case FuId.MathMinInt:
case FuId.MathMinDouble:
case FuId.MathRound:
case FuId.MathTruncate:
Include("System");
Write("Math.");
Write(method.Name);
WriteArgsInParentheses(method, args);
WriteInParentheses(args);
break;
case FuId.MathIsFinite:
case FuId.MathIsInfinity:
Expand All @@ -742,7 +750,7 @@ public class GenCs : GenTyped
WriteChar('.');
}
WriteName(method);
WriteArgsInParentheses(method, args);
WriteCoercedArgsInParentheses(method, args);
break;
}
}
Expand Down
Loading

0 comments on commit 3412e3e

Please sign in to comment.