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

C versions of remquo deliver wrong quotient if x == -y #283

Open
statham-arm opened this issue Oct 30, 2023 · 0 comments
Open

C versions of remquo deliver wrong quotient if x == -y #283

statham-arm opened this issue Oct 30, 2023 · 0 comments
Labels
arm ARM-based architectures bug Incorrect or unexpected results, crashes, etc.

Comments

@statham-arm
Copy link

The following test program uses all three of remquof, remquo and remquol to compute the remainder of −3 mod +3. The remainder should be zero, and the quotient should be (some low bits of) −1.

#include <stdio.h>
#include <math.h>

int main(void)
{
  int quo;
  float remf = remquof(-3.0F, 3.0F, &quo);
  printf("remquof(-3,+3) = %g, quo=%08x\n", remf, quo);
  double rem = remquo(-3.0, 3.0, &quo);
  printf("remquo (-3,+3) = %g, quo=%08x\n", rem, quo);
  long double reml = remquol(-3.0L, 3.0L, &quo);
  printf("remquol(-3,+3) = %Lg, quo=%08x\n", reml, quo);
}

Compiling openlibm for amd64, so that these functions are provided by amd64/s_remquo.S and friends, this behaves as expected:

$ cc test.c libopenlibm.a && ./a.out
remquof(-3,+3) = -0, quo=ffffffff
remquo (-3,+3) = -0, quo=ffffffff
remquol(-3,+3) = -0, quo=ffffffff

But on aarch64, where the C versions in src/s_remquo.c and friends are used, the wrong quotient is delivered:

$ cc test.c libopenlibm.a && ./a.out
remquof(-3,+3) = -0, quo=00000001
remquo (-3,+3) = -0, quo=00000001
remquol(-3,+3) = -0, quo=00000001

Looking at the source code, it appears that there's a special case for |x| = |y|, which always sets *quo = 1 without checking signs.

(Tested against the current HEAD, commit 12f5ffc, in each case.)

@ararslan ararslan added arm ARM-based architectures bug Incorrect or unexpected results, crashes, etc. labels Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arm ARM-based architectures bug Incorrect or unexpected results, crashes, etc.
Projects
None yet
Development

No branches or pull requests

2 participants