Skip to content

Go back to using static inline stubs for sqrt and remainder #6769

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

Merged
merged 4 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions stdlib/public/SwiftShims/LibcShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,34 @@ __swift_uint32_t
_swift_stdlib_cxx11_mt19937_uniform(__swift_uint32_t upper_bound);

// Math library functions
SWIFT_RUNTIME_STDLIB_INTERFACE float _swift_stdlib_remainderf(float, float);
SWIFT_RUNTIME_STDLIB_INTERFACE float _swift_stdlib_squareRootf(float);

SWIFT_RUNTIME_STDLIB_INTERFACE double _swift_stdlib_remainder(double, double);
SWIFT_RUNTIME_STDLIB_INTERFACE double _swift_stdlib_squareRoot(double);
static inline float _swift_stdlib_remainderf(float _self, float _other) {
return __builtin_remainderf(_self, _other);
}

static inline float _swift_stdlib_squareRootf(float _self) {
return __builtin_sqrtf(_self);
}

static inline double _swift_stdlib_remainder(double _self, double _other) {
return __builtin_remainder(_self, _other);
}

static inline double _swift_stdlib_squareRoot(double _self) {
return __builtin_sqrt(_self);
}

// TODO: Remove horrible workaround when importer does Float80 <-> long double.
#if (defined __i386__ || defined __x86_64__) && !defined _MSC_VER
SWIFT_RUNTIME_STDLIB_INTERFACE
void _swift_stdlib_remainderl(void *_self, const void *_other);
SWIFT_RUNTIME_STDLIB_INTERFACE
void _swift_stdlib_squareRootl(void *_self);
#endif
static inline void _swift_stdlib_remainderl(void *_self, const void *_other) {
long double *_f80self = (long double *)_self;
*_f80self = __builtin_remainderl(*_f80self, *(const long double *)_other);
}

static inline void _swift_stdlib_squareRootl(void *_self) {
long double *_f80self = (long double *)_self;
*_f80self = __builtin_sqrtl(*_f80self);
}
#endif // Have Float80

#ifdef __cplusplus
}} // extern "C", namespace swift
Expand Down
29 changes: 0 additions & 29 deletions stdlib/public/stubs/LibcShims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,3 @@ swift::_swift_stdlib_cxx11_mt19937_uniform(__swift_uint32_t upper_bound) {
std::uniform_int_distribution<__swift_uint32_t> RandomUniform(0, upper_bound);
return RandomUniform(getGlobalMT19937());
}

SWIFT_RUNTIME_STDLIB_INTERFACE
float swift::_swift_stdlib_remainderf(float dividend, float divisor) {
return std::remainder(dividend, divisor);
}

SWIFT_RUNTIME_STDLIB_INTERFACE
float swift::_swift_stdlib_squareRootf(float x) { return std::sqrt(x); }

SWIFT_RUNTIME_STDLIB_INTERFACE
double swift::_swift_stdlib_remainder(double dividend, double divisor) {
return std::remainder(dividend, divisor);
}

SWIFT_RUNTIME_STDLIB_INTERFACE
double swift::_swift_stdlib_squareRoot(double x) { return std::sqrt(x); }

#if (defined(__i386__) || defined(__x86_64__)) && !defined(_WIN32)
SWIFT_RUNTIME_STDLIB_INTERFACE
void swift::_swift_stdlib_remainderl(void *_self, const void *_other) {
*(long double *)_self = std::remainder(*(long double *)_self,
*(const long double *)_other);
}

SWIFT_RUNTIME_STDLIB_INTERFACE
void swift::_swift_stdlib_squareRootl(void *_self) {
*(long double *)_self = std::sqrt(*(long double *)_self);
}
#endif // Have Float80
34 changes: 31 additions & 3 deletions test/IRGen/builtin_math.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -emit-ir %s | %FileCheck %s
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -emit-ir -O %s | %FileCheck %s

// XFAIL: linux

Expand All @@ -25,15 +25,43 @@ public func test2(f : Double) -> Double {
// we want sqrt(negative) to be defined to be NaN for IEEE 754 conformance.

// CHECK-LABEL: define {{.*}}test3
// CHECK-NOT: call double @llvm.sqrt.f64
// CHECK: call double @sqrt

public func test3(d : Double) -> Double {
return sqrt(d)
}

// CHECK-LABEL: define {{.*}}test4
// CHECK-NOT: call float @llvm.sqrt.f32
// CHECK: call float @sqrtf

public func test4(f : Float) -> Float {
return sqrt(f)
}

// CHECK-LABEL: define {{.*}}test5
// CHECK: ret float 2

public func test5( ) -> Float {
return sqrt(4)
}

// CHECK-LABEL: define {{.*}}test6
// CHECK: ret double 2

public func test6( ) -> Double {
return sqrt(4)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess since we're worried about LLVM's sqrt, is it worth adding a test for sqrt(-1) not becoming undef or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the earlier CHECK-NOT llvm.sqrt probably covers us, but I can definitely add one.


// CHECK-LABEL: define {{.*}}test7
// CHECK-NOT: ret float undef

public func test7( ) -> Float {
return sqrt(-1)
}

// CHECK-LABEL: define {{.*}}test8
// CHECK-NOT: ret double undef

public func test8( ) -> Double {
return sqrt(-1)
}