ILGPU icon indicating copy to clipboard operation
ILGPU copied to clipboard

Added XMath Cube Root Functionality

Open MPSQUARK opened this issue 2 years ago • 1 comments

I have added 2 functions into ILGPU.Algorithms.XMath;

  • Cbrt
  • CbrtFast

Cbrt : Can calculate the cube root of an integer/float/double to double-precision accuracy as such double = Cbrt(int) double = Cbrt(float) double = Cbrt(double)

CbrtFast : Can calculate the cube root of an integer/float/double to single-precision accuracy as such float = CbrtFast(int) float = CbrtFast(float) float = CbrtFast(double)

Both methods work for;

  • Negative values
  • Zero
  • Positive values
  • NaN's
  • Positive & Negative Infinities

I have also added a test file to test both Cbrt and CbrtFast in the Algorithms.Tests.XMathTests to verify both functions work as intended

TODO : Would be nice if Math.Cbrt could be mapped to XMath.Cbrt and MathF.Cbrt to XMath.CbrtFast :) TODO : OpenCL unit tests need to be run (I don't have the hardware to do that), cuda and cpu have passed

MPSQUARK avatar Sep 05 '21 15:09 MPSQUARK

Regarding your queries @m4rs-mt General Question: No sources to cite. The Newtonian/Halley iteration methods are common knowledge. And the bit manipulation was something I figured out myself when I found out that I can change a float to int without modifying the binary representation. The only actual online info used was information regarding the IEEE-754 format to know how many bits made up the 'decimal'.

As for Chebyshev polynomials, I think it's worth me looking into it just in case. But I think this method is faster, but then I am not sure of how Chebyshev polynomials would be used to solve this particular problem. my logic is that the initial guess in this cbrt code uses bitshifting which is very fast, 1 multiply, an add and subtract, only slow part is getting the precision but I think XMath.Rcp could aid that, whereas I think that a solution involving polynomials may end up needing more clock cycles to complete.

MPSQUARK avatar Sep 21 '21 21:09 MPSQUARK

@MPSQUARK closing this for now due to inactivity.

m4rs-mt avatar Aug 21 '23 15:08 m4rs-mt