iio: test: fixed-point: new kunit test
Quoted 3 lines · David Lechner
Add a kunit test for
iio_str_to_fixpoint(). This function has an unintuitive API so this is helpful to see how to use it and shows the various edge cases.
...
Quoted 8 lines
Discussion unrelated to the patch:
I'm also a little tempted to introduce a new function that is a bit easier to use. Many callers of
iio_str_to_fixpoint_s64()are doing something likeint_part* 1000 +fract_partand ignoring the possibility of negative values which require special handling.static int
iio_str_to_fixpoint_s64(const char *str, u32 decimal_places, s64 *value)
Quoted 11 lines
{
int int_part, fract_part;
int ret;
ret = iio_str_to_fixpoint(str, int_pow(10, decimal_places - 1),
&int_part, &fract_part);
if (ret)
return ret;
*value = (s64)int_part * int_pow(10, decimal_places) +
(int_part < 0 ? -1 : 1) * fract_part;Obviously if you go this path, the int_pow() can be called only once (yes, we would need a multiplication or division for the other case). The question is how we treat the decimal_places == 0 case.
Quoted 2 lines
return 0; }
Signature · 2 lines
With Best Regards, Andy Shevchenko