Slashdot Mirror


Where Intel Processors Fail At Math (Again)

rastos1 writes: In a recent blog, software developer Bruce Dawson pointed out some issues with the way the FSIN instruction is described in the "Intel® 64 and IA-32 Architectures Software Developer's Manual," noting that the result of FSIN can be very inaccurate in some cases, if compared to the exact mathematical value of the sine function.

Dawson says, "I was shocked when I discovered this. Both the fsin instruction and Intel's documentation are hugely inaccurate, and the inaccurate documentation has led to poor decisions being made. ... Intel has known for years that these instructions are not as accurate as promised. They are now making updates to their documentation. Updating the instruction is not a realistic option."

Intel processors have had a problem with math in the past, too.

47 of 239 comments (clear)

  1. Intel Common Core i7 by Spy+Handler · · Score: 5, Funny

    with new maths

    1. Re:Intel Common Core i7 by Falos · · Score: 4, Funny

      1+1=3 for particularly large values of 1

    2. Re: Intel Common Core i7 by iggymanz · · Score: 4, Insightful

      what's the problem, 8 + 5 = 10, absolutely true in base 13

    3. Re: Intel Common Core i7 by Anonymous Coward · · Score: 3, Funny

      "'8 + 5 = 10' is True or false"

      Is True.

    4. Re: Intel Common Core i7 by sexconker · · Score: 2

      Thats true though. Using nearest integer rounding, 1.4 can be accurately represented as one and produce a sum of 2.8, which can be represented by three.

      In other words, 1+1=3 for sufficiently large values of 1.

      That would be 1.4 + 1.4 = 2.8. 2.8 could be then rounded to 3, at which point you could say 1.4 + 1.4 ~= 3,
      Anything beyond that, though, is horseshit.

    5. Re: Intel Common Core i7 by Kalium70 · · Score: 2

      The idea of significant figures is to avoid grossly overstating the precision of a measurement (or a calculation involving measurements). For example, if substance A is measured as 25 g with spring-type kitchen scale and substance B is measured as 10.3682 g with a digital analytical balance, it would be misleading to report the result as 35.3682 g.

      Taken out of context, without further instructions, the issue about 8 + 5 = 10 is unclear. As an instructor, I suspect that the intend was to see if the student can use the rule for significant figures when performing addition. A student who confuses the sigfig rules for addition and multiplication would conclude that since 8 and 5 each have 1 sigfig, the result should have 1 sigfig.

      A couple notes: Numbers with 0 on the right without any decimal point (e.g. 10, 2500) create an ambiguity with sigfigs as to whether those zeros are significant or not. Some authors put a bar over the last significant figure to clarify, but many do not. In fact, one of the textbooks I used --- I believe it was for trig --- changed its practice in a later addition regarding whether those zeros are significant or not.

    6. Re:Intel Common Core i7 by Calydor · · Score: 2

      > Writing a fancy word.

      > And doing it incorrectly.

      --
      -=This sig has nothing to do with my comment. Move along now=-
    7. Re: Intel Common Core i7 by michelcolman · · Score: 2

      And even if you try to justify with significant digits, treating anything up to 14.99 as "equal" to 10 (relative error 50%?!) while the original numbers had relative errors of less than 20% shows a staggering lack of comprehension. These people should not be allowed to teach. One significant digit for 10 is way less precise than one significant digit on 5 or 8, use some common sense for crying out loud.

  2. What this mean... by __aaclcg7560 · · Score: 4, Interesting

    I should get an AMD CPU and put the extra money towards a graphic card since GPUs do math extremely well in parallel.

    1. Re:What this mean... by Austerity+Empowers · · Score: 3, Insightful

      I would test that theory first. I have a hunch some GPUs are going to take shortcuts with math that someone like the guy who wrote this article will object to.

    2. Re:What this mean... by rasmusbr · · Score: 4, Insightful

      AMD CPU:s reportedly return exactly the same values as Intel CPU:s. I'm guessing they do so for compatibility reasons, so that any workarounds that software developers have implemented work as expected.

    3. Re:What this mean... by K.+S.+Kyosuke · · Score: 2

      AMD CPU:s reportedly return exactly the same values as Intel CPU:s.

      What, for transcendental functions? That's both impractical and useless. No "compatiblity reasons" could potentially justify this. If you take the table maker's dilemma into consideration, there's absolutely no reason to standardize on specific implementations of transcendental function - there's no fundamental simple way of "doing them right". I don't believe that IEEE-754 even standardizes anything beyond basic operations, and for good reason; the thing you're proposing could easily lull numerical developers into a sense of false security from bugs: if your algorithms are hugely sensitive to small differences in rounding, perhaps something is wrong with them. Having a lot of FPUs produce binary-identical results often just masks problems, just like deterministic execution of concurrent programs can mask deeper flaws in those.

      --
      Ezekiel 23:20
    4. Re:What this mean... by numbertheo · · Score: 2

      Compatibility is the reason these instructions will never be fixed. AMD implemented correct behavior in the K5 family. It was then reverted to preserve compatibility.

    5. Re:What this mean... by K.+S.+Kyosuke · · Score: 2

      Wrong, at least the new cores are IEEE-754 compliant. AMD's GCN cores give exact results wherever exact results are guaranteed by the standard. Presumably nVidia is doing the same, otherwise they wouldn't be putting Teslas into supercomputers, now would they?

      --
      Ezekiel 23:20
    6. Re:What this mean... by sexconker · · Score: 4, Informative

      On the consumer side, AMD GPUs rock at double, while nVidia's suck.

    7. Re:What this mean... by K.+S.+Kyosuke · · Score: 2

      There's no guarantee that calculating the value of a transcendental function to any fixed floating point precision would require any reasonably bounded amount of intermediate result digits. That's why FPUs never do that and instead they try to return a value as close as reasonably possible in a time as short as possible, without having substantial bad cases in either area. Yes, there's is a correct answer for cos(0.4). No, that's not the same statement. Calculating the function accurately for ALL input values instead of just for 0.4 takes an unpredictable amount of time. That's why no real-world hardware implementation is "correct", whereas primitive operations like addition and multiplication can be (and are) required to be correct (as in always giving the closest representable number to the precise result) because they're trivial in comparison.

      --
      Ezekiel 23:20
    8. Re:What this mean... by ais523 · · Score: 4, Interesting

      GPUs used to take mathematical shortcuts all the time. More recently, though, with the scientific community starting to use GPU clusters for computation, the main GPU manufacturers have been adding mathematically precise circuitry (and may well use it by default, on the basis that there's no point in having both an accurate and an inaccurate codepath).

      --
      (1)DOCOMEFROM!2~.2'~#1WHILE:1<-"'?.1$.2'~'"':1/.1$.2'~#0"$#65535'"$"'"'&.1$.2'~'#0$#65535'"$#0'~#32767$#1"
    9. Re:What this mean... by Frobnicator · · Score: 5, Informative

      You might take a look at the article and at Intel's reply.

      The issue is in sine, cosine, and similar trig functions, with an actual error of 4e-21. That error scales, of course.

      Intel's documentation change basically says you should scale and reduce your numbers first before running the functions.

      Consider what that level of error precision means. If you were measuring with a meter stick, you could be measuring the radius of electron charge radii with several precision bits left over. If you were measuring the distance between the Sun and Proxima Centari, you could do it in millimeters and have accuracy to spare.

      Even though I've run HPC simulations most of my career, we've seldom needed more than around six decimal digits of precision; that's akin to variations of human hair width when working at the meter level. It's only a problem when someone throws some strange scale into the mix; we're running physics on the kg-m-s scale, and suddenly someone complains that their usage of microseconds and nanometers breaks the physics engine We answer simply, "Yes. Yes, it does." If you need to operate in both scales, you need a different library that handles it.

      Finally, even the actual article admits this is mostly about documentation. "The absolute error in the range I was looking at was fairly constant at about 4e-21, which is quite small. For many purposes this will not matter. ... for the domains where that matters the misleading documentation can easily be a problem." He then points out that a bunch of existing math libraries know about it. He mentions that high precision libraries have different solutions and always have. He mentions that most scientists who need it use better, high precision libraries. And he details that is really just the rough approximations done on the FPU that already plays fast-and-loose by switching between 53-bit and 63-bit floating point values that have been documented as being only good for that kind of approximation since the 1980s. Everybody who works professionally with floating point for any amount of time already knows the entire x86 family (including AMD and Intel) dating back to the original coprocessor are all terrible if you need high precision.

      --
      //TODO: Think of witty sig statement
    10. Re:What this mean... by hairyfeet · · Score: 2, Interesting

      To steal a line from mel brooks "bullshit bullshit aaaannnnddd bullshit". if you look at the actual figures it would take you 18.9 years to break even when comparing an AMD OCTOcore to an 4770k...now are you REALLY gonna be using that 4770k 19 years from now? Didn't think so.

      Sorry but the actual math shows that is a bunch of bull, you can't even buy a pizza with the amount you'd "save" by going intel, and the amount you'd lose in price difference means you would NEVER break even.

      --
      ACs don't waste your time replying, your posts are never seen by me.
    11. Re:What this mean... by Luckyo · · Score: 3, Informative

      To be fair, almost no consumers have any use for double. And commercial entities who do usually don't mind the extra zero at the end of GPU's cost, because to them, that's just expenses to be written off on their taxes.

    12. Re:What this mean... by Billly+Gates · · Score: 2

      In an office environment it adds up fast when you have 100 computers. Also you need a more expensive power supply by going with AMD systems. True the first i7 sucked 200 watts of power but the 4770k is really efficient.

  3. Exact mathematical value isn't the ideal by i+kan+reed · · Score: 4, Insightful

    The main goal for Floating Point coprocessor sine calculations is to get a good enough result in a set number of cycles.

    Given that fully approximating sine takes about as many concrete operations as bits in the value, getting it exactly right isn't usually a trade off people want to make.

    There's a reason the C standard specifies that mathematical trig functions are platform dependent. If you want it precise, do it yourself to the level of precision you need.

    1. Re:Exact mathematical value isn't the ideal by TWX · · Score: 5, Insightful

      From what I gather, the problem is that Intel didn't acknowledge in documentation how poor the instruction was for scientific use though. This is fine for home and probably most general-purpose business use, but becomes a problem when it's more critical. If those that develop software that relies on sine functionality don't know about this then error in the results of their programs will actually matter.

      This won't matter to a gamer playing some first-person shooter.

      --
      Do not look into laser with remaining eye.
    2. Re:Exact mathematical value isn't the ideal by Beck_Neard · · Score: 5, Informative

      The error is not small. If you read the article, on certain very reasonable inputs (not pathological at all), you can sometimes wind up with only _four_ bits being correct.

      Many scientific applications absolutely depend on fast hardware sine implementation. As you said, getting it exactly right isn't a tradeoff that people usually want to make.

      This has nothing to do with the C standard. Intel's own documentation was incorrect, making 'YMMV' completely moot.

      --
      A fool and his hard drive are soon parted.
    3. Re:Exact mathematical value isn't the ideal by Anonymous Coward · · Score: 3, Interesting

      The problem isn't caused by the actual sine calculation, but in the preparatory range reduction, where the input value is mapped to the +-PI/2 interval. It appears that a "hard coded" approximate value of PI is the culprit, because the approximation is only accurate to 66 bits, but for the correct result, it would have to be correct to 128 bits. AMD at one point made processors which used the full precision value of PI and returned correct results for fsin(x). It broke software, so AMD "fixed" it by breaking it in microcode. AMD now uses the same less accurate approximation of PI and returns the exact same wrong values for fsin(x), even though they know it's wrong and already had the correct implementation. Processor cycles don't come into it.

    4. Re:Exact mathematical value isn't the ideal by Cassini2 · · Score: 2

      This is the #1 reason that banks use COBOL, and IBM makes Power processors with high-speed BCD arithmetic instructions.

    5. Re:Exact mathematical value isn't the ideal by Austerity+Empowers · · Score: 2

      Well they would if the shooter was designed to apply, say, the character rotation as a delta versus as an absolute. That operation uses a lot of sin/cos, most games are designed such that the angle is stored, the delta updates the angle, and the rotation reapplied on update. Versus rotating the vertices based on the delta from the update, and saving the result (until the next update). You do the latter too much and eventually your object looks like poo. Mathematically, it's perfectly acceptable, but practically wrong.

    6. Re:Exact mathematical value isn't the ideal by ledow · · Score: 4, Interesting

      Sorry, but anyone relying on this for scientific use where the answer matters should be using software that gives them the accuracy they want and - ultimately - are the only people who will realise whether the result is correct "enough" or not for their process.

      Some idiot researcher who expects Excel or an FPU instruction to be accurate for sin to more than 10 decimal places is going to crop up SO MANY anomalies in their data that they'll stick out like a sore thumb.

      Nobody serious would do this. Any serious calculation requires an error calculation to go with it. There's a reason that there are entire software suites and library for arbitrary precision arithmetic.

      I'm a maths graduate. I'll tell you now that I wouldn't rely on a FPU instruction to be anywhere near accurate. If I was doing anything serious, I'd be plugging into Maple, Matlab, Mathematica and similar who DO NOT rely on hardware instructions. And just because two numbers "add up" on the computer, that's FAR from a formal proof or even a value you could pass to an engineer.

      Nobody's doing that. That's why Intel have managed to "get away" with those instructions being like that for, what? Decades? If you want to rotate an object in 3D space for a game, you used to use the FPU. Now you use the GPU. And NEITHER are reliable except for where it really doesn't matter (i.e. whether you're at a bearing of 0.00001 degrees or 0.00002 degrees).

      Fuck, within a handful of base processor floating point instructions you can lose all accuracy if you're not careful.

    7. Re:Exact mathematical value isn't the ideal by adonoman · · Score: 2
      If you are doing any floating point calculations and assuming exact results, you're going to get yourself in trouble. The issue is that FSIN is less accurate than advertised, not that it's not 100% accurate.

      Anyone who deals with floating point math very quickly learns about error accumulation and how to deal with it.

    8. Re:Exact mathematical value isn't the ideal by ChrisMaple · · Score: 2

      The problem is with sin( near pi ). Range reduction subtracts pi, and the value of pi doesn't have enough bits in the Intel fsin instruction. In gaming, nothing is going to rotate pi radians between updates, so this deficiency won't show up in gaming applications where incremental rotates are used.

      --
      Contribute to civilization: ari.aynrand.org/donate
    9. Re:Exact mathematical value isn't the ideal by gnasher719 · · Score: 2

      I recall working with numerical methods from about 40 years ago, and all of the calculations that required a call to sin were range reduced to the region of +/- pi/4 anyway. The reason is that the taylor series expansions for sine and cos are most accurate in the region of zero, and for values in excess of pi/4, it is more accurate to do a transformation and implement a different call.

      If you read the article, that's what the Intel processor does. Instead of an infinitely precise value of pi, it uses pi rounded to 66 bits (which is 13 bits more than normal double precision arithmetic would use, and 2 bits more than extended precision arithmetic would use). So all these people getting all excited about an error in Intel's FPU most likely wouldn't be capable of implementing the sine function anywhere near as precise as FSIN does.

    10. Re:Exact mathematical value isn't the ideal by whit3 · · Score: 2

      The error is not small. If you read the article, on certain very reasonable inputs (not pathological at all), you can sometimes wind up with only _four_ bits being correct.

      The issue here, is that any computed sine value outside the first quadrant (input values 0 to pi/2) is computed by reducing the input quantity. The function is periodic, so adding or subtracting any multiple of the period (2 pi) from the input value, is mathematically valid. So, the error is made to be small for each value in that first quadrant, and the Intel documentation correctly quotes the errors there. The 'reducing the input quantity' step, however, doesn't use extended precision arithmetic, so the (add 2pi) step adds roundoff error (and that roundoff error generates output errors).

      Thus, the sin(1.14159) calculation, with a 10-decimal-place accurate representation of that number (1.14159), gives roughly a 10-decimal-place determination of the proper sine value. But, the sin(1.14159 x 10**9) will get LOTS of leading digits truncated when you scale the input, and can only determine a 1-decimal-place scaled input value, thus only a 1-decimal-place sine.

      And, if the hypothetical, perfect, sine value has leading zeroes, it looks terrible as a 'percent error'. Any roundofff error at all, in a sin(3.14159265358979323846264338...) calculation, will get a divide-by-zero boost when you calculate a percent error. The absolute error, though, is just what is to be expected from roundoff error in a step that takes the remainder after dividing by (2pi + roundoff_error(2pi) ).

  4. Re:Say what? by ilsaloving · · Score: 2

    http://www.shsmedia.com/pentiu...

    Remember... "Don't Divide, Intel Inside"

  5. Bad intel by Tablizer · · Score: 3, Funny

    We already know it's a sin to eat pi.

    1. Re:Bad intel by wonkey_monkey · · Score: 2

      I ought to tan your hide, cos that was terrible.

      Stop going off on tangents.

      --
      systemd is Roko's Basilisk.
  6. Read TFA. Not even a close approximation, and docu by raymorris · · Score: 5, Informative

    The documentation says that the result will be correct until the last decimal place. So if the CPU says the answer is:

    0.123 456 789 123 456 789

    You have have a close approximation, accurate to the 17th decimal place, according to the documentation.
    The problem is, the correct answer may be:
    0.123 444 555 666 777 888

    The documentation says it's fairly precise. In truth, it's only good to the fourth decimal place in some cases, whereas Intel documented the function to be accurate to 66 bits or so.

  7. Inaccurate headline by Loki_1929 · · Score: 4, Informative

    The headline is quite inaccurate. The processors are doing what they're designed to do; approximate the results of certain operations to a "good enough" value to achieve an optimal result:work ratio. Sort of like how the NFL measures first-downs with a stick, a chain, and some eyeballs rather than bringing in a research team armed with scanning electron microscopes to tell us how many Planck lengths short of the first down they were.

    This is a documentation failure. They're fixing the documentation. For anyone who would actually care about perfect accuracy in these kinds of operations, there are any number of different solutions to achieve the desired, more accurate result. The headline and the summary make it seem as though there's a problem with the processor which is simply incorrect.

    --
    -- "Government is the great fiction through which everybody endeavors to live at the expense of everybody else."
  8. Re:did you even bother looking it up? by Anonymous Coward · · Score: 2, Insightful

    .Net has a native data type called decimal [microsoft.com] that does uses decimal floating point and is accurate to 28 or 29 digits, which makes it a great thing to use when dealing with money. I wish more languages would support something similar.

    They do:
    https://docs.python.org/2/library/decimal.html
    http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

    Just because your world is limited to .NET it does not mean there aren't other things out there... Did you even bother looking up?

  9. Re:Say what? by serviscope_minor · · Score: 4, Funny

    We are pentium of borg. Division is futile. You will be approximated.

    From what I remember it was the first revision of the Pentium 1 aka the Pentium 0.999998163849

    --
    SJW n. One who posts facts.
  10. example from TFA. try it by raymorris · · Score: 3, Informative

    Here's an example from TFA:

    tan(1.5707963267948966193)

    actual -39867976298117107068
    x87 fpu -36893488147419103232
    error 743622037674500958.81 ulp

    1. Re:example from TFA. try it by geantvert · · Score: 2

      1.5707963267948966193 is rounded as 0x1.921FB54442D18p+0
      Look what would happens with different roundings:

      tan(0x1.921FB54442D17p+0) = + 0x1.9153D9443ED0Bp+51
      tan(0x1.921FB54442D18p+0) = + 0x1.D02967C31CDB5p+53
      tan(0x1.921FB54442D19p+0) = - 0x1.617A15494767Ap+52

      Simply speaking, computing the TAN of 1.5707963267948966193 in double precision does not make sense.
      That's a typical floating point precision.

      Now, if you really want to discuss the precision of TAN, you should use 0x1.921FB54442D18p+0 or any other value with an exact double precision representation.
      But even then, it does not really make sense to discuss the precision near special values such as PI/2 because the precision of your input data will be unrealiable around that number.

  11. Re:My workaround by lgw · · Score: 4, Funny

    The positive or the negative root?

    I just use the average of the two, for predictable output.

    --
    Socialism: a lie told by totalitarians and believed by fools.
  12. To be expected by jmv · · Score: 2

    There's nothing I find particularly alarming here and the behaviour is in fact pretty much what I would expect for computing sin(x). Sure, maybe the doc needs updating, but nobody would really expect fsin to do much better than what it does. And in fact, if you wanted to maintain good accuracy even for large values (up to the double-precision range), then you would need a 2048-bit subtraction just for the range reduction! As far as I can tell, the accuracy up to pi/2 is pretty good. If you want good accuracy beyond that, you better do the range reduction yourself. In general, I would also argue that if you have accuracy issues with fsin, then your code is probably broken to begin with.

  13. Total rubbish article by gnasher719 · · Score: 4, Informative

    Here's what the complaint is about: The Intel FSIN instruction performs an argument reduction to calculate the values of the sine function, but not with the exact value of pi, but using a 66 bit approximation of pi. If your argument is close to a multiple of pi, then the argument reduction doesn't give the correct result.

    HOWEVER, if your argument is an extended precision number close to pi = 3.14..., then the last bit in the mantissa of that number has a value of 2^-62. So if you calculate an argument close to pi, the unavoidable bounds for the rounding error are 2^-63. This error in the argument is about 10 times larger than the error caused by using an approximation for pi in the argument reduction. If you use double precision, the error in the argument is about 20,000 times larger than the error caused by the argument reduction.

    All this has been known for years; posting it today and claiming there is any problem is just ridiculous.

  14. Article is wrong - it is documented by gnasher719 · · Score: 2

    It seems that the blogger didn't actually read the documentation that he claimed to read. The exact behaviour is documented in "Intel® 64 and IA-32 Architectures Software Developerâ(TM)s Manual Volume 1: Basic Architecture" of March 2012 on page 8-31. I don't have an older copy of that manual anymore, but I have written code according to that exact documentation sometime around 2001, so I am quite confident that it was in the 2001 version of the document.

    This is what the documentation says: "The internal value of Ï that the x87 FPU uses for argument reduction and other computations is as follows: Ï = 0.f â-- 2^2 where: f = C90FDAA2 2168C234 C". A more precise approximation according to Wikipedia would have been f = C90FDAA2 2168C234 C4C6 4...; the difference between pi and the approximation used by Intel is about 0.0764 * 2^-64.

    If you let x = pi, then people would ordinarily expect that sin (x) = 0. That, however, would be wrong. Storing pi into a floating-point number produces a rounding error. Rounded to extended precision (64 bit mantissa) instead of the usual double precision (53 bit mantissa) produces a result of 4 * 0.C90FDAA2 2168C235 instead of 4 x C90FDAA2 2168C234 C4C6 4...; this is too large by 4 * (1 - 0.C4C64...) * 2^-64. The sine of that number would also be 4 * (1 - 0.C4C64...) * 2^-64.

    But FSIN doesn't subtract pi from that number x, instead it subtracts 4 * 0.C90FDAA2 2168C234 C. So we get a result of 4 * (1 - 0.C) * 2^-64 instead of 4 * (1 - 0.C4C64...) * 2^-64. That's what he complains about. The reality is that the correct result would have been zero, but we couldn't get that because trying to assign pi even to an extended precision number gives some rounding error.

    Now in practice, if you calculate an argument for the sine function, and that argument is close to pi, even if you manage to get a correctly rounded extended precision result, you must expect a rounding error up to 2^-63, and therefore an error in the result up to 2^-63, even if the calculation of the result is perfect. FSIN gives a result that is about 0.0764 * 2^-64 away from that, so the inevitable error caused by rounding the argument is increased by a massive 3.82 percent. Doing the calculation in double precision, as almost everyone does, makes the rounding error 2048 times larger and FSIN is now 0.00185 percent worse than optimal.

  15. Dawson found a bug in gcc 4.3 as well by munch117 · · Score: 2

    Dawson points to an 'optimisation' in gcc 4.3: constant folding is done using the higher-precision MPFR library. At least the gcc developers seem to think it's an optimisation, but unless it's disabled by default, it is actually a bug. In the absence of undefined behaviour, optimisations must not change observable behaviour. And, as Dawson demonstrates, this one does.

    If you need MPFR precision, you should use MPFR explicitly.

  16. Do you really need this precision? by Polizei · · Score: 2

    Come on, guys, you'll ever only use FPU instructions when you need speed, not precision.
    Anyone remember 0x5f375a86?
    The precision used in Quake's source code wasn't even nearly comparable to the FPU, but was fast enough.

    In other words, you'll never calculate shopping cart totals minus discounts and other stuff this way (or, at least, you shouldn't!)
    There's BigDecimal in Ruby/Java, decimal.Decimal in Python, GMP in C/C++, etc...