aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/math/remquof_test.cpp
blob: 4ede7fb3feeca79c8410f4f70c06ff93c1fd5e65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//===-- Unittests for remquof ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/remquof.h"
#include "utils/FPUtil/BasicOperations.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/FPUtil/TestHelpers.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/Test.h"
#include <math.h>

using FPBits = __llvm_libc::fputil::FPBits<float>;
using UIntType = FPBits::UIntType;

namespace mpfr = __llvm_libc::testing::mpfr;

DECLARE_SPECIAL_CONSTANTS(float)

TEST(RemquofTest, SpecialNumbers) {
  int exponent;
  float x, y;

  y = 1.0f;
  x = inf;
  EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
  x = negInf;
  EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);

  x = 1.0f;
  y = zero;
  EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
  y = negZero;
  EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);

  y = nan;
  x = 1.0f;
  EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);

  y = 1.0f;
  x = nan;
  EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);

  x = nan;
  y = nan;
  EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);

  x = zero;
  y = 1.0f;
  EXPECT_FP_EQ(__llvm_libc::remquof(x, y, &exponent), zero);

  x = negZero;
  y = 1.0f;
  EXPECT_FP_EQ(__llvm_libc::remquof(x, y, &exponent), negZero);
}

TEST(RemquofTest, SubnormalRange) {
  constexpr UIntType count = 1000001;
  constexpr UIntType step =
      (FPBits::maxSubnormal - FPBits::minSubnormal) / count;
  for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal;
       v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal;
       v += step, w -= step) {
    float x = FPBits(v), y = FPBits(w);
    mpfr::BinaryOutput<float> result;
    mpfr::BinaryInput<float> input{x, y};
    result.f = __llvm_libc::remquof(x, y, &result.i);
    ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0);
  }
}

TEST(RemquofTest, NormalRange) {
  constexpr UIntType count = 1000001;
  constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count;
  for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal;
       v <= FPBits::maxNormal && w >= FPBits::minNormal; v += step, w -= step) {
    float x = FPBits(v), y = FPBits(w);
    mpfr::BinaryOutput<float> result;
    mpfr::BinaryInput<float> input{x, y};
    result.f = __llvm_libc::remquof(x, y, &result.i);
    ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0);
  }
}