From 835be1c9e05f098885f628ecb43d9f851d468fdf Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 3 Feb 2020 16:31:40 -0800 Subject: libs/sig: add sig_ops_div Included a little hack to defend against divide by zero, seems reasonable given the intended use cases. --- src/libs/sig/ops_div.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/libs/sig/ops_div.c (limited to 'src/libs/sig/ops_div.c') 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 +#include + +#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, +}; -- cgit v1.2.3