Skip to content

Commit 0eb311a

Browse files
committed
Fix Linux compatibility for nexttoward functions
The ISO C standard specifies nexttoward takes long double as the second parameter. However, long double maps to different types across platforms: - macOS x86_64: Float80 (80-bit extended precision) - macOS ARM64 & Linux: double (64-bit, same as double) Swift doesn't have Float80 on Linux/ARM, causing compilation errors. Solution: Always expose double-based wrappers to Swift for cross-platform API consistency, while internally calling the native nexttoward with platform-appropriate type conversion. This provides: - Consistent API surface across all platforms - Each platform's native nexttoward implementation - No surprises for library users
1 parent 4777b0d commit 0eb311a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Sources/CISO9899Math/include/iso9899_math.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,19 @@ static inline double iso9899_nan(const char *tagp) { return nan(tagp); }
150150
static inline float iso9899_nanf(const char *tagp) { return nanf(tagp); }
151151
static inline double iso9899_nextafter(double x, double y) { return nextafter(x, y); }
152152
static inline float iso9899_nextafterf(float x, float y) { return nextafterf(x, y); }
153-
static inline double iso9899_nexttoward(double x, long double y) { return nexttoward(x, y); }
154-
static inline float iso9899_nexttowardf(float x, long double y) { return nexttowardf(x, y); }
153+
154+
// Note: ISO C specifies nexttoward takes long double as second parameter.
155+
// We expose double-based wrappers to Swift for cross-platform consistency,
156+
// but internally call the native nexttoward with appropriate type conversion.
157+
#if defined(__APPLE__) && defined(__x86_64__)
158+
// macOS x86_64: long double is Float80, convert from double
159+
static inline double iso9899_nexttoward(double x, double y) { return nexttoward(x, (long double)y); }
160+
static inline float iso9899_nexttowardf(float x, double y) { return nexttowardf(x, (long double)y); }
161+
#else
162+
// Other platforms: long double is same as double
163+
static inline double iso9899_nexttoward(double x, double y) { return nexttoward(x, y); }
164+
static inline float iso9899_nexttowardf(float x, double y) { return nexttowardf(x, y); }
165+
#endif
155166

156167
// Maximum, minimum, and positive difference functions (ISO/IEC 9899 Section 7.12.12)
157168
static inline double iso9899_fdim(double x, double y) { return fdim(x, y); }

0 commit comments

Comments
 (0)