Skip to content

Conversation

@lntue
Copy link
Contributor

@lntue lntue commented Jul 17, 2024

Division-less Newton iterations algorithm for cube roots.

  1. Range reduction

For x = (-1)^s * 2^e * (1.m), we get 2 reduced arguments x_r and a as:

  x_r = 1.m
  a   = (-1)^s * 2^(e % 3) * (1.m)

Then cbrt(x) = x^(1/3) can be computed as:

  x^(1/3) = 2^(e / 3) * a^(1/3).

In order to avoid division, we compute a^(-2/3) using Newton method and then
multiply the results by a:

  a^(1/3) = a * a^(-2/3).
  1. First approximation to a^(-2/3)

First, we use a degree-7 minimax polynomial generated by Sollya to
approximate x_r^(-2/3) for 1 <= x_r < 2.

  p = P(x_r) ~ x_r^(-2/3),

with relative errors bounded by:

  | p / x_r^(-2/3) - 1 | < 1.16 * 2^-21.

Then we multiply with 2^(e % 3) from a small lookup table to get:

  x_0 = 2^(-2*(e % 3)/3) * p
      ~ 2^(-2*(e % 3)/3) * x_r^(-2/3)
      = a^(-2/3)

with relative errors:

  | x_0 / a^(-2/3) - 1 | < 1.16 * 2^-21.

This step is done in double precision.

  1. First Newton iteration

We follow the method described in:
Sibidanov, A. and Zimmermann, P., "Correctly rounded cubic root evaluation
in double precision", https://core-math.gitlabpages.inria.fr/cbrt64.pdf
to derive multiplicative Newton iterations as below:
Let x_n be the nth approximation to a^(-2/3). Define the n^th error as:

  h_n = x_n^3 * a^2 - 1

Then:

  a^(-2/3) = x_n / (1 + h_n)^(1/3)
           = x_n * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3 + ...)

using the Taylor series expansion of (1 + h_n)^(-1/3).

Apply to x_0 above:

  h_0 = x_0^3 * a^2 - 1
      = a^2 * (x_0 - a^(-2/3)) * (x_0^2 + x_0 * a^(-2/3) + a^(-4/3)),

it's bounded by:

  |h_0| < 4 * 3 * 1.16 * 2^-21 < 2^-17.

So in the first iteration step, we use:

  x_1 = x_0 * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3)

Its relative error is bounded by:

  | x_1 / a^(-2/3) - 1 | < 35/242 * |h_0|^4 < 2^-70.

Then we perform Ziv's rounding test and check if the answer is exact.
This step is done in double-double precision.

  1. Second Newton iteration

If the Ziv's rounding test from the previous step fails, we define the error
term:

  h_1 = x_1^3 * a^2 - 1,

And perform another iteration:

  x_2 = x_1 * (1 - h_1 / 3)

with the relative errors exceed the precision of double-double.
We then check the Ziv's accuracy test with relative errors < 2^-102 to
compensate for rounding errors.

  1. Final iteration

If the Ziv's accuracy test from the previous step fails, we perform another
iteration in 128-bit precision and check for exact outputs.

@lntue
Copy link
Contributor Author

lntue commented Jul 17, 2024

@zimmermann6

@llvmbot
Copy link
Member

llvmbot commented Jul 17, 2024

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

Division-less Newton iterations algorithm for cube roots.

  1. Range reduction

For x = (-1)^s * 2^e * (1.m), we get 2 reduced arguments x_r and a as:

  x_r = 1.m
  a   = (-1)^s * 2^(e % 3) * (1.m)

Then cbrt(x) = x^(1/3) can be computed as:

  x^(1/3) = 2^(e / 3) * a^(1/3).

In order to avoid division, we compute a^(-2/3) using Newton method and then
multiply the results by a:

  a^(1/3) = a * a^(-2/3).
  1. First approximation to a^(-2/3)

First, we use a degree-7 minimax polynomial generated by Sollya to
approximate x_r^(-2/3) for 1 &lt;= x_r &lt; 2.

  p = P(x_r) ~ x_r^(-2/3),

with relative errors bounded by:

  | p / x_r^(-2/3) - 1 | &lt; 1.16 * 2^-21.

Then we multiply with 2^(e % 3) from a small lookup table to get:

  x_0 = 2^(-2*(e % 3)/3) * p
      ~ 2^(-2*(e % 3)/3) * x_r^(-2/3)
      = a^(-2/3)

with relative errors:

  | x_0 / a^(-2/3) - 1 | &lt; 1.16 * 2^-21.

This step is done in double precision.

  1. First Newton iteration

We follow the method described in:
Sibidanov, A. and Zimmermann, P., "Correctly rounded cubic root evaluation
in double precision", https://core-math.gitlabpages.inria.fr/cbrt64.pdf
to derive multiplicative Newton iterations as below:
Let x_n be the nth approximation to a^(-2/3). Define the n^th error as:

  h_n = x_n^3 * a^2 - 1

Then:

  a^(-2/3) = x_n / (1 + h_n)^(1/3)
           = x_n * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3 + ...)

using the Taylor series expansion of (1 + h_n)^(-1/3).

Apply to x_0 above:

  h_0 = x_0^3 * a^2 - 1
      = a^2 * (x_0 - a^(-2/3)) * (x_0^2 + x_0 * a^(-2/3) + a^(-4/3)),

it's bounded by:

  |h_0| &lt; 4 * 3 * 1.16 * 2^-21 * 4 &lt; 2^-17.

So in the first iteration step, we use:

  x_1 = x_0 * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3)

Its relative error is bounded by:

  | x_1 / a^(-2/3) - 1 | &lt; 35/242 * |h_0|^4 &lt; 2^-70.

Then we perform Ziv's rounding test and check if the answer is exact.
This step is done in double-double precision.

  1. Second Newton iteration

If the Ziv's rounding test from the previous step fails, we define the error
term:

  h_1 = x_1^3 * a^2 - 1,

And perform another iteration:

  x_2 = x_1 * (1 - h_1 / 3)

with the relative errors exceed the precision of double-double.
We then check the Ziv's accuracy test with relative errors < 2^-102 to
compensate for rounding errors.

  1. Final iteration

If the Ziv's accuracy test from the previous step fails, we perform another
iteration in 128-bit precision and check for exact outputs.


Patch is 24.49 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/99262.diff

