5 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz>
12 #include <core/GP_Convert_Scale.gen.h>
19 @ for i in range(1, max_in):
20 @ for j in range(1, max_out):
21 static int check_convert_{{ i }}_{{ j }}(void)
23 unsigned int v, fail = 0;
24 uint32_t res, exp_res;
27 for (v = 0; v < {{ 2 ** i - 1 }}; v++) {
28 res = GP_SCALE_VAL_{{ i }}_{{ j }}(v);
31 * We have {{ 2**i }} values and we need to map them to
32 * subset of {{ 2**j }} values while making sure 0 -> 0
33 * and {{ 2**i - 1 }} -> {{ 2**j - 1 }} and that the
34 * mapping is as evenly distributed as possible.
36 * So we map the input to 0-1 interval by dividing it by
37 * maximal input value {{ 2**i - 1 }} and then multiply
38 * it by output maximal value {{ 2**j - 1}}.
40 fres = (v / {{ (2.00 ** i - 1) }}) * {{ (2.00 ** j - 1) }};
41 exp_res = round(fres);
44 * We have {{ 2**i }} values that must be mapped to {{ 2**j }}
45 * so we do simple division and floor() which maps the values
46 * evenly, 0 -> 0 and {{ 2**i - 1 }} -> {{ 2**j - 1 }}.
48 * In terms for implementation this is just bitshift.
50 fres = v * {{ (2.00 ** j) / (2.00 ** i) }};
51 exp_res = floor(fres);
56 tst_msg("GP_SCALE_{{ i }}_{{ j }}(%i) = %i, "
57 "expected %i %f", v, res, exp_res, fres);
64 tst_msg("+ next %u failures", fail - 5);
73 const struct tst_suite tst_suite = {
74 .suite_name = "Convert Scale Testsuite",
76 @ for i in range(1, max_in):
77 @ for j in range(1, max_out):
78 {.name = "SCALE_{{ i }}_{{ j }}()",
79 .tst_fn = check_convert_{{ i }}_{{ j }}},