Skip to content

Commit 3fda509

Browse files
committed
Implementation of ElementaryFunctions / Real protocols.
This commit implements SE-0246, by adding conformance to Real to the Float, CGFloat, Double, and Float80 types, implemented either in terms of the system's C math library, existing standard library functionality, or LLVM intrinsics. It includes basic test coverage for these new functions, and deprecates and obsoletes *some* existing functionality in the Platform overlay. We still need to make a decision about how to handle the remaining "tgmath" functions, because obsoleting them is technically a source-breaking change (if users have unqualified names like "exp(1)", it's fine, but it would break users who have used qualified names like "Darwin.exp(1)".)
1 parent cad116a commit 3fda509

23 files changed

+1151
-417
lines changed

cmake/modules/ClangClCompileRules.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11

22
# clang-cl interprets paths starting with /U as macro undefines, so we need to
33
# put a -- before the input file path to force it to be treated as a path.
4-
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}")
5-
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT}")
4+
if(NOT MSVC AND "${CMAKE_SIMULATE_ID}" STREQUAL "MSVC")
5+
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}")
6+
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT}")
7+
endif()
68

79
# NOTE(compnerd) incremental linking is known to cause corruption in the
810
# protocol conformance tables. Avoid using incremental links with Visual

cmake/modules/SwiftSharedCMakeConfig.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ endmacro()
258258
macro(swift_common_cxx_warnings)
259259
# Make unhandled switch cases be an error in assert builds
260260
if(DEFINED LLVM_ENABLE_ASSERTIONS)
261-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=switch")
261+
check_cxx_compiler_flag("-Werror=switch" CXX_SUPPORTS_WERROR_SWITCH_FLAG)
262+
append_if(CXX_SUPPORTS_WERROR_SWITCH_FLAG "-Werror=switch" CMAKE_CXX_FLAGS)
263+
264+
check_cxx_compiler_flag("/we4062" CXX_SUPPORTS_WE4062)
265+
append_if(CXX_SUPPORTS_WE4062 "/we4062" CMAKE_CXX_FLAGS)
262266
endif()
263267

264268
check_cxx_compiler_flag("-Werror -Wdocumentation" CXX_SUPPORTS_DOCUMENTATION_FLAG)

stdlib/public/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ if(SWIFT_BUILD_STDLIB)
6161
add_subdirectory(stubs)
6262
add_subdirectory(core)
6363
add_subdirectory(SwiftOnoneSupport)
64-
add_subdirectory(Maths)
6564
endif()
6665

6766
if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)

