diff options
author | Hans-Kristian Arntzen <post@arntzen-software.no> | 2021-09-30 18:04:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-30 18:04:30 +0300 |
commit | 97a438d214b24e4958ca137a18639670648cedd0 (patch) | |
tree | 15acaf889d926f13524fc87ce3455304f15850d6 /spirv_common.hpp | |
parent | 457c00bd07a9ad3062e4521ca4dc2dd6219cc22f (diff) | |
parent | f72bb3c6f5f734cfe1fbd2b00d1b90d2262c2722 (diff) |
Merge pull request #1757 from KhronosGroup/fix-1754
Improve handling of INT_MIN/INT64_MIN literals.
Diffstat (limited to 'spirv_common.hpp')
-rw-r--r-- | spirv_common.hpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/spirv_common.hpp b/spirv_common.hpp index 95f75669..e602fbd4 100644 --- a/spirv_common.hpp +++ b/spirv_common.hpp @@ -211,6 +211,28 @@ inline std::string convert_to_string(const T &t) return std::to_string(t); } +static inline std::string convert_to_string(int32_t value) +{ + // INT_MIN is ... special on some backends. If we use a decimal literal, and negate it, we + // could accidentally promote the literal to long first, then negate. + // To workaround it, emit int(0x80000000) instead. + if (value == std::numeric_limits<int32_t>::min()) + return "int(0x80000000)"; + else + return std::to_string(value); +} + +static inline std::string convert_to_string(int64_t value, const std::string &int64_type, bool long_long_literal_suffix) +{ + // INT64_MIN is ... special on some backends. + // If we use a decimal literal, and negate it, we might overflow the representable numbers. + // To workaround it, emit int(0x80000000) instead. + if (value == std::numeric_limits<int64_t>::min()) + return join(int64_type, "(0x8000000000000000u", (long_long_literal_suffix ? "ll" : "l"), ")"); + else + return std::to_string(value) + (long_long_literal_suffix ? "ll" : "l"); +} + // Allow implementations to set a convenient standard precision #ifndef SPIRV_CROSS_FLT_FMT #define SPIRV_CROSS_FLT_FMT "%.32g" |