Advertisement
Geometrian

ℓRGB -> sRGB Quintic Conversion (Draft!)

Oct 27th, 2022 (edited)
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | Science | 0 0
  1. [[nodiscard]] inline static float _helper(float a) noexcept {
  2.     constexpr int32_t P0=0x44c3cb8c, P1=0x10, P2=0x1554; //Magic!
  3.     constexpr float
  4.         s     =  0x1.0e153cp+0f , // ≈  1.055
  5.         BIAS  = -0x1.c28efp-5f  , // ≈ -0.055
  6.         sA    =  0x1.68f75ap-4f , // ≈  s(1/n), (note n=12)
  7.         sB    =  0x1.863818p-5f , // ≈  s(1+n)/(2n²)
  8.         sC    =  0x1.0e774cp-5f , // ≈  s(1+n)(1+2n)/(6n³)
  9.         sD    =  0x1.a429e8p-6f , // ≈  s(1+n)(1+2n)(1+3n)/(24n⁴)
  10.         ONE   =  0x1.0000ccp+0f , // ≈  1
  11.         TWEAK =  0x1.6cb8cap-16f  // ≈  0
  12.     ;
  13.     float x = std::bit_cast<float>(
  14.         P0 - (std::bit_cast<int32_t>(a)>>P1)*P2
  15.     );
  16.     float sx = s * x;
  17.     float sAx = sA * x;
  18.     float x2 = x * x;
  19.     float sqrta = std::sqrt(a);
  20.     float x4 = x2 * x2;
  21.     float ax4 = x4 * a;
  22.     float x8 = x4 * x4;
  23.     float y = std::fma( -ax4,x8, ONE );
  24.     float xy = std::fma( x,y, TWEAK );
  25.     float y2 = y * y;
  26.     float sx_sAxy = std::fma( sAx,y, sx );
  27.     float sC_sDy = std::fma( sD,y, sC );
  28.     float sBy = sB * y;
  29.     float sBy_sCy2_sDy3 = std::fma( sC_sDy,y2, sBy );
  30.     float sx_sAxy_sBxy2_sCxy3_sDxy4 = std::fma(
  31.         sBy_sCy2_sDy3,xy, sx_sAxy
  32.     );
  33.     return std::fma( sqrta,sx_sAxy_sBxy2_sCxy3_sDxy4, BIAS );
  34. }
  35. [[nodiscard]] constexpr static float srgb_invgamma(float val) noexcept {
  36.     return val > 0.0031308f ? _helper(val) : 12.92f*val;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement