-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now successfully creating a single precision and double precision ver…
…sion of the library with implicit casting.
- Loading branch information
Showing
18 changed files
with
11,190 additions
and
5,067 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
public static class Intrinsics | ||
{ | ||
public static Number Cos(Angle x) => ({{float}})System.Math.Cos(x.Value); | ||
public static Number Sin(Angle x) => ({{float}})System.Math.Sin(x.Value); | ||
public static Number Tan(Angle x) => ({{float}})System.Math.Tan(x.Value); | ||
|
||
public static Number Ln(Number x) => ({{float}})System.Math.Log(x.Value); | ||
public static Number Exp(Number x) => ({{float}})System.Math.Exp(x.Value); | ||
|
||
public static Angle Acos(Number x) => new Angle(({{float}})System.Math.Acos(x)); | ||
public static Angle Asin(Number x) => new Angle(({{float}})System.Math.Asin(x)); | ||
public static Angle Atan(Number x) => new Angle(({{float}})System.Math.Atan(x)); | ||
|
||
public static Number Pow(Number x, Number y) => ({{float}})System.Math.Pow(x, y); | ||
public static Number Log(Number x, Number y) => ({{float}})System.Math.Log(x, y); | ||
public static Number NaturalLog(Number x) => ({{float}})System.Math.Log(x); | ||
public static Number NaturalPower(Number x) => ({{float}})System.Math.Pow(x, System.Math.E); | ||
|
||
public static Number Add(Number x, Number y) => x.Value + y.Value; | ||
public static Number Subtract(Number x, Number y) => x.Value - y.Value; | ||
public static Number Divide(Number x, Number y) => x.Value / y.Value; | ||
public static Number Multiply(Number x, Number y) => x.Value * y.Value; | ||
public static Number Modulo(Number x, Number y) => x.Value % y.Value; | ||
public static Number Negative(Number x) => -x.Value; | ||
|
||
public static Integer Add(Integer x, Integer y) => x.Value + y.Value; | ||
public static Integer Subtract(Integer x, Integer y) => x.Value - y.Value; | ||
public static Integer Divide(Integer x, Integer y) => x.Value / y.Value; | ||
public static Integer Multiply(Integer x, Integer y) => x.Value * y.Value; | ||
public static Integer Modulo(Integer x, Integer y) => x.Value % y.Value; | ||
public static Integer Negative(Integer x) => -x.Value; | ||
|
||
public static Array<Integer> Range(Integer x) => new RangeStruct(x); | ||
|
||
public static Boolean And(Boolean x, Boolean y) => x.Value && y.Value; | ||
public static Boolean Or(Boolean x, Boolean y) => x.Value || y.Value; | ||
public static Boolean Not(Boolean x) => !x.Value; | ||
|
||
public static Number ToNumber(Integer x) => x.Value; | ||
|
||
public static string MakeString(string typeName, Array<String> fieldNames, Array<Dynamic> fieldValues) | ||
{ | ||
var sb = new System.Text.StringBuilder(); | ||
sb.Append($"{{ _type=\"{typeName}\" "); | ||
for (var i = 0; i < fieldNames.Count; i++) | ||
sb.Append(", ").Append(fieldNames.At(i).Value).Append(" = ").Append(fieldValues.At(i).Value); | ||
sb.Append(" }"); | ||
return sb.ToString(); | ||
} | ||
|
||
public static int CombineHashCodes(params object[] objects) | ||
{ | ||
if (objects.Length == 0) return 0; | ||
var r = objects[0].GetHashCode(); | ||
for (var i = 1; i < objects.Length; ++i) | ||
r = CombineHashCodes(r, objects[i].GetHashCode()); | ||
return r; | ||
} | ||
|
||
public static (T0, T1) Tuple2<T0, T1>(this T0 item0, T1 item1) | ||
=> (item0, item1); | ||
|
||
public static (T0, T1, T2) Tuple3<T0, T1, T2>(this T0 item0, T1 item1, T2 item2) | ||
=> (item0, item1, item2); | ||
|
||
public static (T0, T1, T2, T3) Tuple4<T0, T1, T2, T3>(this T0 item0, T1 item1, T2 item2, T3 item3) | ||
=> (item0, item1, item2, item3); | ||
|
||
public static Array<T> MakeArray<T>(params T[] args) | ||
=> new PrimitiveArray<T>(args); | ||
|
||
public static int CombineHashCodes(int h1, int h2) | ||
{ | ||
unchecked | ||
{ | ||
var rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); | ||
return ((int)rol5 + h1) ^ h2; | ||
} | ||
} | ||
|
||
public static Array<T1> Map<T0, T1>(this Array<T0> self, System.Func<T0, T1> f) | ||
=> new MappedArray<T0, T1>(self, f); | ||
|
||
public static TAcc Reduce<T, TAcc>(this Array<T> self, TAcc init, System.Func<TAcc, T, TAcc> f) | ||
{ | ||
for (var i = 0; i < self.Count; ++i) | ||
init = f(init, self.At(i)); | ||
return init; | ||
} | ||
} | ||
|
||
public readonly struct PrimitiveArray<T> : Array<T> | ||
{ | ||
private readonly T[] _data; | ||
public Integer Count => _data.Length; | ||
public T At(Integer n) => _data[n]; | ||
public PrimitiveArray(T[] data) => _data = data; | ||
public static Array<T> Default = new PrimitiveArray<T>(System.Array.Empty<T>()); | ||
} | ||
|
||
public readonly struct MappedArray<T0, T1> : Array<T1> | ||
{ | ||
public System.Func<T0, T1> MapFunc { get; } | ||
public Array<T0> Original { get; } | ||
public Integer Count => Original.Count; | ||
public T1 At(Integer n) => MapFunc(Original.At(n)); | ||
|
||
public MappedArray(Array<T0> input, System.Func<T0, T1> f) | ||
{ | ||
Original = input; | ||
MapFunc = f; | ||
} | ||
} | ||
|
||
public readonly struct RangeStruct : Array<Integer> | ||
{ | ||
public Integer Count { get; } | ||
public Integer At(Integer n) => n; | ||
public RangeStruct(Integer n) => Count = n; | ||
} | ||
|
||
public readonly partial struct String | ||
{ | ||
public Integer Compare(String other) => Value.CompareTo(other.Value); | ||
public Character At(Integer n) => Value[n]; | ||
public Integer Count => Value.Length; | ||
} | ||
|
||
public readonly partial struct Boolean | ||
{ | ||
public static bool operator true(Boolean b) => b.Value; | ||
public static bool operator false(Boolean b) => !b.Value; | ||
} | ||
|
||
public readonly partial struct Number | ||
{ | ||
public Number Zero => 0; | ||
public Number One => 1; | ||
public Number MinValue => {{float}}.MinValue; | ||
public Number MaxValue => {{float}}.MaxValue; | ||
public Integer Compare(Number other) => Value.CompareTo(other.Value); | ||
public Number Unlerp(Number a, Number b) => ({{float}})(this - a) / ({{float}})(b - a); | ||
} | ||
|
||
public readonly partial struct Integer | ||
{ | ||
public Integer Zero => 0; | ||
public Integer One => 1; | ||
public Integer MinValue => int.MinValue; | ||
public Integer MaxValue => int.MaxValue; | ||
public Number Magnitude => Value; | ||
public static implicit operator Number(Integer self) => self.Value; | ||
public Integer Compare(Integer other) => Value.CompareTo(other.Value); | ||
public Integer Lerp(Integer b, Number t) => (int)(Value * (1.0 - t) + b * t); | ||
public Number Unlerp(Integer a, Integer b) => ({{float}})(this - a) / ({{float}})(b - a); | ||
} | ||
|
||
public readonly partial struct Character | ||
{ | ||
public Character Zero => (char)0; | ||
public Character One => (char)1; | ||
public Character MinValue => char.MinValue; | ||
public Character MaxValue => char.MaxValue; | ||
public Number Magnitude => Value; | ||
public static implicit operator Number(Character self) => self.Value; | ||
public Integer Compare(Character other) => Value.CompareTo(other.Value); | ||
public Number Unlerp(Character a, Character b) => ({{float}})(this - a) / ({{float}})(b - a); | ||
public Boolean Equals(Character x) => Value.Equals(x.Value); | ||
public Boolean NotEquals(Character x) => !Equals(x); | ||
} | ||
|
||
public readonly partial struct Dynamic | ||
{ | ||
public readonly object Value; | ||
public Dynamic WithValue(object value) => new Dynamic(value); | ||
public Dynamic(object value) => (Value) = (value); | ||
public static Dynamic Default = new Dynamic(); | ||
public static Dynamic New(object value) => new Dynamic(value); | ||
public String TypeName => "Dynamic"; | ||
public Array<String> FieldNames => Intrinsics.MakeArray<String>("Value"); | ||
public Array<Dynamic> FieldValues => Intrinsics.MakeArray<Dynamic>(new Dynamic(Value)); | ||
public T As<T>() => (T)Value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.