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

Rounding error in subnormal division in compiler-rt __divtf3 for f128 #93401

Open
tgross35 opened this issue May 26, 2024 · 1 comment
Open

Comments

@tgross35
Copy link

tgross35 commented May 26, 2024

Multiplying f128 0x00000000000000000000000000000001 * 0x0001ffffffffffffffffffffffffffff should have the result 0x3f8e0000000000000000000000000001, which is produced by the GCC libraries (godbolt). Clang's compiler-rt returns a value with an incorrect exponent, 0x3f8f0000000000000000000000000001.

Reproduction, needs to link against compiler-rt divtf3.c with __divtf3 renamed to __divtf3_x so the system library doesn't get linked instead.

#define __STDC_WANT_IEC_60559_TYPES_EXT__

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#if defined(__clang__) && (defined(__i386) || defined(__x86_64))
#define _Float128 __float128
#endif

_Float128 __divtf3_x(_Float128, _Float128);

typedef struct {
    uint64_t lower, upper;
} u128;


void f128_print(_Float128 val) {
    u128 ival = *((u128 *)(&val));

    #ifndef __clang__
    char buf[1024];
    strfromf128(buf, sizeof(buf), "%.32g", val);
    printf("%#018" PRIx64 "%016" PRIx64 " %s\n", ival.upper, ival.lower, buf);
    #else
    printf("%#018" PRIx64 "%016" PRIx64 " %lf\n", ival.upper, ival.lower, (double)val);
    #endif
}

_Float128 new_f128(uint64_t upper, uint64_t lower) {
    u128 val;
    val.lower = lower;
    val.upper = upper;
    return *((_Float128 *)(&val));
}

int main() {
    _Float128 a = new_f128(0x0, 0x1);
    _Float128 b = new_f128(0x0001ffffffffffff, 0xffffffffffffffff);
    f128_print(a);
    f128_print(b);
    _Float128 c = __divtf3_x(a, b);
    // _Float128 c = a / b;
    f128_print(c);

    return 0;
}
@tgross35 tgross35 changed the title Rounding error in compiler-rt __divtf3 Rounding error in compiler-rt __divtf3 for f128 May 26, 2024
@tgross35 tgross35 changed the title Rounding error in compiler-rt __divtf3 for f128 Rounding error in subnormal division in compiler-rt __divtf3 for f128 May 29, 2024
@tgross35
Copy link
Author

@atrosinenko it looks like you did this implementation, do you have any ideas for how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants