Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do 80-bit floating point arithmetic with the Aarch64 architecture?

To give more context to what I asked in the title: I'm using VS C++, and I need to read an 80-bit floating point number from memory and format it into a human-readable form. I can somewhat do it for the Intel x64/x86 architecture, but I'm struggling to figure out how to do it for Aarch64.

PS. For x64, I was able to do it in assembly by converting the 80-bit long double to double using this function:

; RCX = points to `long double` to convert from
; RDX = points to `double` to convert to

FLD     tbyte ptr [RCX]
FSTP    qword ptr [RDX]
ret

And then by printing the converted double value. It loses some precision, but that's OK.

like image 867
c00000fd Avatar asked Nov 01 '25 13:11

c00000fd


1 Answers

I need to read an 80-bit floating point number from memory and format it into a human-readable form. I can somewhat do it for the Intel x64/x86 architecture, but I'm struggling to figure out how to do it for Aarch64.

Try SoftFloat library, it has floatx80_to_float32, floatx80_to_float64 and floatx80_to_float128. Detect the native format, act accordingly.

#include "softfloat.h"
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    void* (*processFloatx80)(floatx80 fx80);
} FloatProcessor;

void* processFloatx80_to_float64(floatx80 fx80) {
    float64_t* result = (float64_t*)malloc(sizeof(float64_t));
    
    if (result != NULL) {
        *result = floatx80_to_float64(fx80);
    }
    return result;
}

void* processFloatx80_to_float32(floatx80 fx80) {
    float32_t* result = (float32_t*)malloc(sizeof(float32_t));
    
    if (result != NULL) {
        *result = floatx80_to_float32(fx80);
    }
    return result;
}

void* processFloatx80_to_float128(floatx80 fx80) {
    float128_t* result = (float128_t*)malloc(sizeof(float128_t));
    
    if (result != NULL) {
        *result = floatx80_to_float128(fx80);
    }
    return result;
}

int main() {
    ...

    floatx80 fx80;
    FloatProcessor processor;

    #if defined(__FLOAT128__)
    processor.processFloatx80 = processFloatx80_to_float128;
    #elif defined(__aarch64__) || defined(__arm__) || defined(__x86_64__)
    processor.processFloatx80 = processFloatx80_to_float64;
    #else
    processor.processFloatx80 = processFloatx80_to_float32;
    #endif

    ...
}
like image 172
Shelton Liu Avatar answered Nov 04 '25 05:11

Shelton Liu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!