[llvm] [APFloat] add power (PR #122889)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 07:03:29 PST 2025


================
@@ -1536,6 +1536,27 @@ inline APFloat abs(APFloat X) {
   return X;
 }
 
+/// Returns X^N for N >= 0.
+/// Returns X^N for N >= 0.
+inline APFloat pow(const APFloat &X, const int &N) {
+  assert(N >= 0 && "negative exponents not supported.");
+  APFloat Acc = APFloat::getOne(X.getSemantics());
+  if (N == 0) {
+    return APFloat::getOne(X.getSemantics());
+  }
+  APFloat Base = X;
+  int64_t RemainingExponent = N;
+  while (RemainingExponent > 0) {
+    while (RemainingExponent % 2 == 0) {
+      Base = Base * Base;
+      RemainingExponent /= 2;
+    }
+    --RemainingExponent;
+    Acc = Acc * Base;
----------------
kuhar wrote:

I'm not a FP expert, but to me it does seem like a partial accumulation that may affect the numerics. Does this implementation conform to some standard IEEE / libc definition of `pown`?

https://github.com/llvm/llvm-project/pull/122889


More information about the llvm-commits mailing list