15 Files Affected:

  • (modified) libc/config/darwin/arm/entrypoints.txt (+1)
  • (modified) libc/config/linux/aarch64/entrypoints.txt (+1)
  • (modified) libc/config/linux/arm/entrypoints.txt (+1)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+1)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
  • (modified) libc/config/windows/entrypoints.txt (+1)
  • (modified) libc/spec/stdc.td (+1)
  • (modified) libc/src/math/CMakeLists.txt (+1)
  • (added) libc/src/math/cbrt.h (+18)
  • (modified) libc/src/math/generic/CMakeLists.txt (+16)
  • (added) libc/src/math/generic/cbrt.cpp (+340)
  • (modified) libc/test/src/math/CMakeLists.txt (+12)
  • (added) libc/test/src/math/cbrt_test.cpp (+104)
  • (modified) libc/test/src/math/smoke/CMakeLists.txt (+10)
  • (added) libc/test/src/math/smoke/cbrt_test.cpp (+35)
diff --git a/libc/config/darwin/arm/entrypoints.txt b/libc/config/darwin/arm/entrypoints.txt
index 383118dc781e5..32a08f20b328f 100644
--- a/libc/config/darwin/arm/entrypoints.txt
+++ b/libc/config/darwin/arm/entrypoints.txt
@@ -123,6 +123,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.atan2f
     libc.src.math.atanf
     libc.src.math.atanhf
+    libc.src.math.cbrt
     libc.src.math.cbrtf
     libc.src.math.copysign
     libc.src.math.copysignf
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index dee6ac673643e..9b718c3f81151 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -345,6 +345,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.atan2f
     libc.src.math.atanf
     libc.src.math.atanhf
+    libc.src.math.cbrt
     libc.src.math.cbrtf
     libc.src.math.ceil
     libc.src.math.ceilf
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index b0ee0e989b5ed..a72f8668808a5 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -216,6 +216,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.atan2f
     libc.src.math.atanf
     libc.src.math.atanhf
+    libc.src.math.cbrt
     libc.src.math.cbrtf
     libc.src.math.ceil
     libc.src.math.ceilf
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 516a4b6ce3433..266c94d54a9df 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -347,6 +347,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.atan2f
     libc.src.math.atanf
     libc.src.math.atanhf
+    libc.src.math.cbrt
     libc.src.math.cbrtf
     libc.src.math.ceil
     libc.src.math.ceilf
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b6c55e7aa3033..4d19a28f4a2b3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -370,6 +370,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.canonicalize
     libc.src.math.canonicalizef
     libc.src.math.canonicalizel
+    libc.src.math.cbrt
     libc.src.math.cbrtf
     libc.src.math.ceil
     libc.src.math.ceilf
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 499c6bfe3a229..afc9ca87ff094 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -121,6 +121,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.atan2f
     libc.src.math.atanf
     libc.src.math.atanhf
