Update math_util.c and math_util.h to support autodoc with trig functions (inline instead of macros) and add new functions to smlua_math_utils (#359)

* Update math_util.c and math_util.h to move toward inlining functions instead of macros for autodoc. Expose some misc functions useful to lua

* Fix formatting

* Fix math_util.c formatting

* Fix formatting for smlua_math_utils.c

* Fix formatting smlua_functions.c

* Fix type redundancy in _Generic macros

* Add checks for including the highly optimized builtin compiler functions for GCC/Clang

* Add compiler checking for absx() to add in the highly optimized GCC/Clang builtins

* Fix repeated use of float built-ins for non floating point numbers

* Fix grammar mistake

* Fix functions to use camelCase as requested.

* Fixed the use of a custom sqrt approximation as modern procs have a built in FSQRT instruction that is faster.

---------

Co-authored-by: js <js@cartbara.columbus.rr.com>
This commit is contained in:
John S 2024-10-17 22:20:36 -04:00 committed by GitHub
parent 9621424069
commit 3dd9226bc9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 625 additions and 273 deletions

View file

@ -88,7 +88,7 @@ override_allowed_functions = {
override_disallowed_functions = {
"src/audio/external.h": [ " func_" ],
"src/engine/math_util.h": [ "atan2s", "atan2f", "vec3s_sub" ],
"src/engine/math_util.h": [ "atan2f", "vec3s_sub" ],
"src/engine/surface_load.h": [ "alloc_surface_poools" ],
"src/engine/surface_collision.h": [ " debug_", "f32_find_wall_collision" ],
"src/game/mario_actions_airborne.c": [ "^[us]32 act_.*" ],

View file

@ -5024,6 +5024,19 @@ function approach_s32(current, target, inc, dec)
-- ...
end
--- @param y number
--- @param x number
--- @return integer
function atan2s(y, x)
-- ...
end
--- @param sm64Angle integer
--- @return number
function coss(sm64Angle)
-- ...
end
--- @param dest Vec3f
--- @param a Vec3f
--- @param b Vec3f
@ -5040,6 +5053,34 @@ function get_pos_from_transform_mtx(dest, objMtx, camMtx)
-- ...
end
--- @param a integer
--- @param b integer
--- @return integer
function max(a, b)
-- ...
end
--- @param a number
--- @param b number
--- @return number
function maxf(a, b)
-- ...
end
--- @param a integer
--- @param b integer
--- @return integer
function min(a, b)
-- ...
end
--- @param a number
--- @param b number
--- @return number
function minf(a, b)
-- ...
end
--- @param dest Mat4
--- @param upDir Vec3f
--- @param pos Vec3f
@ -5156,6 +5197,12 @@ function not_zero(value, replacement)
-- ...
end
--- @param sm64Angle integer
--- @return number
function sins(sm64Angle)
-- ...
end
--- @param m MarioState
--- @param result Vec4f
--- @param t number
@ -5164,6 +5211,18 @@ function spline_get_weights(m, result, t, c)
-- ...
end
--- @param x integer
--- @return integer
function sqr(x)
-- ...
end
--- @param x number
--- @return number
function sqrf(x)
-- ...
end
--- @param dest Vec3f
--- @param a Vec3f
--- @return void*
@ -8133,43 +8192,34 @@ function clampf(a, b, c)
-- ...
end
--- @param a integer
--- @param b integer
--- @param degreesAngle number
--- @return integer
function max(a, b)
function degrees_to_sm64(degreesAngle)
-- ...
end
--- @param a number
--- @param b number
--- @return number
function maxf(a, b)
function fast_hypot(a, b)
-- ...
end
--- @param a integer
--- @param b integer
--- @param radiansAngle number
--- @return integer
function min(a, b)
function radians_to_sm64(radiansAngle)
-- ...
end
--- @param a number
--- @param b number
--- @param sm64Angle integer
--- @return number
function minf(a, b)
function sm64_to_degrees(sm64Angle)
-- ...
end
--- @param x integer
--- @return integer
function sqr(x)
-- ...
end
--- @param x number
--- @param sm64Angle integer
--- @return number
function sqrf(x)
function sm64_to_radians(sm64Angle)
-- ...
end

View file

@ -196,25 +196,6 @@ end
-- functions --
---------------
--- @param t number Angle
--- @return number
function sins(t)
-- ...
end
--- @param t number Angle
--- @return number
function coss(t)
-- ...
end
--- @param y number
--- @param x number
--- @return integer
function atan2s(y, x)
-- ...
end
--- @param objFieldTable table<any, "u32"|"s32"|"f32">
--- Keys must start with `o` and values must be `"u32"`, `"s32"`, or `"f32"`
function define_custom_obj_fields(objFieldTable)

View file

@ -99,6 +99,47 @@
<br />
## [atan2s](#atan2s)
### Lua Example
`local integerValue = atan2s(y, x)`
### Parameters
| Field | Type |
| ----- | ---- |
| y | `number` |
| x | `number` |
### Returns
- `integer`
### C Prototype
`s16 atan2s(f32 y, f32 x);`
[:arrow_up_small:](#)
<br />
## [coss](#coss)
### Lua Example
`local numberValue = coss(sm64Angle)`
### Parameters
| Field | Type |
| ----- | ---- |
| sm64Angle | `integer` |
### Returns
- `number`
### C Prototype
`f32 coss(s16 sm64Angle);`
[:arrow_up_small:](#)
<br />
## [find_vector_perpendicular_to_plane](#find_vector_perpendicular_to_plane)
### Lua Example
@ -144,6 +185,90 @@
<br />
## [max](#max)
### Lua Example
`local integerValue = max(a, b)`
### Parameters
| Field | Type |
| ----- | ---- |
| a | `integer` |
| b | `integer` |
### Returns
- `integer`
### C Prototype
`s16 max(s16 a, s16 b);`
[:arrow_up_small:](#)
<br />
## [maxf](#maxf)
### Lua Example
`local numberValue = maxf(a, b)`
### Parameters
| Field | Type |
| ----- | ---- |
| a | `number` |
| b | `number` |
### Returns
- `number`
### C Prototype
`f32 maxf(f32 a, f32 b);`
[:arrow_up_small:](#)
<br />
## [min](#min)
### Lua Example
`local integerValue = min(a, b)`
### Parameters
| Field | Type |
| ----- | ---- |
| a | `integer` |
| b | `integer` |
### Returns
- `integer`
### C Prototype
`s16 min(s16 a, s16 b);`
[:arrow_up_small:](#)
<br />
## [minf](#minf)
### Lua Example
`local numberValue = minf(a, b)`
### Parameters
| Field | Type |
| ----- | ---- |
| a | `number` |
| b | `number` |
### Returns
- `number`
### C Prototype
`f32 minf(f32 a, f32 b);`
[:arrow_up_small:](#)
<br />
## [mtxf_align_terrain_normal](#mtxf_align_terrain_normal)
### Lua Example
@ -514,6 +639,26 @@
<br />
## [sins](#sins)
### Lua Example
`local numberValue = sins(sm64Angle)`
### Parameters
| Field | Type |
| ----- | ---- |
| sm64Angle | `integer` |
### Returns
- `number`
### C Prototype
`f32 sins(s16 sm64Angle);`
[:arrow_up_small:](#)
<br />
## [spline_get_weights](#spline_get_weights)
### Lua Example
@ -537,6 +682,46 @@
<br />
## [sqr](#sqr)
### Lua Example
`local integerValue = sqr(x)`
### Parameters
| Field | Type |
| ----- | ---- |
| x | `integer` |
### Returns
- `integer`
### C Prototype
`s16 sqr(s16 x);`
[:arrow_up_small:](#)
<br />
## [sqrf](#sqrf)
### Lua Example
`local numberValue = sqrf(x)`
### Parameters
| Field | Type |
| ----- | ---- |
| x | `number` |
### Returns
- `number`
### C Prototype
`f32 sqrf(f32 x);`
[:arrow_up_small:](#)
<br />
## [vec3f_add](#vec3f_add)
### Lua Example

View file

@ -1801,31 +1801,30 @@
<br />
## [max](#max)
## [degrees_to_sm64](#degrees_to_sm64)
### Lua Example
`local integerValue = max(a, b)`
`local integerValue = degrees_to_sm64(degreesAngle)`
### Parameters
| Field | Type |
| ----- | ---- |
| a | `integer` |
| b | `integer` |
| degreesAngle | `number` |
### Returns
- `integer`
### C Prototype
`s32 max(s32 a, s32 b);`
`s16 degrees_to_sm64(f32 degreesAngle);`
[:arrow_up_small:](#)
<br />
## [maxf](#maxf)
## [fast_hypot](#fast_hypot)
### Lua Example
`local numberValue = maxf(a, b)`
`local numberValue = fast_hypot(a, b)`
### Parameters
| Field | Type |
@ -1837,89 +1836,67 @@
- `number`
### C Prototype
`f32 maxf(f32 a, f32 b);`
`f32 fast_hypot(f32 a, f32 b);`
[:arrow_up_small:](#)
<br />
## [min](#min)
## [radians_to_sm64](#radians_to_sm64)
### Lua Example
`local integerValue = min(a, b)`
`local integerValue = radians_to_sm64(radiansAngle)`
### Parameters
| Field | Type |
| ----- | ---- |
| a | `integer` |
| b | `integer` |
| radiansAngle | `number` |
### Returns
- `integer`
### C Prototype
`s32 min(s32 a, s32 b);`
`s16 radians_to_sm64(f32 radiansAngle);`
[:arrow_up_small:](#)
<br />
## [minf](#minf)
## [sm64_to_degrees](#sm64_to_degrees)
### Lua Example
`local numberValue = minf(a, b)`
`local numberValue = sm64_to_degrees(sm64Angle)`
### Parameters
| Field | Type |
| ----- | ---- |
| a | `number` |
| b | `number` |
| sm64Angle | `integer` |
### Returns
- `number`
### C Prototype
`f32 minf(f32 a, f32 b);`
`f32 sm64_to_degrees(s16 sm64Angle);`
[:arrow_up_small:](#)
<br />
## [sqr](#sqr)
## [sm64_to_radians](#sm64_to_radians)
### Lua Example
`local integerValue = sqr(x)`
`local numberValue = sm64_to_radians(sm64Angle)`
### Parameters
| Field | Type |
| ----- | ---- |
| x | `integer` |
### Returns
- `integer`
### C Prototype
`s32 sqr(s32 x);`
[:arrow_up_small:](#)
<br />
## [sqrf](#sqrf)
### Lua Example
`local numberValue = sqrf(x)`
### Parameters
| Field | Type |
| ----- | ---- |
| x | `number` |
| sm64Angle | `integer` |
### Returns
- `number`
### C Prototype
`f32 sqrf(f32 x);`
`f32 sm64_to_radians(s16 sm64Angle);`
[:arrow_up_small:](#)

View file

@ -1134,8 +1134,14 @@
- [anim_spline_poll](functions-4.md#anim_spline_poll)
- [approach_f32](functions-4.md#approach_f32)
- [approach_s32](functions-4.md#approach_s32)
- [atan2s](functions-4.md#atan2s)
- [coss](functions-4.md#coss)
- [find_vector_perpendicular_to_plane](functions-4.md#find_vector_perpendicular_to_plane)
- [get_pos_from_transform_mtx](functions-4.md#get_pos_from_transform_mtx)
- [max](functions-4.md#max)
- [maxf](functions-4.md#maxf)
- [min](functions-4.md#min)
- [minf](functions-4.md#minf)
- [mtxf_align_terrain_normal](functions-4.md#mtxf_align_terrain_normal)
- [mtxf_align_terrain_triangle](functions-4.md#mtxf_align_terrain_triangle)
- [mtxf_billboard](functions-4.md#mtxf_billboard)
@ -1153,7 +1159,10 @@
- [mtxf_to_mtx](functions-4.md#mtxf_to_mtx)
- [mtxf_translate](functions-4.md#mtxf_translate)
- [not_zero](functions-4.md#not_zero)
- [sins](functions-4.md#sins)
- [spline_get_weights](functions-4.md#spline_get_weights)
- [sqr](functions-4.md#sqr)
- [sqrf](functions-4.md#sqrf)
- [vec3f_add](functions-4.md#vec3f_add)
- [vec3f_combine](functions-4.md#vec3f_combine)
- [vec3f_copy](functions-4.md#vec3f_copy)
@ -1708,12 +1717,11 @@
- smlua_math_utils.h
- [clamp](functions-5.md#clamp)
- [clampf](functions-5.md#clampf)
- [max](functions-5.md#max)
- [maxf](functions-5.md#maxf)
- [min](functions-5.md#min)
- [minf](functions-5.md#minf)
- [sqr](functions-5.md#sqr)
- [sqrf](functions-5.md#sqrf)
- [degrees_to_sm64](functions-5.md#degrees_to_sm64)
- [fast_hypot](functions-5.md#fast_hypot)
- [radians_to_sm64](functions-5.md#radians_to_sm64)
- [sm64_to_degrees](functions-5.md#sm64_to_degrees)
- [sm64_to_radians](functions-5.md#sm64_to_radians)
<br />

View file

@ -12,6 +12,55 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-local-addr"
#if defined(__clang__) || defined(__GNUC__)
// Use built-in functions when using Clang or GCC
inline f32 minf(f32 a, f32 b) {
return __builtin_fminf(a, b);
}
inline f32 maxf(f32 a, f32 b) {
return __builtin_fmaxf(a, b);
}
#else
// Fallback to the original implementation for iDO
inline f32 minf(f32 a, f32 b) {
return (a <= b) ? a : b;
}
inline f32 maxf(f32 a, f32 b) {
return (a > b) ? a : b;
}
#endif
// The sqr, min, max, and trig functions do not have/need built-ins, so it's safe to leave them as is
inline s16 (min)(s16 a, s16 b) {
return (a <= b) ? a : b;
}
inline s16 (max)(s16 a, s16 b) {
return (a > b) ? a : b;
}
inline f32 sqrf(f32 x) {
return x * x;
}
inline s16 (sqr)(s16 x) {
return x * x;
}
inline f32 sins(s16 sm64Angle) {
return gSineTable[(u16) (sm64Angle) >> 4];
}
inline f32 coss(s16 sm64Angle) {
return gCosineTable[(u16) (sm64Angle) >> 4];
}
/// Copy vector 'src' to 'dest'
void *vec3f_copy(Vec3f dest, Vec3f src) {
dest[0] = src[0];

View file

@ -24,24 +24,44 @@ extern f32 gSineTable[];
extern f32 gCosineTable[];
#endif
#define sins(x) gSineTable[(u16) (x) >> 4]
#define coss(x) gCosineTable[(u16) (x) >> 4]
// Inline Function prototypes
f32 minf(f32 a, f32 b);
s16 min(s16 a, s16 b);
f32 maxf(f32 a, f32 b);
s16 max(s16 a, s16 b);
f32 sqrf(f32 x);
s16 sqr(s16 x);
f32 sins(s16 sm64Angle);
f32 coss(s16 sm64Angle);
#if defined(min)
#undef min
#endif
#define min(a, b) _Generic((a), \
f32: minf, \
default: min \
)(a, b)
#if defined(max)
#undef max
#endif
#define max(a, b) _Generic((a), \
f32: maxf, \
default: max \
)(a, b)
#define min(a, b) ((a) <= (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define sqr(x) _Generic((x), \
f32: sqrf, \
default: sqr \
)(x)
#define sqr(x) ((x) * (x))
#if defined(__clang__) || defined(__GNUC__)
#define absx(x) _Generic((x), \
f32: __builtin_fabsf(x), \
default: __builtin_abs(x) \
)
#else
#define absx(x) ((x) < 0 ? -(x) : (x))
#endif
#include "../../include/libc/stdlib.h"
void *vec3f_copy(Vec3f dest, Vec3f src);

View file

@ -38,38 +38,6 @@ bool smlua_functions_valid_param_range(lua_State* L, int min, int max) {
// misc //
//////////
int smlua_func_sins(lua_State* L) {
if (!smlua_functions_valid_param_count(L, 1)) { return 0; }
s16 x = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("sins: Failed to convert parameter 1"); return 0; }
lua_pushnumber(L, sins(x));
return 1;
}
int smlua_func_coss(lua_State* L) {
if (!smlua_functions_valid_param_count(L, 1)) { return 0; }
s16 x = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("coss: Failed to convert parameter 1"); return 0; }
lua_pushnumber(L, coss(x));
return 1;
}
int smlua_func_atan2s(lua_State* L) {
if (!smlua_functions_valid_param_count(L, 2)) { return 0; }
f32 y = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("atan2s: Failed to convert parameter 1"); return 0; }
f32 x = smlua_to_number(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("atan2s: Failed to convert parameter 2"); return 0; }
lua_pushinteger(L, atan2s(y, x));
return 1;
}
int smlua_func_init_mario_after_warp(lua_State* L) {
if (network_player_connected_count() >= 2) {
LOG_LUA_LINE("init_mario_after_warp() can only be used in singleplayer");
@ -880,9 +848,6 @@ void smlua_bind_functions(void) {
lua_State* L = gLuaState;
// misc
smlua_bind_function(L, "sins", smlua_func_sins);
smlua_bind_function(L, "coss", smlua_func_coss);
smlua_bind_function(L, "atan2s", smlua_func_atan2s);
smlua_bind_function(L, "init_mario_after_warp", smlua_func_init_mario_after_warp);
smlua_bind_function(L, "initiate_warp", smlua_func_initiate_warp);
smlua_bind_function(L, "network_init_object", smlua_func_network_init_object);

View file

@ -18362,6 +18362,42 @@ int smlua_func_approach_s32(lua_State* L) {
return 1;
}
int smlua_func_atan2s(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "atan2s", 2, top);
return 0;
}
f32 y = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "atan2s"); return 0; }
f32 x = smlua_to_number(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "atan2s"); return 0; }
lua_pushinteger(L, atan2s(y, x));
return 1;
}
int smlua_func_coss(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "coss", 1, top);
return 0;
}
s16 sm64Angle = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "coss"); return 0; }
lua_pushnumber(L, coss(sm64Angle));
return 1;
}
int smlua_func_find_vector_perpendicular_to_plane(lua_State* L) {
if (L == NULL) { return 0; }
@ -18514,6 +18550,82 @@ int smlua_func_get_pos_from_transform_mtx(lua_State* L) {
return 1;
}
int smlua_func_max(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "max", 2, top);
return 0;
}
s16 a = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "max"); return 0; }
s16 b = smlua_to_integer(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "max"); return 0; }
lua_pushinteger(L, max(a, b));
return 1;
}
int smlua_func_maxf(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "maxf", 2, top);
return 0;
}
f32 a = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "maxf"); return 0; }
f32 b = smlua_to_number(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "maxf"); return 0; }
lua_pushnumber(L, maxf(a, b));
return 1;
}
int smlua_func_min(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "min", 2, top);
return 0;
}
s16 a = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "min"); return 0; }
s16 b = smlua_to_integer(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "min"); return 0; }
lua_pushinteger(L, min(a, b));
return 1;
}
int smlua_func_minf(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "minf", 2, top);
return 0;
}
f32 a = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "minf"); return 0; }
f32 b = smlua_to_number(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "minf"); return 0; }
lua_pushnumber(L, minf(a, b));
return 1;
}
int smlua_func_mtxf_align_terrain_normal(lua_State* L) {
if (L == NULL) { return 0; }
@ -19723,6 +19835,23 @@ int smlua_func_not_zero(lua_State* L) {
return 1;
}
int smlua_func_sins(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "sins", 1, top);
return 0;
}
s16 sm64Angle = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "sins"); return 0; }
lua_pushnumber(L, sins(sm64Angle));
return 1;
}
int smlua_func_spline_get_weights(lua_State* L) {
if (L == NULL) { return 0; }
@ -19756,6 +19885,40 @@ int smlua_func_spline_get_weights(lua_State* L) {
return 1;
}
int smlua_func_sqr(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "sqr", 1, top);
return 0;
}
s16 x = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "sqr"); return 0; }
lua_pushinteger(L, sqr(x));
return 1;
}
int smlua_func_sqrf(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "sqrf", 1, top);
return 0;
}
f32 x = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "sqrf"); return 0; }
lua_pushnumber(L, sqrf(x));
return 1;
}
int smlua_func_vec3f_add(lua_State* L) {
if (L == NULL) { return 0; }
@ -30019,112 +30182,89 @@ int smlua_func_clampf(lua_State* L) {
return 1;
}
int smlua_func_max(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "max", 2, top);
return 0;
}
s32 a = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "max"); return 0; }
s32 b = smlua_to_integer(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "max"); return 0; }
lua_pushinteger(L, max(a, b));
return 1;
}
int smlua_func_maxf(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "maxf", 2, top);
return 0;
}
f32 a = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "maxf"); return 0; }
f32 b = smlua_to_number(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "maxf"); return 0; }
lua_pushnumber(L, maxf(a, b));
return 1;
}
int smlua_func_min(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "min", 2, top);
return 0;
}
s32 a = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "min"); return 0; }
s32 b = smlua_to_integer(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "min"); return 0; }
lua_pushinteger(L, min(a, b));
return 1;
}
int smlua_func_minf(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "minf", 2, top);
return 0;
}
f32 a = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "minf"); return 0; }
f32 b = smlua_to_number(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "minf"); return 0; }
lua_pushnumber(L, minf(a, b));
return 1;
}
int smlua_func_sqr(lua_State* L) {
int smlua_func_degrees_to_sm64(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "sqr", 1, top);
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "degrees_to_sm64", 1, top);
return 0;
}
s32 x = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "sqr"); return 0; }
f32 degreesAngle = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "degrees_to_sm64"); return 0; }
lua_pushinteger(L, sqr(x));
lua_pushinteger(L, degrees_to_sm64(degreesAngle));
return 1;
}
int smlua_func_sqrf(lua_State* L) {
int smlua_func_fast_hypot(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 2) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "fast_hypot", 2, top);
return 0;
}
f32 a = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "fast_hypot"); return 0; }
f32 b = smlua_to_number(L, 2);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 2, "fast_hypot"); return 0; }
lua_pushnumber(L, fast_hypot(a, b));
return 1;
}
int smlua_func_radians_to_sm64(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "sqrf", 1, top);
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "radians_to_sm64", 1, top);
return 0;
}
f32 x = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "sqrf"); return 0; }
f32 radiansAngle = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "radians_to_sm64"); return 0; }
lua_pushnumber(L, sqrf(x));
lua_pushinteger(L, radians_to_sm64(radiansAngle));
return 1;
}
int smlua_func_sm64_to_degrees(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "sm64_to_degrees", 1, top);
return 0;
}
s16 sm64Angle = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "sm64_to_degrees"); return 0; }
lua_pushnumber(L, sm64_to_degrees(sm64Angle));
return 1;
}
int smlua_func_sm64_to_radians(lua_State* L) {
if (L == NULL) { return 0; }
int top = lua_gettop(L);
if (top != 1) {
LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "sm64_to_radians", 1, top);
return 0;
}
s16 sm64Angle = smlua_to_integer(L, 1);
if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "sm64_to_radians"); return 0; }
lua_pushnumber(L, sm64_to_radians(sm64Angle));
return 1;
}
@ -34168,8 +34308,14 @@ void smlua_bind_functions_autogen(void) {
smlua_bind_function(L, "anim_spline_poll", smlua_func_anim_spline_poll);
smlua_bind_function(L, "approach_f32", smlua_func_approach_f32);
smlua_bind_function(L, "approach_s32", smlua_func_approach_s32);
smlua_bind_function(L, "atan2s", smlua_func_atan2s);
smlua_bind_function(L, "coss", smlua_func_coss);
smlua_bind_function(L, "find_vector_perpendicular_to_plane", smlua_func_find_vector_perpendicular_to_plane);
smlua_bind_function(L, "get_pos_from_transform_mtx", smlua_func_get_pos_from_transform_mtx);
smlua_bind_function(L, "max", smlua_func_max);
smlua_bind_function(L, "maxf", smlua_func_maxf);
smlua_bind_function(L, "min", smlua_func_min);
smlua_bind_function(L, "minf", smlua_func_minf);
smlua_bind_function(L, "mtxf_align_terrain_normal", smlua_func_mtxf_align_terrain_normal);
smlua_bind_function(L, "mtxf_align_terrain_triangle", smlua_func_mtxf_align_terrain_triangle);
smlua_bind_function(L, "mtxf_billboard", smlua_func_mtxf_billboard);
@ -34187,7 +34333,10 @@ void smlua_bind_functions_autogen(void) {
smlua_bind_function(L, "mtxf_to_mtx", smlua_func_mtxf_to_mtx);
smlua_bind_function(L, "mtxf_translate", smlua_func_mtxf_translate);
smlua_bind_function(L, "not_zero", smlua_func_not_zero);
smlua_bind_function(L, "sins", smlua_func_sins);
smlua_bind_function(L, "spline_get_weights", smlua_func_spline_get_weights);
smlua_bind_function(L, "sqr", smlua_func_sqr);
smlua_bind_function(L, "sqrf", smlua_func_sqrf);
smlua_bind_function(L, "vec3f_add", smlua_func_vec3f_add);
smlua_bind_function(L, "vec3f_combine", smlua_func_vec3f_combine);
smlua_bind_function(L, "vec3f_copy", smlua_func_vec3f_copy);
@ -34723,12 +34872,11 @@ void smlua_bind_functions_autogen(void) {
// smlua_math_utils.h
smlua_bind_function(L, "clamp", smlua_func_clamp);
smlua_bind_function(L, "clampf", smlua_func_clampf);
smlua_bind_function(L, "max", smlua_func_max);
smlua_bind_function(L, "maxf", smlua_func_maxf);
smlua_bind_function(L, "min", smlua_func_min);
smlua_bind_function(L, "minf", smlua_func_minf);
smlua_bind_function(L, "sqr", smlua_func_sqr);
smlua_bind_function(L, "sqrf", smlua_func_sqrf);
smlua_bind_function(L, "degrees_to_sm64", smlua_func_degrees_to_sm64);
smlua_bind_function(L, "fast_hypot", smlua_func_fast_hypot);
smlua_bind_function(L, "radians_to_sm64", smlua_func_radians_to_sm64);
smlua_bind_function(L, "sm64_to_degrees", smlua_func_sm64_to_degrees);
smlua_bind_function(L, "sm64_to_radians", smlua_func_sm64_to_radians);
// smlua_misc_utils.h
smlua_bind_function(L, "allocate_mario_action", smlua_func_allocate_mario_action);

View file

@ -1,28 +1,24 @@
#include "sm64.h"
#include "types.h"
s32 min(s32 a, s32 b) {
return ((a) <= (b) ? (a) : (b));
f32 sm64_to_radians(s16 sm64Angle) {
return sm64Angle * M_PI / 0x8000;
}
f32 minf(f32 a, f32 b) {
return ((a) <= (b) ? (a) : (b));
s16 radians_to_sm64(f32 radiansAngle) {
return radiansAngle * 0x8000 / M_PI;
}
s32 max(s32 a, s32 b) {
return ((a) > (b) ? (a) : (b));
f32 sm64_to_degrees(s16 sm64Angle) {
return sm64Angle * 180.0f / 0x8000;
}
f32 maxf(f32 a, f32 b) {
return ((a) > (b) ? (a) : (b));
s16 degrees_to_sm64(f32 degreesAngle) {
return degreesAngle * 0x8000 / 180.0f;
}
s32 sqr(s32 x) {
return x * x;
}
f32 sqrf(f32 x) {
return x * x;
f32 hypotf(f32 a, f32 b) {
return sqrtf(a * a + b * b);
}
s32 clamp(s32 a, s32 b, s32 c) {

View file

@ -1,28 +1,13 @@
#ifndef SMLUA_MATH_UTILS_H
#define SMLUA_MATH_UTILS_H
#if defined(min)
#undef min
#endif
#if defined(max)
#undef max
#endif
#if defined(sqr)
#undef sqr
#endif
s32 min(s32 a, s32 b);
f32 minf(f32 a, f32 b);
s32 max(s32 a, s32 b);
f32 maxf(f32 a, f32 b);
s32 sqr(s32 x);
f32 sqrf(f32 x);
f32 sm64_to_radians(s16 sm64Angle);
s16 radians_to_sm64(f32 radiansAngle);
f32 sm64_to_degrees(s16 sm64Angle);
s16 degrees_to_sm64(f32 degreesAngle);
f32 fast_hypot(f32 a, f32 b);
s32 clamp(s32 a, s32 b, s32 c);
f32 clampf(f32 a, f32 b, f32 c);
#endif
#endif // SMLUA_MATH_UTILS_H

View file

@ -121,18 +121,6 @@ next_get:
/////////////////
static f32 sm64_to_radians(f32 val) {
return val * M_PI / 0x8000;
}
static f32 radians_to_sm64(f32 val) {
return val * 0x8000 / M_PI;
}
static f32 asins(f32 val) {
return radians_to_sm64(asin(sm64_to_radians(val)));
}
f32 delta_interpolate_f32(f32 start, f32 end, f32 delta) {
return start * (1.0f - delta) + end * delta;
}