Bug 45272 - comment about sqrt() implementation wrong: it is not from Carmack
Summary: comment about sqrt() implementation wrong: it is not from Carmack
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 45273
Blocks:
  Show dependency treegraph
 
Reported: 2010-08-13 00:37 UTC by Albert Zeyer
Modified: 2010-08-13 00:44 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Albert Zeyer 2010-08-13 00:37:49 UTC
The code:

/* sqrt() implementation: based on open source QUAKE3 code (magic sqrt
   implementation) by John Carmack.  Returns sqrt of X.  */

static double
mcf_sqrt (double x)
{
#define MAGIC_CONST1    0x1fbcf800
#define MAGIC_CONST2    0x5f3759df
  union {
    int intPart;
    float floatPart;
  } convertor, convertor2;

  gcc_assert (x >= 0);

  convertor.floatPart = x;
  convertor2.floatPart = x;
  convertor.intPart = MAGIC_CONST1 + (convertor.intPart >> 1);
  convertor2.intPart = MAGIC_CONST2 - (convertor2.intPart >> 1);

  return 0.5f * (convertor.floatPart + (x * convertor2.floatPart));
}


Read here:

http://en.wikipedia.org/wiki/Fast_inverse_square_root
http://www.beyond3d.com/content/articles/8/
Comment 1 Andrew Pinski 2010-08-13 00:44:16 UTC
Well really this code should not be using double anyways.  double can produce slight differences between targets.