1d883a4adSTue Ly //===-- Utilities for trigonometric functions -------------------*- C++ -*-===// 2d883a4adSTue Ly // 3d883a4adSTue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4d883a4adSTue Ly // See https://llvm.org/LICENSE.txt for license information. 5d883a4adSTue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6d883a4adSTue Ly // 7d883a4adSTue Ly //===----------------------------------------------------------------------===// 8d883a4adSTue Ly 9d883a4adSTue Ly #ifndef LLVM_LIBC_SRC_MATH_GENERIC_RANGE_REDUCTION_H 10d883a4adSTue Ly #define LLVM_LIBC_SRC_MATH_GENERIC_RANGE_REDUCTION_H 11d883a4adSTue Ly 12d883a4adSTue Ly #include "src/__support/FPUtil/FPBits.h" 13d883a4adSTue Ly #include "src/__support/FPUtil/multiply_add.h" 14d883a4adSTue Ly #include "src/__support/FPUtil/nearest_integer.h" 156363320bSSiva Chandra Reddy #include "src/__support/common.h" 16*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 17d883a4adSTue Ly 18*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 19d883a4adSTue Ly 20d883a4adSTue Ly namespace generic { 21d883a4adSTue Ly 2215b9380dSTue Ly static constexpr uint32_t FAST_PASS_BOUND = 0x4a80'0000U; // 2^22 23d883a4adSTue Ly 24d883a4adSTue Ly static constexpr int N_ENTRIES = 8; 25d883a4adSTue Ly 2642f18379STue Ly // We choose to split bits of 32/pi into 28-bit precision pieces, so that the 2742f18379STue Ly // product of x * THIRTYTWO_OVER_PI_28[i] is exact. 28d883a4adSTue Ly // These are generated by Sollya with: 2942f18379STue Ly // > a1 = D(round(32/pi, 28, RN)); a1; 3042f18379STue Ly // > a2 = D(round(32/pi - a1, 28, RN)); a2; 3142f18379STue Ly // > a3 = D(round(32/pi - a1 - a2, 28, RN)); a3; 3242f18379STue Ly // > a4 = D(round(32/pi - a1 - a2 - a3, 28, RN)); a4; 33d883a4adSTue Ly // ... 3442f18379STue Ly static constexpr double THIRTYTWO_OVER_PI_28[N_ENTRIES] = { 3542f18379STue Ly 0x1.45f306ep+3, -0x1.b1bbeaep-28, 0x1.3f84ebp-57, -0x1.7056592p-87, 3642f18379STue Ly 0x1.c0db62ap-116, -0x1.4cd8778p-145, -0x1.bef806cp-174, 0x1.63abdecp-204}; 37d883a4adSTue Ly 38d883a4adSTue Ly // Exponents of the least significant bits of the corresponding entries in 3942f18379STue Ly // THIRTYTWO_OVER_PI_28. 4042f18379STue Ly static constexpr int THIRTYTWO_OVER_PI_28_LSB_EXP[N_ENTRIES] = { 4142f18379STue Ly -24, -55, -81, -114, -143, -170, -200, -230}; 42d883a4adSTue Ly 4315b9380dSTue Ly // Return k and y, where 4415b9380dSTue Ly // k = round(x * 16 / pi) and y = (x * 16 / pi) - k. 456363320bSSiva Chandra Reddy LIBC_INLINE int64_t small_range_reduction(double x, double &y) { 4642f18379STue Ly double prod = x * THIRTYTWO_OVER_PI_28[0]; 47d883a4adSTue Ly double kd = fputil::nearest_integer(prod); 48d883a4adSTue Ly y = prod - kd; 4942f18379STue Ly y = fputil::multiply_add(x, THIRTYTWO_OVER_PI_28[1], y); 5042f18379STue Ly y = fputil::multiply_add(x, THIRTYTWO_OVER_PI_28[2], y); 51d883a4adSTue Ly return static_cast<int64_t>(kd); 52d883a4adSTue Ly } 53d883a4adSTue Ly 54d883a4adSTue Ly // Return k and y, where 5542f18379STue Ly // k = round(x * 32 / pi) and y = (x * 32 / pi) - k. 5642f18379STue Ly // For large range, there are at most 2 parts of THIRTYTWO_OVER_PI_28 5742f18379STue Ly // contributing to the lowest 6 binary digits (k & 63). If the least 5842f18379STue Ly // significant bit of x * the least significant bit of THIRTYTWO_OVER_PI_28[i] 5942f18379STue Ly // >= 64, we can completely ignore THIRTYTWO_OVER_PI_28[i]. 606363320bSSiva Chandra Reddy LIBC_INLINE int64_t large_range_reduction(double x, int x_exp, double &y) { 61d883a4adSTue Ly int idx = 0; 62d883a4adSTue Ly y = 0; 63c09e6905SGuillaume Chatelet int x_lsb_exp_m4 = x_exp - fputil::FPBits<float>::FRACTION_LEN; 64d883a4adSTue Ly 6542f18379STue Ly // Skipping the first parts of 32/pi such that: 6642f18379STue Ly // LSB of x * LSB of THIRTYTWO_OVER_PI_28[i] >= 32. 6742f18379STue Ly while (x_lsb_exp_m4 + THIRTYTWO_OVER_PI_28_LSB_EXP[idx] > 5) 68d883a4adSTue Ly ++idx; 69d883a4adSTue Ly 7042f18379STue Ly double prod_hi = x * THIRTYTWO_OVER_PI_28[idx]; 7142f18379STue Ly // Get the integral part of x * THIRTYTWO_OVER_PI_28[idx] 72d883a4adSTue Ly double k_hi = fputil::nearest_integer(prod_hi); 7342f18379STue Ly // Get the fractional part of x * THIRTYTWO_OVER_PI_28[idx] 74d883a4adSTue Ly double frac = prod_hi - k_hi; 7542f18379STue Ly double prod_lo = fputil::multiply_add(x, THIRTYTWO_OVER_PI_28[idx + 1], frac); 76d883a4adSTue Ly double k_lo = fputil::nearest_integer(prod_lo); 77d883a4adSTue Ly 78d883a4adSTue Ly // Now y is the fractional parts. 79d883a4adSTue Ly y = prod_lo - k_lo; 8042f18379STue Ly y = fputil::multiply_add(x, THIRTYTWO_OVER_PI_28[idx + 2], y); 8142f18379STue Ly y = fputil::multiply_add(x, THIRTYTWO_OVER_PI_28[idx + 3], y); 82d883a4adSTue Ly 8315b9380dSTue Ly return static_cast<int64_t>(k_hi) + static_cast<int64_t>(k_lo); 84d883a4adSTue Ly } 85d883a4adSTue Ly 86d883a4adSTue Ly } // namespace generic 87d883a4adSTue Ly 88*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 89d883a4adSTue Ly 90d883a4adSTue Ly #endif // LLVM_LIBC_SRC_MATH_GENERIC_RANGE_REDUCTION_H 91