Path: blob/main/contrib/llvm-project/libc/src/__support/FPUtil/PolyEval.h
213799 views
//===-- Common header for PolyEval implementations --------------*- 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_POLYEVAL_H9#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_POLYEVAL_H1011#include "multiply_add.h"12#include "src/__support/CPP/type_traits.h"13#include "src/__support/common.h"14#include "src/__support/macros/config.h"1516// Evaluate polynomial using Horner's Scheme:17// With polyeval(x, a_0, a_1, ..., a_n) = a_n * x^n + ... + a_1 * x + a_0, we18// evaluated it as: a_0 + x * (a_1 + x * ( ... (a_(n-1) + x * a_n) ... ) ) ).19// We will use FMA instructions if available.20// Example: to evaluate x^3 + 2*x^2 + 3*x + 4, call21// polyeval( x, 4.0, 3.0, 2.0, 1.0 )2223namespace LIBC_NAMESPACE_DECL {24namespace fputil {2526template <typename T>27LIBC_INLINE cpp::enable_if_t<(sizeof(T) > sizeof(void *)), T>28polyeval(const T &, const T &a0) {29return a0;30}3132template <typename T>33LIBC_INLINE cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T> polyeval(T,34T a0) {35return a0;36}3738template <typename T, typename... Ts>39LIBC_INLINE cpp::enable_if_t<(sizeof(T) > sizeof(void *)), T>40polyeval(const T &x, const T &a0, const Ts &...a) {41return multiply_add(x, polyeval(x, a...), a0);42}4344template <typename T, typename... Ts>45LIBC_INLINE cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>46polyeval(T x, T a0, Ts... a) {47return multiply_add(x, polyeval(x, a...), a0);48}4950} // namespace fputil51} // namespace LIBC_NAMESPACE_DECL5253#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_POLYEVAL_H545556