Skip to content
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
38 changes: 38 additions & 0 deletions src/Lua/Internal/MathEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,42 @@ public static int NewArrayCapacity(int size)

return newSize;
}

const long DBL_EXP_MASK = 0x7ff0000000000000L;
const int DBL_MANT_BITS = 52;
const long DBL_SGN_MASK = -1 - 0x7fffffffffffffffL;
const long DBL_MANT_MASK = 0x000fffffffffffffL;
const long DBL_EXP_CLR_MASK = DBL_SGN_MASK | DBL_MANT_MASK;

public static (double m, int e) Frexp(double d)
{
var bits = BitConverter.DoubleToInt64Bits(d);
var exp = (int)((bits & DBL_EXP_MASK) >> DBL_MANT_BITS);
var e = 0;

if (exp == 0x7ff || d == 0D)
d += d;
else
{
// Not zero and finite.
e = exp - 1022;
if (exp == 0)
{
// Subnormal, scale d so that it is in [1, 2).
d *= BitConverter.Int64BitsToDouble(0x4350000000000000L); // 2^54
bits = BitConverter.DoubleToInt64Bits(d);
exp = (int)((bits & DBL_EXP_MASK) >> DBL_MANT_BITS);
e = exp - 1022 - 54;
}
// Set exponent to -1 so that d is in [0.5, 1).
d = BitConverter.Int64BitsToDouble((bits & DBL_EXP_CLR_MASK) | 0x3fe0000000000000L);
}

return (d, e);
}

public static (int i, double f) Modf(double d)
{
return ((int)Math.Truncate(d), d % 1.0);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/AbsFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class AbsFunction : LuaFunction
{
public static readonly AbsFunction Instance = new();

public override string Name => "abs";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Abs(arg0);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/AcosFuncion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class AcosFunction : LuaFunction
{
public static readonly AcosFunction Instance = new();

public override string Name => "acos";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Acos(arg0);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/AsinFuncion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class AsinFunction : LuaFunction
{
public static readonly AsinFunction Instance = new();

public override string Name => "asin";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Asin(arg0);
return new(1);
}
}
18 changes: 18 additions & 0 deletions src/Lua/Standard/Mathematics/Atan2Function.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

namespace Lua.Standard.Mathematics;

public sealed class Atan2Function : LuaFunction
{
public static readonly Atan2Function Instance = new();

public override string Name => "atan2";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
var arg1 = context.ReadArgument<double>(1);

buffer.Span[0] = Math.Atan2(arg0, arg1);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/AtanFuncion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class AtanFunction : LuaFunction
{
public static readonly AtanFunction Instance = new();

public override string Name => "atan";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Atan(arg0);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/CeilFuncion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class CeilFunction : LuaFunction
{
public static readonly CeilFunction Instance = new();

public override string Name => "ceil";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Ceiling(arg0);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/CosFuncion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class CosFunction : LuaFunction
{
public static readonly CosFunction Instance = new();

public override string Name => "cos";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Cos(arg0);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/CoshFuncion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class CoshFunction : LuaFunction
{
public static readonly CoshFunction Instance = new();

public override string Name => "cosh";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Cosh(arg0);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/DegFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class DegFunction : LuaFunction
{
public static readonly DegFunction Instance = new();

public override string Name => "deg";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = arg0 * (180.0 / Math.PI);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/ExpFuncion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class ExpFunction : LuaFunction
{
public static readonly ExpFunction Instance = new();

public override string Name => "exp";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Exp(arg0);
return new(1);
}
}
16 changes: 16 additions & 0 deletions src/Lua/Standard/Mathematics/FloorFuncion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace Lua.Standard.Mathematics;

public sealed class FloorFunction : LuaFunction
{
public static readonly FloorFunction Instance = new();

public override string Name => "floor";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
buffer.Span[0] = Math.Floor(arg0);
return new(1);
}
}
17 changes: 17 additions & 0 deletions src/Lua/Standard/Mathematics/FmodFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

namespace Lua.Standard.Mathematics;

public sealed class FmodFunction : LuaFunction
{
public static readonly FmodFunction Instance = new();

public override string Name => "fmod";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
var arg1 = context.ReadArgument<double>(1);
buffer.Span[0] = arg0 % arg1;
return new(1);
}
}
19 changes: 19 additions & 0 deletions src/Lua/Standard/Mathematics/FrexpFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

namespace Lua.Standard.Mathematics;

public sealed class FrexpFunction : LuaFunction
{
public static readonly FrexpFunction Instance = new();

public override string Name => "frexp";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);

var (m, e) = MathEx.Frexp(arg0);
buffer.Span[0] = m;
buffer.Span[1] = e;
return new(2);
}
}
18 changes: 18 additions & 0 deletions src/Lua/Standard/Mathematics/LdexpFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

namespace Lua.Standard.Mathematics;

public sealed class LdexpFunction : LuaFunction
{
public static readonly LdexpFunction Instance = new();

public override string Name => "ldexp";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
var arg1 = context.ReadArgument<double>(1);

buffer.Span[0] = arg0 * Math.Pow(2, arg1);
return new(1);
}
}
26 changes: 26 additions & 0 deletions src/Lua/Standard/Mathematics/LogFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

namespace Lua.Standard.Mathematics;

public sealed class LogFunction : LuaFunction
{
public static readonly LogFunction Instance = new();

public override string Name => "log";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);

if (context.ArgumentCount == 1)
{
buffer.Span[0] = Math.Log(arg0);
}
else
{
var arg1 = context.ReadArgument<double>(1);
buffer.Span[0] = Math.Log(arg0, arg1);
}

return new(1);
}
}
22 changes: 22 additions & 0 deletions src/Lua/Standard/Mathematics/MaxFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

namespace Lua.Standard.Mathematics;

public sealed class MaxFunction : LuaFunction
{
public static readonly MaxFunction Instance = new();

public override string Name => "max";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var x = context.ReadArgument<double>(0);
for (int i = 1; i < context.ArgumentCount; i++)
{
x = Math.Max(x, context.ReadArgument<double>(i));
}

buffer.Span[0] = x;

return new(1);
}
}
22 changes: 22 additions & 0 deletions src/Lua/Standard/Mathematics/MinFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

namespace Lua.Standard.Mathematics;

public sealed class MinFunction : LuaFunction
{
public static readonly MinFunction Instance = new();

public override string Name => "min";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var x = context.ReadArgument<double>(0);
for (int i = 1; i < context.ArgumentCount; i++)
{
x = Math.Min(x, context.ReadArgument<double>(i));
}

buffer.Span[0] = x;

return new(1);
}
}
18 changes: 18 additions & 0 deletions src/Lua/Standard/Mathematics/ModfFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

namespace Lua.Standard.Mathematics;

public sealed class ModfFunction : LuaFunction
{
public static readonly ModfFunction Instance = new();

public override string Name => "modf";

protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
{
var arg0 = context.ReadArgument<double>(0);
var (i, f) = MathEx.Modf(arg0);
buffer.Span[0] = i;
buffer.Span[1] = f;
return new(2);
}
}
Loading