+    libc.src.math.cbrt
     libc.src.math.cbrtf
     libc.src.math.copysign
     libc.src.math.copysignf
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index aa56152aee141..a4c6b40b98388 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -382,6 +382,7 @@ def StdC : StandardSpec<"stdc"> {
       ],
       [], // Enumerations
       [
+          FunctionSpec<"cbrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
           FunctionSpec<"cbrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
 
           FunctionSpec<"copysign", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 6462afbc54a4f..dc2339896f2bb 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -65,6 +65,7 @@ add_math_entrypoint_object(canonicalizel)
 add_math_entrypoint_object(canonicalizef16)
 add_math_entrypoint_object(canonicalizef128)
 
+add_math_entrypoint_object(cbrt)
 add_math_entrypoint_object(cbrtf)
 
 add_math_entrypoint_object(ceil)
diff --git a/libc/src/math/cbrt.h b/libc/src/math/cbrt.h
new file mode 100644
index 0000000000000..a7d5fe80e57b3
--- /dev/null
+++ b/libc/src/math/cbrt.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for cbrt --------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_CBRT_H
+#define LLVM_LIBC_SRC_MATH_CBRT_H
+
+namespace LIBC_NAMESPACE {
+
+double cbrt(double x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_CBRT_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index c2f58fb1a4f71..318728d6e315c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4180,3 +4180,19 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.multiply_add
     libc.src.__support.macros.optimization
 )
+
+add_entrypoint_object(
+  cbrt
+  SRCS
+    cbrt.cpp
+  HDRS
+    ../cbrt.h
+  COMPILE_OPTIONS
+    -O3
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.macros.optimization
+)
diff --git a/libc/src/math/generic/cbrt.cpp b/libc/src/math/generic/cbrt.cpp
new file mode 100644
index 0000000000000..a60e2ea44b6e9
--- /dev/null
+++ b/libc/src/math/generic/cbrt.cpp
@@ -0,0 +1,340 @@
+//===-- Implementation of cbrt function -----------------------------------===//
+//
+// 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/cbrt.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/common.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+#if ((LIBC_MATH & LIBC_MATH_SKIP_ACCURATE_PASS) != 0)
+#define LIBC_MATH_CBRT_SKIP_ACCURATE_PASS
+#endif
+
+namespace LIBC_NAMESPACE_DECL {
+
+using DoubleDouble = fputil::DoubleDouble;
+using Float128 = typename fputil::DyadicFloat<128>;
+
+namespace {
+
+// Initial approximation of x^(-2/3) for 1 <= x < 2.
+// Polynomial generated by Sollya with:
+// > P = fpminimax(x^(-2/3), 7, [|D...|], [1, 2]);
+// > dirtyinfnorm(P/x^(-2/3) - 1, [1, 2]);
+// 0x1.28...p-21
+constexpr double intial_approximation(double x) {
+  constexpr double COEFFS[8] = {
+      0x1.bc52aedead5c6p1,  -0x1.b52bfebf110b3p2,  0x1.1d8d71d53d126p3,
+      -0x1.de2db9e81cf87p2, 0x1.0154ca06153bdp2,   -0x1.5973c66ee6da7p0,
+      0x1.07bf6ac832552p-2, -0x1.5e53d9ce41cb8p-6,
+  };
+
+  double x_sq = x * x;
+
+  double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
+  double c1 = fputil::multiply_add(x, COEFFS[3], COEFFS[2]);
+  double c2 = fputil::multiply_add(x, COEFFS[5], COEFFS[4]);
+  double c3 = fputil::multiply_add(x, COEFFS[7], COEFFS[6]);
+
+  double x_4 = x_sq * x_sq;
+  double d0 = fputil::multiply_add(x_sq, c1, c0);
+  double d1 = fputil::multiply_add(x_sq, c3, c2);
+
+  return fputil::multiply_add(x_4, d1, d0);
+}
+
+// Get the error term for Newton iteration:
+//   h(x) = x^3 * a^2 - 1,
+#ifdef LIBC_TARGET_CPU_HAS_FMA
+constexpr double get_error(const DoubleDouble &x_3, const DoubleDouble &a_sq) {
+  return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +
+         fputil::multiply_add(x_3.lo, a_sq.hi, x_3.hi * a_sq.lo);
+}
+#else
+constexpr double get_error(const DoubleDouble &x_3, const DoubleDouble &a_sq) {
+  DoubleDouble x_3_a_sq = fputil::quick_mult(a_sq, x_3);
+  return (x_3_a_sq.hi - 1.0) + x_3_a_sq.lo;
+}
+#endif
+
+} // anonymous namespace
+
+// Correctly rounded cbrt algorithm:
+//
+// === Step 1 - Range reduction ===
+// For x = (-1)^s * 2^e * (1.m), we get 2 reduced arguments x_r and a as:
+//   x_r = 1.m
+//   a   = (-1)^s * 2^(e % 3) * (1.m)
+// Then cbrt(x) = x^(1/3) can be computed as:
+//   x^(1/3) = 2^(e / 3) * a^(1/3).
+//
+// In order to avoid division, we compute a^(-2/3) using Newton method and then
+// multiply the results by a:
+//   a^(1/3) = a * a^(-2/3).
+//
+// === Step 2 - First approximation to a^(-2/3) ===
+// First, we use a degree-7 minimax polynomial generated by Sollya to
+// approximate x_r^(-2/3) for 1 <= x_r < 2.
+//   p = P(x_r) ~ x_r^(-2/3),
+// with relative errors bounded by:
+//   | p / x_r^(-2/3) - 1 | < 1.16 * 2^-21.
+//
+// Then we multiply with 2^(e % 3) from a small lookup table to get:
+//   x_0 = 2^(-2*(e % 3)/3) * p
+//       ~ 2^(-2*(e % 3)/3) * x_r^(-2/3)
+//       = a^(-2/3)
+// With relative errors:
+//   | x_0 / a^(-2/3) - 1 | < 1.16 * 2^-21.
+// This step is done in double precision.
+//
+// === Step 3 - First Newton iteration ===
+// We follow the method described in:
+//   Sibidanov, A. and Zimmermann, P., "Correctly rounded cubic root evaluation
+//   in double precision", https://core-math.gitlabpages.inria.fr/cbrt64.pdf
+// to derive multiplicative Newton iterations as below:
+// Let x_n be the nth approximation to a^(-2/3).  Define the n^th error as:
+//   h_n = x_n^3 * a^2 - 1
+// Then:
+//   a^(-2/3) = x_n / (1 + h_n)^(1/3)
+//            = x_n * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3 + ...)
+// using the Taylor series expansion of (1 + h_n)^(-1/3).
+//
+// Apply to x_0 above:
+//   h_0 = x_0^3 * a^2 - 1
+//       = a^2 * (x_0 - a^(-2/3)) * (x_0^2 + x_0 * a^(-2/3) + a^(-4/3)),
+// it's bounded by:
+//   |h_0| < 4 * 3 * 1.16 * 2^-21 * 4 < 2^-17.
+// So in the first iteration step, we use:
+//   x_1 = x_0 * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3)
+// Its relative error is bounded by:
+//   | x_1 / a^(-2/3) - 1 | < 35/242 * |h_0|^4 < 2^-70.
+// Then we perform Ziv's rounding test and check if the answer is exact.
+// This step is done in double-double precision.
+//
+// === Step 4 - Second Newton iteration ===
+// If the Ziv's rounding test from the previous step fails, we define the error
+// term:
+//   h_1 = x_1^3 * a^2 - 1,
+// And perform another iteration:
+//   x_2 = x_1 * (1 - h_1 / 3)
+// with the relative errors exceed the precision of double-double.
+// We then check the Ziv's accuracy test with relative errors < 2^-102 to
+// compensate for rounding errors.
+//
+// === Step 5 - Final iteration ===
+// If the Ziv's accuracy test from the previous step fails, we perform another
+// iteration in 128-bit precision and check for exact outputs.
+//
+// TODO: It is possible to replace this costly computation step with special
+// exceptional handling, similar to what was done in the CORE-MATH project:
+// https://gitlab.inria.fr/core-math/core-math/-/blob/master/src/binary64/cbrt/cbrt.c
+
+LLVM_LIBC_FUNCTION(double, cbrt, (double x)) {
+  using FPBits = typename fputil::FPBits<double>;
+
+  uint64_t x_u = FPBits(x).uintval();
+  uint64_t x_abs = x_u & 0x7fff'ffff'ffff'ffff;
+
+  unsigned exp_bias_correction = 682; // 1023 * 2/3
+
+  if (LIBC_UNLIKELY(x_abs < FPBits::min_normal().uintval() ||
+                    x_abs >= FPBits::inf().uintval())) {
+    if (x_abs == 0 || x_abs >= FPBits::inf().uintval())
+      // x is 0, Inf, or NaN.
+      return x;
+
+    // x is non-zero denormal number.
+    // Normalize x.
+    x *= 0x1.0p60;
+    exp_bias_correction -= 20;
+  }
+
+  FPBits x_bits(x);
+
+  // When using biased exponent of x in double precision,
+  //   x_e = real_exponent_of_x + 1023
+  // Then:
+  //   x_e / 3 = real_exponent_of_x / 3 + 1023/3
+  //           = real_exponent_of_x / 3 + 341
+  // So to make it the correct biased exponent of x^(1/3), we add
+  //   1023 - 341 = 682
+  // to the quotient x_e / 3.
+  unsigned x_e = static_cast<unsigned>(x_bits.get_biased_exponent());
+  unsigned out_e = (x_e / 3 + exp_bias_correction);
+  unsigned shift_e = x_e % 3;
+
+  // Set x_r = 1.mantissa
+  double x_r =
+      FPBits(x_bits.get_mantissa() |
+             (static_cast<uint64_t>(FPBits::EXP_BIAS) << FPBits::FRACTION_LEN))
+          .get_val();
+
+  // Set a = (-1)^x_sign * 2^(x_e % 3) * (1.mantissa)
+  uint64_t a_bits = x_bits.uintval() & 0x800F'FFFF'FFFF'FFFF;
+  a_bits |=
+      (static_cast<uint64_t>(shift_e + static_cast<unsigned>(FPBits::EXP_BIAS))
+       << FPBits::FRACTION_LEN);
+  double a = FPBits(a_bits).get_val();
+
+  // Initial approximation of x_r^(-2/3).
+  double p = intial_approximation(x_r);
+
+  // Look up for 2^(-2*n/3) used for first approximation step.
+  constexpr double EXP2_M2_OVER_3[3] = {1.0, 0x1.428a2f98d728bp-1,
+                                        0x1.965fea53d6e3dp-2};
+
+  // x0 is an initial approximation of a^(-2/3) for 1 <= |a| < 8.
+  // Relative error: < 1.16 * 2^(-21).
+  double x0 = static_cast<double>(EXP2_M2_OVER_3[shift_e] * p);
+
+  // First iteration in double precision.
+  DoubleDouble a_sq = fputil::exact_mult(a, a);
+
+  // h0 = x0^3 * a^2 - 1
+  DoubleDouble x0_sq = fputil::exact_mult(x0, x0);
+  DoubleDouble x0_3 = fputil::quick_mult(x0, x0_sq);
+
+  double h0 = get_error(x0_3, a_sq);
+
+#ifdef LIBC_MATH_CBRT_SKIP_ACCURATE_PASS
+  constexpr double REL_ERROR = 0;
+#else
+  constexpr double REL_ERROR = 0x1.0p-51;
+#endif // LIBC_MATH_CBRT_SKIP_ACCURATE_PASS
+
+  // Taylor polynomial of (1 + h)^(-1/3):
+  //   (1 + h)^(-1/3) = 1 - h/3 + 2 h^2 / 9 - 14 h^3 / 81 + ...
+  constexpr double ERR_COEFFS[3] = {
+      -0x1.5555555555555p-2 - REL_ERROR, // -1/3 - relative_error
+      0x1.c71c71c71c71cp-3,              // 2/9
+      -0x1.61f9add3c0ca4p-3,             // -14/81
+  };
+  // e0 = -14 * h^2 / 81 + 2 * h / 9 - 1/3 - relative_error.
+  double e0 = fputil::polyeval(h0, ERR_COEFFS[0], ERR_COEFFS[1], ERR_COEFFS[2]);
+  double x0_h0 = x0 * h0;
+
+  // x1 = x0 (1 - h0/3 + 2 h0^2 / 9 - 14 h0^3 / 81)
+  // x1 approximate a^(-2/3) with relative errors bounded by:
+  //   | x1 / a^(-2/3) - 1 | < (34/243) h0^4 < h0 * REL_ERROR
+  DoubleDouble x1_dd{x0_h0 * e0, x0};
+
+  // r1 = x1 * a ~ a^(-2/3) * a = a^(1/3).
+  DoubleDouble r1 = fputil::quick_mult(a, x1_dd);
+
+  // Lambda function to update the exponent of the result.
+  auto update_exponent = [=](double r) -> double {
+    uint64_t r_m = FPBits(r).uintval() & 0x800F'FFFF'FFFF'FFFF;
+    // Adjust exponent and sign.
+    uint64_t r_bits =
+        r_m | (static_cast<uint64_t>(out_e) << FPBits::FRACTION_LEN);
+    return FPBits(r_bits).get_val();
+  };
+
+#ifdef LIBC_MATH_CBRT_SKIP_ACCURATE_PASS
+  // TODO: We probably don't need to use double-double if accurate tests and
+  // passes are skipped.
+  return update_exponent(r1.hi + r1.lo);
+#else
+  // Accurate checks and passes.
+  double r1_lower = r1.hi + r1.lo;
+  double r1_upper =
+      r1.hi + fputil::multiply_add(x0_h0, 2.0 * REL_ERROR * a, r1.lo);
+
+  // Ziv's accuracy test.
+  if (LIBC_LIKELY(r1_upper == r1_lower)) {
+    // Test for exact outputs.
+    // Check if lower (52 - 17 = 35) bits are 0's.
+    if (LIBC_UNLIKELY((FPBits(r1_lower).uintval() & 0x0000'0007'FFFF'FFFF) ==
+                      0)) {
+      double r1_err = (r1_lower - r1.hi) - r1.lo;
+      if (FPBits(r1_err).abs().get_val() < 0x1.0p69)
+        fputil::clear_except_if_required(FE_INEXACT);
+    }
+
+    return update_exponent(r1_lower);
+  }
+
+  // Accuracy test failed, perform another Newton iteration.
+  double x1 = x1_dd.hi + (e0 + REL_ERROR) * x0_h0;
+
+  // Second iteration in double-double precision.
+  // h1 = x1^3 * a^2 - 1.
+  DoubleDouble x1_sq = fputil::exact_mult(x1, x1);
+  DoubleDouble x1_3 = fputil::quick_mult(x1, x1_sq);
+  double h1 = get_error(x1_3, a_sq);
+
+  // e1 = -x1*h1/3.
+  double e1 = h1 * (x1 * -0x1.5555555555555p-2);
+  // x2 = x1*(1 - h1/3) = x1 + e1 ~ a^(-2/3) with relative errors < 2^-101.
+  DoubleDouble x2 = fputil::exact_add(x1, e1);
+  // r2 = a * x2 ~ a * a^(-2/3) = a^(1/3) with relative errors < 2^-100.
+  DoubleDouble r2 = fputil::quick_mult(a, x2);
+
+  double r2_upper = r2.hi + fputil::multiply_add(a, 0x1.0p-102, r2.lo);
+  double r2_lower = r2.hi + fputil::multiply_add(a, -0x1.0p-102, r2.lo);
+
+  // Ziv's accuracy test.
+  if (LIBC_LIKELY(r2_upper == r2_lower))
+    return update_exponent(r2_upper);
+
+  // TODO: Investigate removing float128 and just list exceptional cases.
+  // Apply another Newton iteration with ~126-bit accuracy.
+  Float128 x2_f128 = fputil::quick_add(Float128(x2.hi), Float128(x2.lo));
+  // x2^3
+  Float128 x2_3 =
+      fputil::quick_mul(fputil::quick_mul(x2_f128, x2_f128), x2_f128);
+  // a^2
+  Float128 a_sq_f128 = fputil::quick_mul(Float128(a), Float128(a));
+  // x2^3 * a^2
+  Float128 x2_3_a_sq = fputil::quick_mul(x2_3, a_sq_f128);
+  // h2 = x2^3 * a^2 - 1
+  Float128 h2_f128 = fputil::quick_add(x2_3_a_sq, Float128(-1.0));
+  double h2 = static_cast<double>(h2_f128);
+  // t2 = 1 - h2 / 3
+  Float128 t2 =
+      fputil::quick_add(Float128(1.0), Float128(h2 * (-0x1.5555555555555p-2)));
+  // x3 = x2 * (1 - h2 / 3) ~ a^(-2/3)
+  Float128 x3 = fputil::quick_mul(x2_f128, t2);
+  // r3 = a * x3 ~ a * a^(-2/3) = a^(1/3)
+  Float128 r3 = fputil::quick_mul(Float128(a), x3);
+
+  // Check for exact cases:
+  Float128::MantissaType rounding_bits =
+      r3.mantissa & 0x0000'0000'0000'03FF'FFFF'FFFF'FFFF'FFFF_u128;
+
+  double result = static_cast<double>(r3);
+  if ((rounding_bits < 0x0000'0000'0000'0000'0000'0000'0000'000F_u128) ||
+      (rounding_bits >= 0x0000'0000'0000'03FF'FFFF'FFFF'FFFF'FFF0_u128)) {
+    // Output is exact.
+    r3.mantissa &= 0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFF0_u128;
+
+    if (rounding_bits >= 0x0000'0000'0000'03FF'FFFF'FFFF'FFFF'FFF0_u128) {
+      Float128 tmp{r3.sign, r3.exponent - 123,
+                   0x8000'0000'0000'0000'0000'0000'0000'0000_u128};
+      Float128 r4 = fputil::quick_add(r3, tmp);
+      result = static_cast<double>(r4);
+    } else {
+      result = static_cast<double>(r3);
+    }
+
+    fputil::clear_except_if_required(FE_INEXACT);
+  }
+
+  return update_exponent(result);
+#endif // LIBC_MATH_CBRT_SKIP_ACCURATE_PASS
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 0dc7ae6aae2df..64b4d2c58fb6a 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2225,6 +2225,18 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  cbrt_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    cbrt_test.cpp
+  DEPENDS
+    libc.src.math.cbrt
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_subdirectory(generic)
 add_subdirectory(smoke)
 
diff --git a/libc/test/src/math/cbrt_test.cpp b/libc/test/src/math/cbrt_test.cpp
new file mode 100644
index 0000000000000..123351496118b
--- /dev/null
+++ b/libc/test/src/math/cbrt_test.cpp
@@ -0,0 +1,104 @@
+//===-- Unittests for cbrt ------------------------------------------------===//
+//
+// 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 "hdr/math_macros.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/cbrt.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcCbrtTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+using LIBC_NAMESPACE::testing::tlog;
+
+TEST_F(LlvmLibcCbrtTest, InDoubleRange) {
+  constexpr uint64_t COUNT = 123'451;
+  uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(1.0).uintval();
+  uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(8.0).uintval();
+  uint64_t STEP = (STOP - START) / COUNT;
+
+  auto test = [&](mpfr::RoundingMode rounding_mode) {
+    mpfr::ForceRoundingMode force_rounding(rounding_mode);
+    if (!force_rounding.success)
+      return;
+
+    uint64_t fails = 0;
+    uint64_t tested = 0;
+    uint64_t total = 0;
+    double worst_input, worst_output = 0.0;
+    double ulp = 0.5;
+
+    for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
+      double x = FPBits(v).get_val();
+      if (isnan(x) || isinf(x))
+        continue;
+
+      double result = LIB...
[truncated]

@zimmermann6
Copy link

all tests are ok on my side

@lntue lntue requested a review from petrhosek July 17, 2024 12:04
Comment on lines +39 to +43
constexpr double COEFFS[8] = {
0x1.bc52aedead5c6p1, -0x1.b52bfebf110b3p2, 0x1.1d8d71d53d126p3,
-0x1.de2db9e81cf87p2, 0x1.0154ca06153bdp2, -0x1.5973c66ee6da7p0,
0x1.07bf6ac832552p-2, -0x1.5e53d9ce41cb8p-6,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a policy on when to use C-style arrays, and when to use cpp::array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have one.

@lntue lntue requested a review from overmighty July 17, 2024 16:08
@lntue lntue merged commit 7fc9fb9 into llvm:main Jul 17, 2024
@lntue lntue deleted the cbrt branch July 17, 2024 16:23
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 17, 2024

LLVM Buildbot has detected a new failure on builder libc-aarch64-ubuntu-dbg running on libc-aarch64-ubuntu while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/104/builds/2394

Here is the relevant piece of the build log for the reference:

Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
-- Performing Test HAVE_POSIX_REGEX -- success
-- Performing Test HAVE_STEADY_CLOCK -- success
-- Performing Test HAVE_PTHREAD_AFFINITY -- failed to compile
-- Configuring done
-- Generating done
-- Build files have been written to: /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/build
@@@BUILD_STEP build libc@@@
Running: ninja libc
[1/2] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o 
/usr/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_18_0_0_git -D_DEBUG -Iprojects/libc/src/math/generic -I/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/src/math/generic -I/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc -isystem projects/libc/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -O3 -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -c /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/src/math/generic/cbrt.cpp
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/src/math/generic/cbrt.cpp:38:18: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr double intial_approximation(double x) {
                 ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/src/math/generic/cbrt.cpp:47:15: note: non-constexpr function 'multiply_add' cannot be used in a constant expression
  double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
              ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: declared here
LIBC_INLINE double multiply_add(double x, double y, double z) {
                   ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/src/math/generic/cbrt.cpp:62:18: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr double get_error(const DoubleDouble &x_3, const DoubleDouble &a_sq) {
                 ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/src/math/generic/cbrt.cpp:63:10: note: non-constexpr function 'multiply_add' cannot be used in a constant expression
  return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +
         ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: declared here
LIBC_INLINE double multiply_add(double x, double y, double z) {
                   ^
2 errors generated.
ninja: build stopped: subcommand failed.
['ninja', 'libc'] exited with return code 1.
The build step threw an exception...
Traceback (most recent call last):
  File "../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 164, in step
    yield
  File "../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 121, in main
    run_command(['ninja', 'libc'])
  File "../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 179, in run_command
    util.report_run_cmd(cmd, cwd=directory)
  File "/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu/llvm-zorg/zorg/buildbot/builders/annotated/util.py", line 49, in report_run_cmd
    subprocess.check_call(cmd, shell=shell, *args, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 190, in check_call
    raise CalledProcessError(retcode, cmd)
CalledProcessError: Command '['ninja', 'libc']' returned non-zero exit status 1
@@@STEP_FAILURE@@@
@@@BUILD_STEP libc-unit-tests@@@
Running: ninja libc-unit-tests

@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 17, 2024

LLVM Buildbot has detected a new failure on builder libc-aarch64-ubuntu-fullbuild-dbg running on libc-aarch64-ubuntu while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/71/builds/2383

Here is the relevant piece of the build log for the reference:

Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
[147/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterf16.dir/nextafterf16.cpp.o
[148/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterl.__internal__.dir/nextafterl.cpp.o
[149/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterf.__internal__.dir/nextafterf.cpp.o
[150/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterf.dir/nextafterf.cpp.o
[151/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafter.dir/nextafter.cpp.o
[152/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nexttoward.__internal__.dir/nexttoward.cpp.o
[153/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fmodl.__internal__.dir/fmodl.cpp.o
[154/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafter.__internal__.dir/nextafter.cpp.o
[155/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fromfpxf128.__internal__.dir/fromfpxf128.cpp.o
[156/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o 
/usr/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -Iprojects/libc/src/math/generic -I/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic -I/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc -isystem projects/libc/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -ffreestanding -DLIBC_FULL_BUILD -nostdlibinc -idirafter/usr/include -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -O3 -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -c /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:38:18: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr double intial_approximation(double x) {
                 ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:47:15: note: non-constexpr function 'multiply_add' cannot be used in a constant expression
  double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
              ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: declared here
LIBC_INLINE double multiply_add(double x, double y, double z) {
                   ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:62:18: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr double get_error(const DoubleDouble &x_3, const DoubleDouble &a_sq) {
                 ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:63:10: note: non-constexpr function 'multiply_add' cannot be used in a constant expression
  return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +
         ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: declared here
LIBC_INLINE double multiply_add(double x, double y, double z) {
                   ^
2 errors generated.
[157/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fromfpf128.dir/fromfpf128.cpp.o
[158/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.rint.__NO_ROUND_OPT.__internal__.dir/rint.cpp.o
[159/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.copysignf.__internal__.dir/copysignf.cpp.o
[160/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fmax.dir/fmax.cpp.o
[161/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nanf128.dir/nanf128.cpp.o
[162/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.llroundf128.dir/llroundf128.cpp.o
[163/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fdim.__internal__.dir/fdim.cpp.o
[164/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nanf16.dir/nanf16.cpp.o
[165/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterl.dir/nextafterl.cpp.o
[166/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fdimf128.__internal__.dir/fdimf128.cpp.o
[167/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nan.dir/nan.cpp.o
[168/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.hypot.dir/hypot.cpp.o
[169/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.hypot.__internal__.dir/hypot.cpp.o
[170/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cospif.__internal__.dir/cospif.cpp.o
[171/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.hypotf.__internal__.dir/hypotf.cpp.o
[172/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.acosf.__internal__.dir/acosf.cpp.o
[173/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.f16div.__internal__.dir/f16div.cpp.o
ninja: build stopped: subcommand failed.
Step 8 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[147/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterf16.dir/nextafterf16.cpp.o
[148/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterl.__internal__.dir/nextafterl.cpp.o
[149/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterf.__internal__.dir/nextafterf.cpp.o
[150/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterf.dir/nextafterf.cpp.o
[151/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafter.dir/nextafter.cpp.o
[152/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nexttoward.__internal__.dir/nexttoward.cpp.o
[153/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fmodl.__internal__.dir/fmodl.cpp.o
[154/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafter.__internal__.dir/nextafter.cpp.o
[155/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fromfpxf128.__internal__.dir/fromfpxf128.cpp.o
[156/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o 
/usr/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -Iprojects/libc/src/math/generic -I/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic -I/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc -isystem projects/libc/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -ffreestanding -DLIBC_FULL_BUILD -nostdlibinc -idirafter/usr/include -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -O3 -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -c /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:38:18: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr double intial_approximation(double x) {
                 ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:47:15: note: non-constexpr function 'multiply_add' cannot be used in a constant expression
  double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
              ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: declared here
LIBC_INLINE double multiply_add(double x, double y, double z) {
                   ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:62:18: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr double get_error(const DoubleDouble &x_3, const DoubleDouble &a_sq) {
                 ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:63:10: note: non-constexpr function 'multiply_add' cannot be used in a constant expression
  return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +
         ^
/home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: declared here
LIBC_INLINE double multiply_add(double x, double y, double z) {
                   ^
2 errors generated.
[157/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fromfpf128.dir/fromfpf128.cpp.o
[158/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.rint.__NO_ROUND_OPT.__internal__.dir/rint.cpp.o
[159/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.copysignf.__internal__.dir/copysignf.cpp.o
[160/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fmax.dir/fmax.cpp.o
[161/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nanf128.dir/nanf128.cpp.o
[162/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.llroundf128.dir/llroundf128.cpp.o
[163/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fdim.__internal__.dir/fdim.cpp.o
[164/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nanf16.dir/nanf16.cpp.o
[165/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nextafterl.dir/nextafterl.cpp.o
[166/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fdimf128.__internal__.dir/fdimf128.cpp.o
[167/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.nan.dir/nan.cpp.o
[168/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.hypot.dir/hypot.cpp.o
[169/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.hypot.__internal__.dir/hypot.cpp.o
[170/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cospif.__internal__.dir/cospif.cpp.o
[171/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.hypotf.__internal__.dir/hypotf.cpp.o
[172/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.acosf.__internal__.dir/acosf.cpp.o
[173/3105] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.f16div.__internal__.dir/f16div.cpp.o
ninja: build stopped: subcommand failed.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 17, 2024

LLVM Buildbot has detected a new failure on builder libc-x86_64-debian-gcc-fullbuild-dbg running on libc-x86_64-debian-fullbuild while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/131/builds/2342

Here is the relevant piece of the build log for the reference:

Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
[763/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fma.__internal__.dir/fma.cpp.o
[764/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fmaf.dir/fmaf.cpp.o
[765/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fma.dir/fma.cpp.o
[766/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.scalbnf128.dir/scalbnf128.cpp.o
[767/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_uc.__internal__.dir/stdc_leading_zeros_uc.cpp.o
[768/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrtf.__NO_FMA_OPT.__internal__.dir/cbrtf.cpp.o
[769/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_us.__internal__.dir/stdc_leading_zeros_us.cpp.o
[770/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrtf.__internal__.dir/cbrtf.cpp.o
[771/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_ui.__internal__.dir/stdc_leading_zeros_ui.cpp.o
[772/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o 
/usr/bin/g++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/src/math/generic -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc -isystem /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -mavx2 -mfma -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -ffreestanding -DLIBC_FULL_BUILD -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -O3 -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o -c /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp: In function ‘constexpr double __llvm_libc_19_0_0_git::{anonymous}::intial_approximation(double)’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:47:35: error: call to non-‘constexpr’ function ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’
   47 |   double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
      |               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/PolyEval.h:12,
                 from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:13:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’ declared here
   53 | LIBC_INLINE double multiply_add(double x, double y, double z) {
      |                    ^~~~~~~~~~~~
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp: In function ‘constexpr double __llvm_libc_19_0_0_git::{anonymous}::get_error(const __llvm_libc_19_0_0_git::DoubleDouble&, const __llvm_libc_19_0_0_git::DoubleDouble&)’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:63:30: error: call to non-‘constexpr’ function ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’
   63 |   return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +
      |          ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’ declared here
   53 | LIBC_INLINE double multiply_add(double x, double y, double z) {
      |                    ^~~~~~~~~~~~
[773/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_ul.__internal__.dir/stdc_leading_zeros_ul.cpp.o
[774/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.atanhf.__NO_ROUND_OPT.__internal__.dir/atanhf.cpp.o
[775/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_ull.__internal__.dir/stdc_leading_zeros_ull.cpp.o
[776/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrtf.__NO_FMA_OPT.dir/cbrtf.cpp.o
[777/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_ones_uc.__internal__.dir/stdc_leading_ones_uc.cpp.o
[778/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_ones_ui.__internal__.dir/stdc_leading_ones_ui.cpp.o
[779/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o 
/usr/bin/g++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/src/math/generic -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc -isystem /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -mavx2 -mfma -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -ffreestanding -DLIBC_FULL_BUILD -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -O3 -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -c /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp: In function ‘constexpr double __llvm_libc_19_0_0_git::{anonymous}::intial_approximation(double)’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:47:35: error: call to non-‘constexpr’ function ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’
   47 |   double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
      |               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/PolyEval.h:12,
                 from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:13:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’ declared here
   53 | LIBC_INLINE double multiply_add(double x, double y, double z) {
      |                    ^~~~~~~~~~~~
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp: In function ‘constexpr double __llvm_libc_19_0_0_git::{anonymous}::get_error(const __llvm_libc_19_0_0_git::DoubleDouble&, const __llvm_libc_19_0_0_git::DoubleDouble&)’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:63:30: error: call to non-‘constexpr’ function ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’
   63 |   return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +
Step 8 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[763/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fma.__internal__.dir/fma.cpp.o
[764/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fmaf.dir/fmaf.cpp.o
[765/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.fma.dir/fma.cpp.o
[766/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.scalbnf128.dir/scalbnf128.cpp.o
[767/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_uc.__internal__.dir/stdc_leading_zeros_uc.cpp.o
[768/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrtf.__NO_FMA_OPT.__internal__.dir/cbrtf.cpp.o
[769/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_us.__internal__.dir/stdc_leading_zeros_us.cpp.o
[770/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrtf.__internal__.dir/cbrtf.cpp.o
[771/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_ui.__internal__.dir/stdc_leading_zeros_ui.cpp.o
[772/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o 
/usr/bin/g++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/src/math/generic -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc -isystem /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -mavx2 -mfma -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -ffreestanding -DLIBC_FULL_BUILD -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -O3 -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.__internal__.dir/cbrt.cpp.o -c /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp: In function ‘constexpr double __llvm_libc_19_0_0_git::{anonymous}::intial_approximation(double)’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:47:35: error: call to non-‘constexpr’ function ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’
   47 |   double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
      |               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/PolyEval.h:12,
                 from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:13:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’ declared here
   53 | LIBC_INLINE double multiply_add(double x, double y, double z) {
      |                    ^~~~~~~~~~~~
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp: In function ‘constexpr double __llvm_libc_19_0_0_git::{anonymous}::get_error(const __llvm_libc_19_0_0_git::DoubleDouble&, const __llvm_libc_19_0_0_git::DoubleDouble&)’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:63:30: error: call to non-‘constexpr’ function ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’
   63 |   return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +
      |          ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’ declared here
   53 | LIBC_INLINE double multiply_add(double x, double y, double z) {
      |                    ^~~~~~~~~~~~
[773/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_ul.__internal__.dir/stdc_leading_zeros_ul.cpp.o
[774/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.atanhf.__NO_ROUND_OPT.__internal__.dir/atanhf.cpp.o
[775/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_zeros_ull.__internal__.dir/stdc_leading_zeros_ull.cpp.o
[776/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrtf.__NO_FMA_OPT.dir/cbrtf.cpp.o
[777/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_ones_uc.__internal__.dir/stdc_leading_ones_uc.cpp.o
[778/3763] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_leading_ones_ui.__internal__.dir/stdc_leading_ones_ui.cpp.o
[779/3763] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o 
/usr/bin/g++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/src/math/generic -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc -isystem /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -mavx2 -mfma -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -ffreestanding -DLIBC_FULL_BUILD -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -O3 -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -c /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp: In function ‘constexpr double __llvm_libc_19_0_0_git::{anonymous}::intial_approximation(double)’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:47:35: error: call to non-‘constexpr’ function ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’
   47 |   double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
      |               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/PolyEval.h:12,
                 from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:13:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:53:20: note: ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’ declared here
   53 | LIBC_INLINE double multiply_add(double x, double y, double z) {
      |                    ^~~~~~~~~~~~
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp: In function ‘constexpr double __llvm_libc_19_0_0_git::{anonymous}::get_error(const __llvm_libc_19_0_0_git::DoubleDouble&, const __llvm_libc_19_0_0_git::DoubleDouble&)’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:63:30: error: call to non-‘constexpr’ function ‘double __llvm_libc_19_0_0_git::fputil::multiply_add(double, double, double)’
   63 |   return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +

@lntue lntue mentioned this pull request Jul 17, 2024
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 17, 2024

LLVM Buildbot has detected a new failure on builder libc-arm32-debian-dbg running on libc-arm32-debian while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/182/builds/1172

Here is the relevant piece of the build log for the reference:

Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
-- Performing Test HAVE_POSIX_REGEX -- success
-- Performing Test HAVE_STEADY_CLOCK -- success
-- Performing Test HAVE_PTHREAD_AFFINITY -- success
-- Configuring done
-- Generating done
-- Build files have been written to: /llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build
@@@BUILD_STEP build libc@@@
Running: ninja libc
[1/2] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o 
/usr/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -I/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/projects/libc/src/math/generic -I/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic -I/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc -isystem /llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/projects/libc/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -O3 -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -c /llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic/cbrt.cpp
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:38:18: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr double intial_approximation(double x) {
                 ^
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:47:15: note: non-constexpr function 'multiply_add<double>' cannot be used in a constant expression
  double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
              ^
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:27:1: note: declared here
multiply_add(const T &x, const T &y, const T &z) {
^
1 error generated.
ninja: build stopped: subcommand failed.
['ninja', 'libc'] exited with return code 1.
The build step threw an exception...
Traceback (most recent call last):
  File "/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 164, in step
    yield
  File "/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 121, in main
    run_command(['ninja', 'libc'])
  File "/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 179, in run_command
    util.report_run_cmd(cmd, cwd=directory)
  File "/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-zorg/zorg/buildbot/builders/annotated/util.py", line 49, in report_run_cmd
    subprocess.check_call(cmd, shell=shell, *args, **kwargs)
  File "/usr/lib/python3.11/subprocess.py", line 413, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['ninja', 'libc']' returned non-zero exit status 1.
@@@STEP_FAILURE@@@
@@@BUILD_STEP libc-unit-tests@@@
Running: ninja libc-unit-tests
Step 6 (build libc) failure: build libc (failure)
@@@BUILD_STEP build libc@@@
Running: ninja libc
[1/2] Building CXX object projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o
FAILED: projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o 
/usr/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -I/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/projects/libc/src/math/generic -I/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic -I/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc -isystem /llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/projects/libc/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -fpie -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -O3 -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -MF projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o.d -o projects/libc/src/math/generic/CMakeFiles/libc.src.math.generic.cbrt.dir/cbrt.cpp.o -c /llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic/cbrt.cpp
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:38:18: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr double intial_approximation(double x) {
                 ^
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic/cbrt.cpp:47:15: note: non-constexpr function 'multiply_add<double>' cannot be used in a constant expression
  double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
              ^
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/__support/FPUtil/multiply_add.h:27:1: note: declared here
multiply_add(const T &x, const T &y, const T &z) {
^
1 error generated.
ninja: build stopped: subcommand failed.
['ninja', 'libc'] exited with return code 1.
The build step threw an exception...
Traceback (most recent call last):
  File "/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 164, in step
    yield
  File "/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 121, in main
    run_command(['ninja', 'libc'])
  File "/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/build/../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py", line 179, in run_command
    util.report_run_cmd(cmd, cwd=directory)
  File "/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-zorg/zorg/buildbot/builders/annotated/util.py", line 49, in report_run_cmd
    subprocess.check_call(cmd, shell=shell, *args, **kwargs)
  File "/usr/lib/python3.11/subprocess.py", line 413, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['ninja', 'libc']' returned non-zero exit status 1.

petrhosek added a commit to petrhosek/llvm-project that referenced this pull request Jul 22, 2024
petrhosek added a commit that referenced this pull request Jul 22, 2024
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
… rounding modes. (#99262)

Division-less Newton iterations algorithm for cube roots.

1. **Range reduction**

For `x = (-1)^s * 2^e * (1.m)`, we get 2 reduced arguments `x_r` and `a`
as:
```
  x_r = 1.m
  a   = (-1)^s * 2^(e % 3) * (1.m)
```
Then `cbrt(x) = x^(1/3)` can be computed as:
```
  x^(1/3) = 2^(e / 3) * a^(1/3).
```

In order to avoid division, we compute `a^(-2/3)` using Newton method
and then
multiply the results by a:
```
  a^(1/3) = a * a^(-2/3).
```

2. **First approximation to a^(-2/3)**

First, we use a degree-7 minimax polynomial generated by Sollya to
approximate `x_r^(-2/3)` for `1 <= x_r < 2`.
```
  p = P(x_r) ~ x_r^(-2/3),
```
with relative errors bounded by:
```
  | p / x_r^(-2/3) - 1 | < 1.16 * 2^-21.
```

Then we multiply with `2^(e % 3)` from a small lookup table to get:
```
  x_0 = 2^(-2*(e % 3)/3) * p
      ~ 2^(-2*(e % 3)/3) * x_r^(-2/3)
      = a^(-2/3)
```
with relative errors:
```
  | x_0 / a^(-2/3) - 1 | < 1.16 * 2^-21.
```
This step is done in double precision.

3. **First Newton iteration**

We follow the method described in:
Sibidanov, A. and Zimmermann, P., "Correctly rounded cubic root
evaluation
in double precision", https://core-math.gitlabpages.inria.fr/cbrt64.pdf
to derive multiplicative Newton iterations as below:
Let `x_n` be the nth approximation to `a^(-2/3)`. Define the n^th error
as:
```
  h_n = x_n^3 * a^2 - 1
```
Then:
```
  a^(-2/3) = x_n / (1 + h_n)^(1/3)
           = x_n * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3 + ...)
```
using the Taylor series expansion of `(1 + h_n)^(-1/3)`.

Apply to `x_0` above:
```
  h_0 = x_0^3 * a^2 - 1
      = a^2 * (x_0 - a^(-2/3)) * (x_0^2 + x_0 * a^(-2/3) + a^(-4/3)),
```
it's bounded by:
```
  |h_0| < 4 * 3 * 1.16 * 2^-21 * 4 < 2^-17.
```
So in the first iteration step, we use:
```
  x_1 = x_0 * (1 - (1/3) * h_n + (2/9) * h_n^2 - (14/81) * h_n^3)
```
Its relative error is bounded by:
```
  | x_1 / a^(-2/3) - 1 | < 35/242 * |h_0|^4 < 2^-70.
```
Then we perform Ziv's rounding test and check if the answer is exact.
This step is done in double-double precision.

4. **Second Newton iteration**

If the Ziv's rounding test from the previous step fails, we define the
error
term:
```
  h_1 = x_1^3 * a^2 - 1,
```
And perform another iteration:
```
  x_2 = x_1 * (1 - h_1 / 3)
```
with the relative errors exceed the precision of double-double.
We then check the Ziv's accuracy test with relative errors < 2^-102 to
compensate for rounding errors.

5. **Final iteration**
 
If the Ziv's accuracy test from the previous step fails, we perform
another
iteration in 128-bit precision and check for exact outputs.
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants