1 //===-- Common header for PolyEval implementations --------------*- C++ -*-===// 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_POLYEVAL_H 10 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_POLYEVAL_H 11 12 #include "multiply_add.h" 13 14 // Evaluate polynomial using Horner's Scheme: 15 // With polyeval(x, a_0, a_1, ..., a_n) = a_n * x^n + ... + a_1 * x + a_0, we 16 // evaluated it as: a_0 + x * (a_1 + x * ( ... (a_(n-1) + x * a_n) ... ) ) ). 17 // We will use FMA instructions if available. 18 // Example: to evaluate x^3 + 2*x^2 + 3*x + 4, call 19 // polyeval( x, 4.0, 3.0, 2.0, 1.0 ) 20 21 namespace __llvm_libc { 22 namespace fputil { 23 24 template <typename T> static inline T polyeval(T x, T a0) { return a0; } 25 26 template <typename T, typename... Ts> 27 static inline T polyeval(T x, T a0, Ts... a) { 28 return multiply_add(x, polyeval(x, a...), a0); 29 } 30 31 } // namespace fputil 32 } // namespace __llvm_libc 33 34 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_POLYEVAL_H 35