Megatokio / Libraries

c++ libraries required for all of my projects
Other
12 stars 8 forks source link

`long double` is different size on Mac ARM64 than regular ARM64 #10

Closed barbeque closed 1 year ago

barbeque commented 1 year ago

For some reason (maybe easy compatibility with x86?) the long double type on a new MacBook Pro M2 is 8 and not 16.

I'm building from the root of the zasm repository, with Libraries checked out as a submodule.

% make
g++ -c -pipe -Os -Wall -W -fPIE -DNDEBUG -DRELEASE -Wno-multichar -std=c++11 -I. -ISource -ILibraries -o tmp/cstrings.o Libraries/cstrings/cstrings.cpp
In file included from Libraries/cstrings/cstrings.cpp:5:
In file included from Libraries/cstrings/cstrings.h:6:
In file included from Libraries/kio/kio.h:44:
In file included from Libraries/kio/auto_config.h:21:
Libraries/kio/detect_configuration.h:426:1: error: static_assert failed due to requirement 'sizeof(long double) == 16' "Size is not correct"
static_assert(sizeof(long double) == _sizeof_long_double, "Size is not correct");
^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [tmp/cstrings.o] Error 1

Apple mentions it in their own porting guide: https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Handle-data-types-and-data-alignment-properly

Thank you for the great assembler! I have used it for a bunch of projects and look forward to using it for many more.

Megatokio commented 1 year ago

Hi Barbeque, actually it's even the other way round: this makes long doubles on M2 incompatible with AMD64. AMD64 has 16 byte long doubles (thereof 6 bytes unused) and i assume this is also true on Apple platform. And 64bit ARM somehow has them too but as you pointed out not on Apple. The reason is probably that AMD64 has hardware support and full library support for long double and ARM64 has not. ARM64 has only hardware support for double, and C and C++ libraries provided by ARM declare but don't implement many of the long double functions unless you use some open source versions which Apple probably didn't want to use for License reasons. So they fixed the problem by defining long double the same as double, which is perfectly legal in C/C++ terms but was unexpected/unknown to me. I added an #if switch to define long doubles to 8 bytes on Apple platforms. Thanks a lot for your report!

ARM docs: https://developer.arm.com/documentation/ka004751/1-0