Path: blob/main/contrib/llvm-project/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
213799 views
//===-- Floating point divsion and remainder operations ---------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_DIVISIONANDREMAINDEROPERATIONS_H9#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_DIVISIONANDREMAINDEROPERATIONS_H1011#include "FPBits.h"12#include "ManipulationFunctions.h"13#include "NormalFloat.h"1415#include "src/__support/CPP/type_traits.h"16#include "src/__support/common.h"17#include "src/__support/macros/config.h"1819namespace LIBC_NAMESPACE_DECL {20namespace fputil {2122static constexpr int QUOTIENT_LSB_BITS = 3;2324// The implementation is a bit-by-bit algorithm which uses integer division25// to evaluate the quotient and remainder.26template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>27LIBC_INLINE T remquo(T x, T y, int &q) {28FPBits<T> xbits(x), ybits(y);29if (xbits.is_nan())30return x;31if (ybits.is_nan())32return y;33if (xbits.is_inf() || ybits.is_zero())34return FPBits<T>::quiet_nan().get_val();3536if (xbits.is_zero()) {37q = 0;38return LIBC_NAMESPACE::fputil::copysign(T(0.0), x);39}4041if (ybits.is_inf()) {42q = 0;43return x;44}4546const Sign result_sign =47(xbits.sign() == ybits.sign() ? Sign::POS : Sign::NEG);4849// Once we know the sign of the result, we can just operate on the absolute50// values. The correct sign can be applied to the result after the result51// is evaluated.52xbits.set_sign(Sign::POS);53ybits.set_sign(Sign::POS);5455NormalFloat<T> normalx(xbits), normaly(ybits);56int exp = normalx.exponent - normaly.exponent;57typename NormalFloat<T>::StorageType mx = normalx.mantissa,58my = normaly.mantissa;5960q = 0;61while (exp >= 0) {62unsigned shift_count = 0;63typename NormalFloat<T>::StorageType n = mx;64for (shift_count = 0; n < my; n <<= 1, ++shift_count)65;6667if (static_cast<int>(shift_count) > exp)68break;6970exp -= shift_count;71if (0 <= exp && exp < QUOTIENT_LSB_BITS)72q |= (1 << exp);7374mx = n - my;75if (mx == 0) {76q = result_sign.is_neg() ? -q : q;77return LIBC_NAMESPACE::fputil::copysign(T(0.0), x);78}79}8081NormalFloat<T> remainder(Sign::POS, exp + normaly.exponent, mx);8283// Since NormalFloat to native type conversion is a truncation operation84// currently, the remainder value in the native type is correct as is.85// However, if NormalFloat to native type conversion is updated in future,86// then the conversion to native remainder value should be updated87// appropriately and some directed tests added.88T native_remainder(remainder);89T absy = ybits.get_val();90int cmp = remainder.mul2(1).cmp(normaly);91if (cmp > 0) {92q = q + 1;93if (x >= T(0.0))94native_remainder = native_remainder - absy;95else96native_remainder = absy - native_remainder;97} else if (cmp == 0) {98if (q & 1) {99q += 1;100if (x >= T(0.0))101native_remainder = -native_remainder;102} else {103if (x < T(0.0))104native_remainder = -native_remainder;105}106} else {107if (x < T(0.0))108native_remainder = -native_remainder;109}110111q = result_sign.is_neg() ? -q : q;112if (native_remainder == T(0.0))113return LIBC_NAMESPACE::fputil::copysign(T(0.0), x);114return native_remainder;115}116117} // namespace fputil118} // namespace LIBC_NAMESPACE_DECL119120#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_DIVISIONANDREMAINDEROPERATIONS_H121122123