diff options
author | Jacques Lucke <jacques@blender.org> | 2020-07-16 12:05:13 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-07-16 12:05:30 +0300 |
commit | f6f93b5b126d320a223977a07e3e0510ba684ceb (patch) | |
tree | 5dc36cc11ae21a320612392bc6a1c42c49fa0d7f /source/blender/blenlib/intern/math_base_inline.c | |
parent | 7e0bf7a0f10bc86fa50ddc852a949890efc11fe2 (diff) |
BLI: add safe_divide, safe_modf and safe_logf
Those are defined exactly as their corresponding functions in Cycles.
Diffstat (limited to 'source/blender/blenlib/intern/math_base_inline.c')
-rw-r--r-- | source/blender/blenlib/intern/math_base_inline.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index 26dc4d35c80..bd4eb12a72c 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -348,6 +348,24 @@ MINLINE signed char round_db_to_char_clamp(double a){ #undef _round_clamp_fl_impl #undef _round_clamp_db_impl +MINLINE float safe_divide(float a, float b) +{ + return (b != 0.0f) ? a / b : 0.0f; +} + +MINLINE float safe_modf(float a, float b) +{ + return (b != 0.0f) ? fmodf(a, b) : 0.0f; +} + +MINLINE float safe_logf(float a, float base) +{ + if (UNLIKELY(a <= 0.0f || base <= 0.0f)) { + return 0.0f; + } + return safe_divide(logf(a), logf(base)); +} + /* integer division that rounds 0.5 up, particularly useful for color blending * with integers, to avoid gradual darkening when rounding down */ MINLINE int divide_round_i(int a, int b) |