stdlib/public/Darwin/CoreGraphics/CGFloat.swift.gyb

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -513,18 +513,111 @@ public func %=(lhs: inout CGFloat, rhs: CGFloat) {
513513
// tgmath
514514
//===----------------------------------------------------------------------===//
515515

516+
%from SwiftMathFunctions import *
517+
extension CGFloat: Real {
518+
% for func in ElementaryFunctions + RealFunctions:
519+
520+
@_alwaysEmitIntoClient
521+
public static func ${func.decl("CGFloat")} {
522+
return CGFloat(NativeType.${func.swiftName}(${func.params("", ".native")}))
523+
}
524+
% end
525+
526+
@_alwaysEmitIntoClient
527+
public static func pow(_ x: CGFloat, _ y: CGFloat) -> CGFloat {
528+
return CGFloat(NativeType.pow(x.native, y.native))
529+
}
530+
531+
@_alwaysEmitIntoClient
532+
public static func pow(_ x: CGFloat, _ n: Int) -> CGFloat {
533+
return CGFloat(NativeType.pow(x.native, n))
534+
}
535+
536+
@_alwaysEmitIntoClient
537+
public static func root(_ x: CGFloat, _ n: Int) -> CGFloat {
538+
return CGFloat(NativeType.root(x.native, n))
539+
}
540+
541+
@_alwaysEmitIntoClient
542+
public static func atan2(y: CGFloat, x: CGFloat) -> CGFloat {
543+
return CGFloat(NativeType.atan2(y: y.native, x: x.native))
544+
}
545+
546+
#if !os(Windows)
547+
@_alwaysEmitIntoClient
548+
public static func logGamma(_ x: CGFloat) -> CGFloat {
549+
return CGFloat(NativeType.logGamma(x.native))
550+
}
551+
#endif
552+
}
553+
554+
@available(swift, deprecated: 5.1, message: "Use `root(x, 3)`.")
555+
@_transparent
556+
public func cbrt(_ x: CGFloat) -> CGFloat {
557+
return CGFloat.root(x, 3)
558+
}
559+
560+
@available(swift, deprecated: 5.1, message: "Use CGFloat.minimum( ) or Swift.min( )")
561+
@_transparent
562+
public func fmin(_ x: CGFloat, _ y: CGFloat) -> CGFloat {
563+
return .minimum(x, y)
564+
}
565+
566+
@available(swift, deprecated: 5.1, message: "Use CGFloat.maximum( ) or Swift.max( )")
567+
@_transparent
568+
public func fmax(_ x: CGFloat, _ y: CGFloat) -> CGFloat {
569+
return .maximum(x, y)
570+
}
571+
572+
#if !os(Windows)
573+
@available(swift, deprecated: 5.1, message: "Use (logGamma(x), signGamma(x)).")
574+
@_transparent
575+
public func lgamma(_ x: CGFloat) -> (CGFloat, Int) {
576+
return (CGFloat.logGamma(x), CGFloat.signGamma(x) == .plus ? 1 : -1)
577+
}
578+
#endif
579+
580+
@available(swift, deprecated: 5.1, message: "Use `x.exponent` or `floor(log2(x))`.")
581+
@_transparent
582+
public func logb(_ x: CGFloat) -> CGFloat {
583+
return CGFloat.log2(x).rounded(.down)
584+
}
585+
586+
@available(swift, deprecated: 5.1, message: "Swift does not model dynamic rounding modes, use x.rounded(.toNearestOrEven).")
587+
@_transparent
588+
public func nearbyint(_ x: CGFloat) -> CGFloat {
589+
return x.rounded(.toNearestOrEven)
590+
}
591+
592+
@available(swift, deprecated: 5.1, message: "Use the .nextUp or .nextDown property.")
593+
@_transparent
594+
public func nextafter(_ x: CGFloat, _ y: CGFloat) -> CGFloat {
595+
return y > x ? x.nextUp : (y < x ? x.nextDown : y)
596+
}
597+
598+
@available(swift, deprecated: 5.1, message: "Swift does not model dynamic rounding modes, use x.rounded(.toNearestOrEven).")
599+
@_transparent
600+
public func rint(_ x: CGFloat) -> CGFloat {
601+
return x.rounded(.toNearestOrEven)
602+
}
603+
604+
@available(swift, deprecated: 5.1, message: "Use `gamma(x)`.")
605+
@_transparent
606+
public func tgamma(_ x: CGFloat) -> CGFloat {
607+
return CGFloat.gamma(x)
608+
}
609+
516610
%{
517611
UnaryFunctions = [
518612
'acos', 'asin', 'atan', 'cos', 'sin', 'tan',
519613
'acosh', 'asinh', 'atanh', 'cosh', 'sinh', 'tanh',
520614
'exp', 'exp2', 'expm1',
521-
'log', 'log10', 'log1p', 'log2', 'logb',
522-
'cbrt', 'erf', 'erfc', 'tgamma',
523-
'nearbyint', 'rint'
615+
'log', 'log10', 'log1p', 'log2',
616+
'erf', 'erfc',
524617
]
525618

526619
BinaryFunctions = [
527-
'atan2', 'hypot', 'pow', 'copysign', 'nextafter', 'fdim', 'fmax', 'fmin'
620+
'atan2', 'hypot', 'pow', 'copysign', 'fdim'
528621
]
529622
}%
530623

@@ -571,18 +664,12 @@ public func ldexp(_ x: CGFloat, _ n: Int) -> CGFloat {
571664
return CGFloat(ldexp(x.native, n))
572665
}
573666

574-
@available(swift, deprecated: 4.2, message: "use the exponent property.")
667+
@available(swift, deprecated: 4.2, obsoleted: 5.1, message: "use the exponent property.")
575668
@_transparent
576669
public func ilogb(_ x: CGFloat) -> Int {
577670
return Int(x.exponent)
578671
}
579672

580-
@_transparent
581-
public func lgamma(_ x: CGFloat) -> (CGFloat, Int) {
582-
let (value, sign) = lgamma(x.native)
583-
return (CGFloat(value), sign)
584-
}
585-
586673
@_transparent
587674
public func remquo(_ x: CGFloat, _ y: CGFloat) -> (CGFloat, Int) {
588675
let (rem, quo) = remquo(x.native, y.native)

stdlib/public/Maths/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

stdlib/public/Maths/Maths.swift.gyb

Lines changed: 0 additions & 51 deletions
This file was deleted.

stdlib/public/Platform/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ add_swift_target_library(swiftDarwin ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_
1818
SWIFT_COMPILE_FLAGS -Xfrontend -disable-objc-attr-requires-foundation-module "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
1919
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
2020
TARGET_SDKS ALL_APPLE_PLATFORMS
21-
SWIFT_MODULE_DEPENDS Maths
2221

2322
# This is overly conservative, but we have so few API notes files that
2423
# haven't migrated to the Swift repo that it's probably fine in practice.
@@ -34,7 +33,6 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O
3433
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
3534
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
3635
TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX HAIKU
37-
SWIFT_MODULE_DEPENDS Maths
3836
DEPENDS glibc_modulemap)
3937

4038
add_swift_target_library(swiftMSVCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
@@ -46,8 +44,7 @@ add_swift_target_library(swiftMSVCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_
4644

4745
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
4846
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
49-
TARGET_SDKS WINDOWS
50-
SWIFT_MODULE_DEPENDS Maths)
47+
TARGET_SDKS WINDOWS)
5148

5249
set(glibc_modulemap_target_list)
5350
foreach(sdk ${SWIFT_SDKS})

0 commit comments

Comments
 (0)