Advertisement
WarPie90

Untitled

Jan 9th, 2025
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.93 KB | None | 0 0
  1. (*
  2.  * ====================================================
  3.  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4.  *
  5.  * Developed at SunPro, a Sun Microsystems, Inc. business.
  6.  * Permission to use, copy, modify, and distribute this
  7.  * software is freely granted, provided that this notice
  8.  * is preserved.
  9.  * ====================================================
  10.  *
  11.  * Optimized by Bruce D. Evans.
  12.  *
  13.  * Ported to pascal for single datatype by Jarl <slacky> Holta
  14.  *)
  15.  
  16. const
  17.   B1: UInt32 = 709958130; // (127-127/3-0.03306235651)*2^23
  18.   B2: UInt32 = 696219795; // (127-127/3-54/3-0.03306235651)*2^23
  19.  
  20.   P0: Single =  1.87595182427177;  // Polynomial coefficients (single precision)
  21.   P1: Single = -1.88497979543377;
  22.   P2: Single =  1.62142972010535;
  23.   P3: Single = -0.75839793477877;
  24.   P4: Single =  0.14599619288661;
  25.  
  26. function cbrt(x: Single): Single; inline;
  27. var
  28.   hx, sign, approx: UInt32;
  29.   t, r, s, w: Single;
  30.   u: record
  31.     case Boolean of
  32.       True: (value: Single);
  33.       False: (bits: UInt32);
  34.   end;
  35. begin
  36.   u.value := x;
  37.   hx := u.bits;
  38.  
  39.   sign := hx and UInt32($80000000);
  40.   hx   := hx and UInt32($7FFFFFFF);
  41.  
  42.   if hx >= $7F800000 then Exit(x + x);
  43.   if hx = 0 then          Exit(x);
  44.  
  45.   if hx < $00800000 then
  46.   begin
  47.     u.value := x * Single(1.6777216e+7);
  48.     approx := u.bits;
  49.     approx := approx div 3 + B2;
  50.     u.bits := approx;
  51.     t := u.value * Single(5.9604645e-8);
  52.   end
  53.   else
  54.   begin
  55.     // Compute the initial approximation for normal numbers
  56.     approx := hx div 3 + B1;
  57.     u.bits := sign or approx; // Combine sign and approximation
  58.     t := u.value;
  59.   end;
  60.  
  61.   r := (t * t) * (t / x);
  62.   t := t * ((P0 + r * (P1 + r * P2)) + ((r * r) * r) * (P3 + r * P4));
  63.  
  64.   u.value := t;
  65.   u.bits := (u.bits + UInt32($00400000)) and UInt32($FF800000);
  66.   t := u.value;
  67.  
  68.   s := t * t;
  69.   r := x / s;
  70.   w := t + t;
  71.   r := (r - t) / (w + r);
  72.   t := t + t * r;
  73.  
  74.   Result := t;
  75. end;  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement