Reimplement C-like min() and max() functions.
All checks were successful
The build was successful.

These functions should have C-like implementation and take exactly 2 arguments: integer or float.
Finally, C-implementation will be faster than using PH7 builtin library that needs to be compiled and interpreted.
This commit is contained in:
2018-08-15 17:26:26 +02:00
parent 89d7aca73a
commit af139b3f0a
3 changed files with 69 additions and 65 deletions

View File

@@ -624,6 +624,70 @@ static int PH7_builtin_hypot(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_result_double(pCtx, r);
return PH7_OK;
}
/*
* float/int64 max(float/int64 $arg )
* Absolute value.
* Parameter
* The number to process.
* Return
* The absolute value of number.
*/
static int PH7_builtin_max(ph7_context *pCtx, int nArg, ph7_value **apArg) {
if(nArg < 2) {
/* Missing argument,return 0 */
ph7_result_int(pCtx, 0);
return PH7_OK;
}
if(ph7_value_is_float(apArg[0]) && ph7_value_is_float(apArg[1])) {
double a, b;
a = ph7_value_to_double(apArg[0]);
b = ph7_value_to_double(apArg[1]);
/* Perform the requested operation */
ph7_result_double(pCtx, SXMAX(a, b));
} else if(ph7_value_is_int(apArg[0]) && ph7_value_is_int(apArg[1])) {
sxi64 a, b;
a = ph7_value_to_int64(apArg[0]);
b = ph7_value_to_int64(apArg[1]);
/* Perform the requested operation */
ph7_result_int64(pCtx, SXMAX(a, b));
} else {
/* Two parameters of different type */
ph7_result_int(pCtx, 0);
}
return PH7_OK;
}
/*
* float/int64 min(float/int64 $arg )
* Absolute value.
* Parameter
* The number to process.
* Return
* The absolute value of number.
*/
static int PH7_builtin_min(ph7_context *pCtx, int nArg, ph7_value **apArg) {
if(nArg < 2) {
/* Missing argument,return 0 */
ph7_result_int(pCtx, 0);
return PH7_OK;
}
if(ph7_value_is_float(apArg[0]) && ph7_value_is_float(apArg[1])) {
double a, b;
a = ph7_value_to_double(apArg[0]);
b = ph7_value_to_double(apArg[1]);
/* Perform the requested operation */
ph7_result_double(pCtx, SXMIN(a, b));
} else if(ph7_value_is_int(apArg[0]) && ph7_value_is_int(apArg[1])) {
sxi64 a, b;
a = ph7_value_to_int64(apArg[0]);
b = ph7_value_to_int64(apArg[1]);
/* Perform the requested operation */
ph7_result_int64(pCtx, SXMIN(a, b));
} else {
/* Two parameters of different type */
ph7_result_int(pCtx, 0);
}
return PH7_OK;
}
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
sxi32 rc;

View File

@@ -51,6 +51,8 @@ static int PH7_builtin_pow(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_pi(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_fmod(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_hypot(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_max(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_min(ph7_context *pCtx, int nArg, ph7_value **apArg);
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc);
static const ph7_builtin_constant mathConstList[] = {
@@ -94,7 +96,9 @@ static const ph7_builtin_func mathFuncList[] = {
{ "pow", PH7_builtin_pow },
{ "pi", PH7_builtin_pi },
{ "fmod", PH7_builtin_fmod },
{ "hypot", PH7_builtin_hypot }
{ "hypot", PH7_builtin_hypot },
{ "max", PH7_builtin_max },
{ "min", PH7_builtin_min }
};
#endif