155 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H11#define _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H12 13#include <__assert>14#include <__config>15#include <__memory/addressof.h>16#include <__type_traits/integer_traits.h>17#include <__utility/cmp.h>18#include <limits>19 20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif23 24_LIBCPP_PUSH_MACROS25#include <__undef_macros>26 27_LIBCPP_BEGIN_NAMESPACE_STD28 29#if _LIBCPP_STD_VER >= 2030 31template <__signed_or_unsigned_integer _Tp>32_LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {33# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 210134 return __builtin_elementwise_add_sat(__x, __y);35# else36 if (_Tp __sum; !__builtin_add_overflow(__x, __y, std::addressof(__sum)))37 return __sum;38 // Handle overflow39 if constexpr (__unsigned_integer<_Tp>) {40 return std::numeric_limits<_Tp>::max();41 } else {42 // Signed addition overflow43 if (__x > 0)44 // Overflows if (x > 0 && y > 0)45 return std::numeric_limits<_Tp>::max();46 else47 // Overflows if (x < 0 && y < 0)48 return std::numeric_limits<_Tp>::min();49 }50# endif51}52 53template <__signed_or_unsigned_integer _Tp>54_LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {55# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 210156 return __builtin_elementwise_sub_sat(__x, __y);57# else58 if (_Tp __sub; !__builtin_sub_overflow(__x, __y, std::addressof(__sub)))59 return __sub;60 // Handle overflow61 if constexpr (__unsigned_integer<_Tp>) {62 // Overflows if (x < y)63 return std::numeric_limits<_Tp>::min();64 } else {65 // Signed subtration overflow66 if (__x >= 0)67 // Overflows if (x >= 0 && y < 0)68 return std::numeric_limits<_Tp>::max();69 else70 // Overflows if (x < 0 && y > 0)71 return std::numeric_limits<_Tp>::min();72 }73# endif74}75 76template <__signed_or_unsigned_integer _Tp>77_LIBCPP_HIDE_FROM_ABI constexpr _Tp __mul_sat(_Tp __x, _Tp __y) noexcept {78 if (_Tp __mul; !__builtin_mul_overflow(__x, __y, std::addressof(__mul)))79 return __mul;80 // Handle overflow81 if constexpr (__unsigned_integer<_Tp>) {82 return std::numeric_limits<_Tp>::max();83 } else {84 // Signed multiplication overflow85 if ((__x > 0 && __y > 0) || (__x < 0 && __y < 0))86 return std::numeric_limits<_Tp>::max();87 // Overflows if (x < 0 && y > 0) || (x > 0 && y < 0)88 return std::numeric_limits<_Tp>::min();89 }90}91 92template <__signed_or_unsigned_integer _Tp>93_LIBCPP_HIDE_FROM_ABI constexpr _Tp __div_sat(_Tp __x, _Tp __y) noexcept {94 _LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined");95 if constexpr (__unsigned_integer<_Tp>) {96 return __x / __y;97 } else {98 // Handle signed division overflow99 if (__x == std::numeric_limits<_Tp>::min() && __y == _Tp{-1})100 return std::numeric_limits<_Tp>::max();101 return __x / __y;102 }103}104 105template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>106_LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturate_cast(_Tp __x) noexcept {107 // Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be108 // optimized out by the compiler.109 110 // Handle overflow111 if (std::cmp_less(__x, std::numeric_limits<_Rp>::min()))112 return std::numeric_limits<_Rp>::min();113 if (std::cmp_greater(__x, std::numeric_limits<_Rp>::max()))114 return std::numeric_limits<_Rp>::max();115 // No overflow116 return static_cast<_Rp>(__x);117}118 119#endif // _LIBCPP_STD_VER >= 20120 121#if _LIBCPP_STD_VER >= 26122 123template <__signed_or_unsigned_integer _Tp>124[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {125 return std::__add_sat(__x, __y);126}127 128template <__signed_or_unsigned_integer _Tp>129[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {130 return std::__sub_sat(__x, __y);131}132 133template <__signed_or_unsigned_integer _Tp>134[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {135 return std::__mul_sat(__x, __y);136}137 138template <__signed_or_unsigned_integer _Tp>139[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {140 return std::__div_sat(__x, __y);141}142 143template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>144[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {145 return std::__saturate_cast<_Rp>(__x);146}147 148#endif // _LIBCPP_STD_VER >= 26149 150_LIBCPP_END_NAMESPACE_STD151 152_LIBCPP_POP_MACROS153 154#endif // _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H155