Skip to content

Commit

Permalink
[libm] Fix Building For ARM64 & Add Stubs
Browse files Browse the repository at this point in the history
It really sucks that Apple no longer open sources `libm`... I'm not sure if we should replace `libm` with another macOS friendly math library, or try to implement the math stubs...
  • Loading branch information
CuriousTommy committed Nov 5, 2023
1 parent ad810d3 commit 10552e4
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/libm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ if (TARGET_i386 OR TARGET_x86_64)
Source/Intel/truncl.S
Source/Intel/trunc.S
)
elseif (TARGET_ARM64)
set(libm_sources ${libm_sources}
Source/ARM/nan.c
Source/ARM/fpclassify.c
Source/ARM/logb.c
Source/ARM/logbf.c
Source/ARM/stub.c
)
endif ()

# Gross hack to rename symbols in $fenv_access_off variants
Expand All @@ -164,6 +172,10 @@ else ()
endif ()
make_fat(system_m_extra)

if (TARGET_i386 OR TARGET_x86_64)
set (LIBM_ALIAS_LIST "-Wl,-alias_list,${CMAKE_CURRENT_SOURCE_DIR}/Exports/libm_Intel.a.alias -Wl,-alias_list,${CMAKE_CURRENT_SOURCE_DIR}/Exports/libmathCommonIntel.alias")
endif ()

set(DYLIB_INSTALL_NAME "/usr/lib/system/libsystem_m.dylib")
add_circular(system_m FAT
SOURCES
Expand All @@ -174,7 +186,7 @@ add_circular(system_m FAT
system_c
system_dyld
LINK_FLAGS
"-Wl,-alias_list,${CMAKE_CURRENT_SOURCE_DIR}/Exports/libm_Intel.a.alias -Wl,-alias_list,${CMAKE_CURRENT_SOURCE_DIR}/Exports/libmathCommonIntel.alias"
"${LIBM_ALIAS_LIST}"
)
#set_property(TARGET system_m APPEND_STRING PROPERTY LINK_FLAGS
# "-Wl,-alias_list,${CMAKE_CURRENT_SOURCE_DIR}/Exports/libm_Intel.a.alias -Wl,-alias_list,${CMAKE_CURRENT_SOURCE_DIR}/Exports/libmathCommonIntel.alias")
Expand Down
94 changes: 94 additions & 0 deletions src/libm/Source/ARM/stub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
double scalbn(double x, int exp) {
__builtin_trap();
return 0.0;
}

float scalbnf(float x, int exp) {
__builtin_trap();
return 0.0;
}

long double scalbnl(long double x, int exp) {
__builtin_trap();
return 0.0;
}

long double logbl(long double x) {
__builtin_trap();
return 0.0;
}

long double nanl(const char *tagp) {
__builtin_trap();
return 0.0;
}

double cos(double x) {
__builtin_trap();
return 0.0;
}

float cosf(float x) {
__builtin_trap();
return 0.0;
}

double pow(double x, double y) {
__builtin_trap();
return 0.0;
}

double sin(double x) {
__builtin_trap();
return 0.0;
}

float sinf(float x) {
__builtin_trap();
return 0.0;
}

double acos(double x) {
__builtin_trap();
return 0.0;
}

double asin(double x) {
__builtin_trap();
return 0.0;
}

double atan(double x) {
__builtin_trap();
return 0.0;
}

double atan2(double y, double x) {
__builtin_trap();
return 0.0;
}

double log(double x) {
__builtin_trap();
return 0.0;
}

double modf(double value, double *iptr) {
__builtin_trap();
return 0.0;
}

double tan(double x) {
__builtin_trap();
return 0.0;
}

double fmod(double x, double y) {
__builtin_trap();
return 0.0;
}

double log2(double x) {
__builtin_trap();
return 0.0;
}
2 changes: 1 addition & 1 deletion src/libm/Source/fenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "architecture/ppc/fenv.h"
#elif (defined (__i386__) || defined( __x86_64__ ))
#include "architecture/i386/fenv.h"
#elif defined(__arm__)
#elif defined(__arm__) || defined(__arm64__)
#include "architecture/arm/fenv.h"
#else
#error Unknown architecture
Expand Down
2 changes: 1 addition & 1 deletion src/libm/Source/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "architecture/ppc/math.h"
#elif (defined (__i386__) || defined( __x86_64__ ))
#include "architecture/i386/math.h"
#elif defined(__arm__)
#elif defined(__arm__) || defined(__arm64__)
#include "architecture/arm/math.h"
#else
#error Unknown architecture
Expand Down
1 change: 1 addition & 0 deletions src/libm/include/architecture/arm/fenv.h
1 change: 1 addition & 0 deletions src/libm/include/architecture/arm/math.h

0 comments on commit 10552e4

Please sign in to comment.