brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.4 KiB · c2be8c5 Raw
160 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: std-at-least-c++2610 11// <numeric>12 13// template<class T>14// constexpr T sub_sat(T x, T y) noexcept;                     // freestanding15 16#include <cassert>17#include <concepts>18#include <limits>19#include <numeric>20 21#include "test_macros.h"22 23template <typename IntegerT>24constexpr bool test_signed() {25  constexpr auto minVal = std::numeric_limits<IntegerT>::min();26  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();27 28  std::same_as<IntegerT> decltype(auto) _ = std::sub_sat(minVal, maxVal);29 30  static_assert(noexcept(std::sub_sat(minVal, maxVal)));31 32  // clang-format off33 34  // Limit values (-1, 0, 1, min, max)35 36  assert(std::sub_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 0});37  assert(std::sub_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{-1});38  assert(std::sub_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-2});39  assert(std::sub_sat(IntegerT{-1},       minVal) == IntegerT{-1} - minVal);40  assert(std::sub_sat(IntegerT{-1},       maxVal) == IntegerT{-1} - maxVal);41 42  assert(std::sub_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 1});43  assert(std::sub_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});44  assert(std::sub_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{-1});45  assert(std::sub_sat(IntegerT{ 0},       minVal) == maxVal); // saturated46  assert(std::sub_sat(IntegerT{ 0},       maxVal) == -maxVal);47 48  assert(std::sub_sat(      minVal, IntegerT{-1}) == minVal - IntegerT{-1});49  assert(std::sub_sat(      minVal, IntegerT{ 0}) == minVal);50  assert(std::sub_sat(      minVal, IntegerT{ 1}) == minVal); // saturated51  assert(std::sub_sat(      minVal,       minVal) == IntegerT{0});52  assert(std::sub_sat(      minVal,       maxVal) == minVal); // saturated53 54  assert(std::sub_sat(      maxVal, IntegerT{-1}) == maxVal); // saturated55  assert(std::sub_sat(      maxVal, IntegerT{ 0}) == maxVal);56  assert(std::sub_sat(      maxVal, IntegerT{ 1}) == maxVal - IntegerT{ 1});57  assert(std::sub_sat(      maxVal,       minVal) == maxVal); // saturated58  assert(std::sub_sat(      maxVal,       maxVal) == IntegerT{0});59 60  // No saturation (no limit values)61 62  assert(std::sub_sat(IntegerT{ 27}, IntegerT{-28}) ==  55);63  assert(std::sub_sat(IntegerT{ 27}, IntegerT{ 28}) ==  -1);64  assert(std::sub_sat(IntegerT{-27}, IntegerT{ 28}) == -55);65  assert(std::sub_sat(IntegerT{-27}, IntegerT{-28}) ==   1);66 67  // Saturation (no limit values)68 69  {70    constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};71    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};72    assert(std::sub_sat(lesserVal, biggerVal) == minVal); // saturated73  }74  {75    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};76    constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};77    assert(std::sub_sat(biggerVal, lesserVal) == maxVal); // saturated78  }79 80  // clang-format on81 82  return true;83}84 85template <typename IntegerT>86constexpr bool test_unsigned() {87  constexpr auto minVal = std::numeric_limits<IntegerT>::min();88  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();89 90  std::same_as<IntegerT> decltype(auto) _ = std::sub_sat(minVal, maxVal);91 92  static_assert(noexcept(std::sub_sat(minVal, maxVal)));93 94  // clang-format off95 96  // Limit values (0, 1, min, max)97 98  assert(std::sub_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0});99  assert(std::sub_sat(IntegerT{0}, IntegerT{1}) == minVal); // saturated100  assert(std::sub_sat(IntegerT{0},      minVal) == minVal);101  assert(std::sub_sat(IntegerT{0},      maxVal) == minVal); // saturated102 103  assert(std::sub_sat(IntegerT{1}, IntegerT{0}) == IntegerT{1});104  assert(std::sub_sat(IntegerT{1}, IntegerT{1}) == IntegerT{0});105  assert(std::sub_sat(IntegerT{1},      minVal) == IntegerT{1});106  assert(std::sub_sat(IntegerT{1},      maxVal) == minVal); // saturated107 108  assert(std::sub_sat(     minVal, IntegerT{0}) == IntegerT{0});109  assert(std::sub_sat(     minVal, IntegerT{1}) == minVal);110  assert(std::sub_sat(     minVal,      maxVal) == minVal);111  assert(std::sub_sat(     minVal,      maxVal) == minVal);112 113  assert(std::sub_sat(     maxVal, IntegerT{0}) == maxVal);114  assert(std::sub_sat(     maxVal, IntegerT{1}) == maxVal - IntegerT{1});115  assert(std::sub_sat(     maxVal,      minVal) == maxVal);116  assert(std::sub_sat(     maxVal,      maxVal) == IntegerT{0});117 118  // Saturation (no limit values)119 120  {121    constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};122    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};123    assert(std::sub_sat(lesserVal, biggerVal) == minVal); // saturated124  }125 126  // clang-format on127 128  return true;129}130 131constexpr bool test() {132  // Signed133  test_signed<signed char>();134  test_signed<short int>();135  test_signed<int>();136  test_signed<long int>();137  test_signed<long long int>();138#ifndef TEST_HAS_NO_INT128139  test_signed<__int128_t>();140#endif141  // Unsigned142  test_unsigned<unsigned char>();143  test_unsigned<unsigned short int>();144  test_unsigned<unsigned int>();145  test_unsigned<unsigned long int>();146  test_unsigned<unsigned long long int>();147#ifndef TEST_HAS_NO_INT128148  test_unsigned<__uint128_t>();149#endif150 151  return true;152}153 154int main(int, char**) {155  test();156  static_assert(test());157 158  return 0;159}160