Skip to content

Commit d89f8f4

Browse files
Go back to using static inline stubs for sqrt and remainder (#6769)
* Go back to using static inline implementations of sqrt and remainder now that SR-2089 is resolved. * Fix typo: sqrt -> squareRoot. * Added test for constant-folding sqrt with -O. * Added test case requested by jrose.
1 parent 2888cc7 commit d89f8f4

File tree

3 files changed

+56
-42
lines changed

3 files changed

+56
-42
lines changed

stdlib/public/SwiftShims/LibcShims.h

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,34 @@ __swift_uint32_t
9191
_swift_stdlib_cxx11_mt19937_uniform(__swift_uint32_t upper_bound);
9292

9393
// Math library functions
94-
SWIFT_RUNTIME_STDLIB_INTERFACE float _swift_stdlib_remainderf(float, float);
95-
SWIFT_RUNTIME_STDLIB_INTERFACE float _swift_stdlib_squareRootf(float);
96-
97-
SWIFT_RUNTIME_STDLIB_INTERFACE double _swift_stdlib_remainder(double, double);
98-
SWIFT_RUNTIME_STDLIB_INTERFACE double _swift_stdlib_squareRoot(double);
94+
static inline float _swift_stdlib_remainderf(float _self, float _other) {
95+
return __builtin_remainderf(_self, _other);
96+
}
97+
98+
static inline float _swift_stdlib_squareRootf(float _self) {
99+
return __builtin_sqrtf(_self);
100+
}
101+
102+
static inline double _swift_stdlib_remainder(double _self, double _other) {
103+
return __builtin_remainder(_self, _other);
104+
}
105+
106+
static inline double _swift_stdlib_squareRoot(double _self) {
107+
return __builtin_sqrt(_self);
108+
}
99109

100110
// TODO: Remove horrible workaround when importer does Float80 <-> long double.
101111
#if (defined __i386__ || defined __x86_64__) && !defined _MSC_VER
102-
SWIFT_RUNTIME_STDLIB_INTERFACE
103-
void _swift_stdlib_remainderl(void *_self, const void *_other);
104-
SWIFT_RUNTIME_STDLIB_INTERFACE
105-
void _swift_stdlib_squareRootl(void *_self);
106-
#endif
112+
static inline void _swift_stdlib_remainderl(void *_self, const void *_other) {
113+
long double *_f80self = (long double *)_self;
114+
*_f80self = __builtin_remainderl(*_f80self, *(const long double *)_other);
115+
}
116+
117+
static inline void _swift_stdlib_squareRootl(void *_self) {
118+
long double *_f80self = (long double *)_self;
119+
*_f80self = __builtin_sqrtl(*_f80self);
120+
}
121+
#endif // Have Float80
107122

108123
#ifdef __cplusplus
109124
}} // extern "C", namespace swift

stdlib/public/stubs/LibcShims.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -143,32 +143,3 @@ swift::_swift_stdlib_cxx11_mt19937_uniform(__swift_uint32_t upper_bound) {
143143
std::uniform_int_distribution<__swift_uint32_t> RandomUniform(0, upper_bound);
144144
return RandomUniform(getGlobalMT19937());
145145
}
146-
147-
SWIFT_RUNTIME_STDLIB_INTERFACE
148-
float swift::_swift_stdlib_remainderf(float dividend, float divisor) {
149-
return std::remainder(dividend, divisor);
150-
}
151-
152-
SWIFT_RUNTIME_STDLIB_INTERFACE
153-
float swift::_swift_stdlib_squareRootf(float x) { return std::sqrt(x); }
154-
155-
SWIFT_RUNTIME_STDLIB_INTERFACE
156-
double swift::_swift_stdlib_remainder(double dividend, double divisor) {
157-
return std::remainder(dividend, divisor);
158-
}
159-
160-
SWIFT_RUNTIME_STDLIB_INTERFACE
161-
double swift::_swift_stdlib_squareRoot(double x) { return std::sqrt(x); }
162-
163-
#if (defined(__i386__) || defined(__x86_64__)) && !defined(_WIN32)
164-
SWIFT_RUNTIME_STDLIB_INTERFACE
165-
void swift::_swift_stdlib_remainderl(void *_self, const void *_other) {
166-
*(long double *)_self = std::remainder(*(long double *)_self,
167-
*(const long double *)_other);
168-
}
169-
170-
SWIFT_RUNTIME_STDLIB_INTERFACE
171-
void swift::_swift_stdlib_squareRootl(void *_self) {
172-
*(long double *)_self = std::sqrt(*(long double *)_self);
173-
}
174-
#endif // Have Float80

test/IRGen/builtin_math.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -emit-ir %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -emit-ir -O %s | %FileCheck %s
22

33
// XFAIL: linux
44

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

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

3030
public func test3(d : Double) -> Double {
3131
return sqrt(d)
3232
}
3333

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

3737
public func test4(f : Float) -> Float {
3838
return sqrt(f)
3939
}
40+
41+
// CHECK-LABEL: define {{.*}}test5
42+
// CHECK: ret float 2
43+
44+
public func test5( ) -> Float {
45+
return sqrt(4)
46+
}
47+
48+
// CHECK-LABEL: define {{.*}}test6
49+
// CHECK: ret double 2
50+
51+
public func test6( ) -> Double {
52+
return sqrt(4)
53+
}
54+
55+
// CHECK-LABEL: define {{.*}}test7
56+
// CHECK-NOT: ret float undef
57+
58+
public func test7( ) -> Float {
59+
return sqrt(-1)
60+
}
61+
62+
// CHECK-LABEL: define {{.*}}test8
63+
// CHECK-NOT: ret double undef
64+
65+
public func test8( ) -> Double {
66+
return sqrt(-1)
67+
}

0 commit comments

Comments
 (0)