diff options
author | Sergey Sharybin <sergey@blender.org> | 2022-02-10 12:37:00 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey@blender.org> | 2022-02-10 14:39:41 +0300 |
commit | 04d55038ee52fc1155cc0ece916d90fd535c2364 (patch) | |
tree | c216c00833a652d914a3a7012751bdd4c2fb90a9 /intern/cycles/util | |
parent | 87d2de88fdeec75c7dcd77f93d51ee71542b45ce (diff) |
Fix size_t -> int -> size_t round trip in Cycles
There are two things achieved by this change:
- No possible downcast of size_t to int when calculating motion steps.
- Disambiguate call to `min()` which was for some reason considered
ambiguous on 32bit platforms `min(int, unsigned int)`.
- Do the same for the `max()` call to keep them symmetrical.
On an implementation side the `min()` is defined for a fixed width
integer type to disambiguate uint from size_t on 32bit platforms,
and yet be able to use it for 32bit operands on 64bit platforms without
upcast.
This ended up in a bit bigger change as the conditional compile-in of
functions is easiest if the functions is templated. Making the functions
templated required to remove the other source of ambiguity which is
`algorithm.h` which was pulling min/max from std.
Now it is the `math.h` which is the source of truth for min/max.
It was only one place which was relying on `algorithm.h` for these
functions, hence the choice of `math.h` as the safest and least
intrusive.
Fixes 32bit platforms (such as i386) in Debian package build system.
Differential Revision: https://developer.blender.org/D14062
Diffstat (limited to 'intern/cycles/util')
-rw-r--r-- | intern/cycles/util/algorithm.h | 2 | ||||
-rw-r--r-- | intern/cycles/util/math.h | 36 | ||||
-rw-r--r-- | intern/cycles/util/murmurhash.cpp | 2 |
3 files changed, 36 insertions, 4 deletions
diff --git a/intern/cycles/util/algorithm.h b/intern/cycles/util/algorithm.h index 63abd4e92a3..7a37ca0fdf4 100644 --- a/intern/cycles/util/algorithm.h +++ b/intern/cycles/util/algorithm.h @@ -21,8 +21,6 @@ CCL_NAMESPACE_BEGIN -using std::max; -using std::min; using std::remove; using std::sort; using std::stable_sort; diff --git a/intern/cycles/util/math.h b/intern/cycles/util/math.h index 605a19aaef0..2bfd4ba19b6 100644 --- a/intern/cycles/util/math.h +++ b/intern/cycles/util/math.h @@ -124,7 +124,41 @@ ccl_device_inline int min(int a, int b) return (a < b) ? a : b; } -ccl_device_inline uint min(uint a, uint b) +ccl_device_inline uint32_t max(uint32_t a, uint32_t b) +{ + return (a > b) ? a : b; +} + +ccl_device_inline uint32_t min(uint32_t a, uint32_t b) +{ + return (a < b) ? a : b; +} + +ccl_device_inline uint64_t max(uint64_t a, uint64_t b) +{ + return (a > b) ? a : b; +} + +ccl_device_inline uint64_t min(uint64_t a, uint64_t b) +{ + return (a < b) ? a : b; +} + +/* NOTE: On 64bit Darwin the `size_t` is defined as `unsigned long int` and `uint64_t` is defined + * as `unsigned long long`. Both of the definitions are 64 bit unsigned integer, but the automatic + * substitution does not allow to automatically pick function defined for `uint64_t` as it is not + * exactly the same type definition. + * Work this around by adding a templated function enabled for `size_t` type which will be used + * when there is no explicit specialization of `min()`/`max()` above. */ + +template<class T> +ccl_device_inline typename std::enable_if_t<std::is_same_v<T, size_t>, T> max(T a, T b) +{ + return (a > b) ? a : b; +} + +template<class T> +ccl_device_inline typename std::enable_if_t<std::is_same_v<T, size_t>, T> min(T a, T b) { return (a < b) ? a : b; } diff --git a/intern/cycles/util/murmurhash.cpp b/intern/cycles/util/murmurhash.cpp index 9ba0a282cc2..603e4a717a1 100644 --- a/intern/cycles/util/murmurhash.cpp +++ b/intern/cycles/util/murmurhash.cpp @@ -23,7 +23,7 @@ #include <stdlib.h> #include <string.h> -#include "util/algorithm.h" +#include "util/math.h" #include "util/murmurhash.h" #if defined(_MSC_VER) |