Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • 1
    $\begingroup$ In your Godbolt compiler explorer link, your function has if(n==0) return 0; (not return 1 like in your question). x^0 = 1, so that's a bug. Not that it matters for the rest of the question, though; the iterative asm checks for that special case first. But strangely, the iterative implementation introduces a multiply of 1 * x that wasn't present in the source, even if we make a float version. gcc.godbolt.org/z/eqwine (and gcc only succeeds with -ffast-math.) $\endgroup$ Commented Mar 16, 2019 at 10:01
  • $\begingroup$ @PeterCordes Good catch. The return 0 has been fixed. The multiplication by 1 is interesting. I'm not sure what to make of it. $\endgroup$ Commented Mar 16, 2019 at 18:06
  • $\begingroup$ I think it's a side-effect of the way GCC transforms when turning it into a loop. Clearly gcc has some missed optimizations here, e.g. missing it for float without -ffast-math, even though it's the same value being multiplied every time. (Except for the 1.0f` which might be the sticking point?) $\endgroup$ Commented Mar 16, 2019 at 18:11