Skip to content
Commit 37ef725c authored by Marc Herbert's avatar Marc Herbert Committed by Anas Nashif
Browse files

time_units.h: never return zero from z_tmcvt_divisor(a, b)



Since commit ea61c8c1 ("sys/time_units.h: Work around clang
div-by-zero warning") `z_tmcvt_divisor(a, b)` started to "cheat" and
never return 0; but only when `#ifdef __clang__`.

After some very tedious experiments and "reverse-engineering" of
pre-processor output (the horror...), I finally found why xt-xc++
12.0.8 (based on gcc 4.2.0) produces the same warnings and I discovered
this clang-specific workaround.

Rather than adding gcc to a list of "privileged" compilers, make the
code more consistent and predictable: make `z_tmcvt_divisor(a, b)` never
return zero for _all_ compilers.

Drop the usage of the non-standard "Elvis" operator `?: 1` to be
compatible with any compiler; everything is evaluated at compile-time
anyway (and the pre-processor output is almost impossible to read
already anyway)

Surprisingly, only C++ files (_every_ C++ file) emitted the
divide-by-zero warning with that xt-xc++ version. The very same code
compiled by xt-gcc in the same toolchain never showed that warning.

Probably due to the abuse of macros, the warning was super cryptic:
```
          from every.cpp:

   include/zephyr/kernel.h: In function ‘int32_t k_msleep(int32_t)’:
   include/zephyr/kernel.h: warning: division by zero in
      ‘(((uint64_t)(ms <unknown operator> 0)) + 0xffffffffffffffffull) / 0’
```

I didn't find any `0xffffffffffffffffull` anywhere in the code and I
still have no idea what <unknown operator> is.

Signed-off-by: default avatarMarc Herbert <marc.herbert@intel.com>
parent 69001149
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment