Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update non-C99 version of caml_round for 80bit FP #8629

Open
dra27 opened this issue Apr 19, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@dra27
Copy link
Contributor

commented Apr 19, 2019

Context: #8627 and in particular #8627 (comment)

New optimisations in GCC 7.4 mingw mean that with -O, caml_round is producing the "wrong" answers. It's been fixed in the original PR by enabling SSE2 for the specific platform, but a keen soul may wish to update the integer implementation in caml_round to be able to cope with the excess precision afforded by the use of x87 80bit fpregs.

@xavierleroy

This comment has been minimized.

Copy link
Contributor

commented Apr 21, 2019

Here is an implementation based on modf that is particularly legible (no need to look at the bit-level representation) and I believe immune to excess precision:

double myround(double f)
{
  double intg, frac;
  if (!isfinite(f)) return f;
  /* Decompose f in integral part (intg) + fractional part (frac).
     Both parts have the same sign as f. */
  frac = modf(f, &intg);
  if (frac >= 0.5)
    return intg + 1.0;          /* round away from 0, i.e. up */
  else if (frac <= -0.5)
    return intg - 1.0;          /* round away from 0, i.e. down */
  else
    return intg;                /* round towards 0 */
}

I believe the two FP operations intg + 1.0 and intg - 1.0 are always exact (in double precision), because otherwise the fractional part frac would be 0.0. Hence excess precision will not hurt.

This assumes that the math library provides a correct implementation of modf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.