Ignore:
Timestamp:
Apr 9, 2009, 8:42:12 AM (15 years ago)
Author:
ole
Message:

Added code for optimised squareroot calculations. It is not enabled (yet).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/shallow_water/shallow_water_ext.c

    r6737 r6778  
    190190}
    191191
     192
     193// Optimised squareroot computation
     194//
     195//See
     196//http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf
     197//and http://mail.opensolaris.org/pipermail/tools-gcc/2005-August/000047.html
     198float fast_squareroot_approximation(float number) {
     199  float x;
     200  const float f = 1.5;
     201
     202  // Allow i and y to occupy the same memory
     203  union
     204  {
     205    long i;
     206    float y;
     207  } u;
     208 
     209  // Good initial guess 
     210  x = number * 0.5; 
     211  u.y  = number;
     212  u.i  = 0x5f3759df - ( u.i >> 1 );
     213 
     214  // Take a few iterations
     215  u.y  = u.y * ( f - ( x * u.y * u.y ) );
     216  u.y  = u.y * ( f - ( x * u.y * u.y ) );
     217   
     218  return number * u.y;
     219}
     220
     221
     222
     223// Optimised squareroot computation (double version, slower)
     224double Xfast_squareroot_approximation(double number) {
     225  double x;
     226  const double f = 1.5;
     227   
     228  // Allow i and y to occupy the same memory   
     229  union
     230  {
     231    long long i;
     232    double y;
     233  } u;
     234
     235  // Good initial guess
     236  x = number * 0.5;
     237  u.y  = number;
     238  u.i  = 0x5fe6ec85e7de30daLL - ( u.i >> 1 );
     239 
     240  // Take a few iterations 
     241  u.y  = u.y * ( f - ( x * u.y * u.y ) );
     242  u.y  = u.y * ( f - ( x * u.y * u.y ) );
     243
     244  return number * u.y;
     245}
     246
     247
    192248// Innermost flux function (using stage w=z+h)
    193249int _flux_function_central(double *q_left, double *q_right,
     
    262318  // Maximal and minimal wave speeds
    263319  soundspeed_left  = sqrt(g*h_left);
    264   soundspeed_right = sqrt(g*h_right);
     320  soundspeed_right = sqrt(g*h_right); 
     321 
     322  // Code to use fast square root optimisation if desired.
     323  //soundspeed_left  = fast_squareroot_approximation(g*h_left);
     324  //soundspeed_right = fast_squareroot_approximation(g*h_right);
    265325
    266326  s_max = max(u_left + soundspeed_left, u_right + soundspeed_right);
Note: See TracChangeset for help on using the changeset viewer.