diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2020-02-03 16:31:40 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2020-02-03 16:40:58 -0800 |
commit | 835be1c9e05f098885f628ecb43d9f851d468fdf (patch) | |
tree | 0035351a6b369bc36a670712668fa125f6eac876 /src | |
parent | 56ccd8fe1c195b7b204aef430b75b67e37481ef7 (diff) |
libs/sig: add sig_ops_div
Included a little hack to defend against divide by zero, seems
reasonable given the intended use cases.
Diffstat (limited to 'src')
-rw-r--r-- | src/libs/sig/Makefile.am | 2 | ||||
-rw-r--r-- | src/libs/sig/ops_div.c | 61 | ||||
-rw-r--r-- | src/libs/sig/sig.h | 1 |
3 files changed, 63 insertions, 1 deletions
diff --git a/src/libs/sig/Makefile.am b/src/libs/sig/Makefile.am index 55eccd0..7c13a16 100644 --- a/src/libs/sig/Makefile.am +++ b/src/libs/sig/Makefile.am @@ -1,3 +1,3 @@ noinst_LIBRARIES = libsig.a -libsig_a_SOURCES = ops_abs.c ops_add.c ops_ceil.c ops_clamp.c ops_const.c ops_expand.c ops_floor.c ops_inv.c ops_lerp.c ops_max.c ops_min.c ops_mult.c ops_pow.c ops_rand.c ops_round.c ops_scale.c ops_sin.c ops_sub.c sig.c sig.h +libsig_a_SOURCES = ops_abs.c ops_add.c ops_ceil.c ops_clamp.c ops_const.c ops_div.c ops_expand.c ops_floor.c ops_inv.c ops_lerp.c ops_max.c ops_min.c ops_mult.c ops_pow.c ops_rand.c ops_round.c ops_scale.c ops_sin.c ops_sub.c sig.c sig.h libsig_a_CPPFLAGS = -I@top_srcdir@/src diff --git a/src/libs/sig/ops_div.c b/src/libs/sig/ops_div.c new file mode 100644 index 0000000..d0fe4e3 --- /dev/null +++ b/src/libs/sig/ops_div.c @@ -0,0 +1,61 @@ +#include <assert.h> +#include <float.h> + +#include "sig.h" + + +typedef struct ops_div_ctxt_t { + sig_t *a, *b; +} ops_div_ctxt_t; + + +static size_t ops_div_size(va_list ap) +{ + return sizeof(ops_div_ctxt_t); +} + + +static void ops_div_init(void *context, va_list ap) +{ + ops_div_ctxt_t *ctxt = context; + + assert(ctxt); + + ctxt->a = va_arg(ap, sig_t *); + ctxt->b = va_arg(ap, sig_t *); +} + + +static void ops_div_destroy(void *context) +{ + ops_div_ctxt_t *ctxt = context; + + assert(ctxt); + + sig_free(ctxt->a); + sig_free(ctxt->b); +} + + +static float ops_div_output(void *context, unsigned ticks_ms) +{ + ops_div_ctxt_t *ctxt = context; + float divisor; + + assert(ctxt); + + /* XXX: protect against divide by zero by replacing with epsilon */ + divisor = sig_output(ctxt->b, ticks_ms); + if (divisor == 0.f) + divisor = FLT_EPSILON; + + return sig_output(ctxt->a, ticks_ms) / divisor; +} + + +sig_ops_t sig_ops_div = { + .size = ops_div_size, + .init = ops_div_init, + .destroy = ops_div_destroy, + .output = ops_div_output, +}; diff --git a/src/libs/sig/sig.h b/src/libs/sig/sig.h index 653202c..57b9a90 100644 --- a/src/libs/sig/sig.h +++ b/src/libs/sig/sig.h @@ -31,6 +31,7 @@ extern sig_ops_t sig_ops_abs; extern sig_ops_t sig_ops_add; extern sig_ops_t sig_ops_ceil; extern sig_ops_t sig_ops_clamp; +extern sig_ops_t sig_ops_div; extern sig_ops_t sig_ops_expand; extern sig_ops_t sig_ops_floor; extern sig_ops_t sig_ops_inv; |