Skip to content

Commit

Permalink
math: additional logarithm functions
Browse files Browse the repository at this point in the history
  • Loading branch information
miconda committed Apr 14, 2023
1 parent 2cf7d05 commit 17d8a04
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/modules/math/doc/math_admin.xml
Expand Up @@ -80,7 +80,7 @@
</programlisting>
</example>
</section>
<section id="math.f.log">
<section id="math.f.log">
<title>
<function moreinfo="none">log(x, res)</function>
</title>
Expand All @@ -97,7 +97,51 @@
...
log("10", "$var(res)");
$var(x) = 10;
pow("$var(x)", "$var(res)");
log("$var(x)", "$var(res)");
...
</programlisting>
</example>
</section>
<section id="math.f.log2">
<title>
<function moreinfo="none">log2(x, res)</function>
</title>
<para>
The function computes the base-2 logarithm of x and stores the result
in res.
</para>
<para>
This function can be used from ANY_ROUTE.
</para>
<example>
<title><function>log2</function> usage</title>
<programlisting format="linespecific">
...
log2("16", "$var(res)");
$var(x) = 16;
log2("$var(x)", "$var(res)");
...
</programlisting>
</example>
</section>
<section id="math.f.log10">
<title>
<function moreinfo="none">log10(x, res)</function>
</title>
<para>
The function computes the base-10 logarithm of x and stores the result
in res.
</para>
<para>
This function can be used from ANY_ROUTE.
</para>
<example>
<title><function>log10</function> usage</title>
<programlisting format="linespecific">
...
log10("100", "$var(res)");
$var(x) = 100;
log10("$var(x)", "$var(res)");
...
</programlisting>
</example>
Expand Down
62 changes: 62 additions & 0 deletions src/modules/math/math_mod.c
Expand Up @@ -37,6 +37,8 @@ MODULE_VERSION

static int w_math_pow(sip_msg_t *msg, char *v1, char *v2, char *r);
static int w_math_log(sip_msg_t *msg, char *v1, char *r);
static int w_math_log2(sip_msg_t *msg, char *v1, char *r);
static int w_math_log10(sip_msg_t *msg, char *v1, char *r);
static int fixup_math_p2(void **param, int param_no);
static int fixup_math_p3(void **param, int param_no);

Expand All @@ -47,6 +49,10 @@ static cmd_export_t cmds[]={
0, ANY_ROUTE},
{"math_log", (cmd_function)w_math_log, 2, fixup_math_p2,
0, ANY_ROUTE},
{"math_log2", (cmd_function)w_math_log2, 2, fixup_math_p2,
0, ANY_ROUTE},
{"math_log10", (cmd_function)w_math_log10, 2, fixup_math_p2,
0, ANY_ROUTE},

{0, 0, 0, 0, 0, 0}
};
Expand Down Expand Up @@ -127,6 +133,62 @@ static int w_math_log(sip_msg_t *msg, char *v1, char *r)
return 1;
}

/**
*
*/
static int w_math_log2(sip_msg_t *msg, char *v1, char *r)
{
int vi1 = 0;
pv_spec_t *dst;
pv_value_t val = {0};

if(fixup_get_ivalue(msg, (gparam_t*)v1, &vi1)<0) {
LM_ERR("failed to get first parameter value\n");
return -1;
}

dst = (pv_spec_t *)r;
if(dst->setf==NULL) {
LM_ERR("target pv is not writable\n");
return -1;
}

val.ri = (long)log2((double)vi1);
val.flags = PV_TYPE_INT|PV_VAL_INT;

dst->setf(msg, &dst->pvp, (int)EQ_T, &val);

return 1;
}

/**
*
*/
static int w_math_log10(sip_msg_t *msg, char *v1, char *r)
{
int vi1 = 0;
pv_spec_t *dst;
pv_value_t val = {0};

if(fixup_get_ivalue(msg, (gparam_t*)v1, &vi1)<0) {
LM_ERR("failed to get first parameter value\n");
return -1;
}

dst = (pv_spec_t *)r;
if(dst->setf==NULL) {
LM_ERR("target pv is not writable\n");
return -1;
}

val.ri = (long)log10((double)vi1);
val.flags = PV_TYPE_INT|PV_VAL_INT;

dst->setf(msg, &dst->pvp, (int)EQ_T, &val);

return 1;
}

/**
*
*/
Expand Down

0 comments on commit 17d8a04

Please sign in to comment.