66 lines · cpp
1//===-- Exhaustive tests for bfloat16 addition ----------------------------===//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#include "exhaustive_test.h"10#include "src/__support/FPUtil/FPBits.h"11#include "src/__support/FPUtil/bfloat16.h"12#include "test/UnitTest/FPMatcher.h"13#include "utils/MPFRWrapper/MPCommon.h"14#include "utils/MPFRWrapper/MPFRUtils.h"15 16namespace mpfr = LIBC_NAMESPACE::testing::mpfr;17using LIBC_NAMESPACE::fputil::BFloat16;18 19static BFloat16 add_func(BFloat16 x, BFloat16 y) { return x + y; }20 21struct Bfloat16AddChecker : public virtual LIBC_NAMESPACE::testing::Test {22 using FloatType = BFloat16;23 using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;24 using StorageType = typename FPBits::StorageType;25 26 uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start,27 uint16_t y_stop, mpfr::RoundingMode rounding) {28 mpfr::ForceRoundingMode r(rounding);29 if (!r.success)30 return true;31 uint16_t xbits = x_start;32 uint64_t failed = 0;33 do {34 BFloat16 x = FPBits(xbits).get_val();35 uint16_t ybits = xbits;36 do {37 BFloat16 y = FPBits(ybits).get_val();38 mpfr::BinaryInput<BFloat16> input{x, y};39 bool correct = TEST_MPFR_MATCH_ROUNDING_SILENTLY(40 mpfr::Operation::Add, input, add_func(x, y), 0.5, rounding);41 failed += (!correct);42 } while (ybits++ < y_stop);43 } while (xbits++ < x_stop);44 return failed;45 }46};47 48using LlvmLibcBfloat16ExhaustiveAddTest =49 LlvmLibcExhaustiveMathTest<Bfloat16AddChecker, 1 << 2>;50 51// range: [0, inf]52static constexpr uint16_t POS_START = 0x0000U;53static constexpr uint16_t POS_STOP = 0x7f80U;54 55// range: [-0, -inf]56static constexpr uint16_t NEG_START = 0x8000U;57static constexpr uint16_t NEG_STOP = 0xff80U;58 59TEST_F(LlvmLibcBfloat16ExhaustiveAddTest, PositiveRange) {60 test_full_range_all_roundings(POS_START, POS_STOP, POS_START, POS_STOP);61}62 63TEST_F(LlvmLibcBfloat16ExhaustiveAddTest, NegativeRange) {64 test_full_range_all_roundings(NEG_START, NEG_STOP, NEG_START, NEG_STOP);65}66