Skip to content

Commit 6ff95da

Browse files
Mark several C macros imported from <float.h> as deprecated. (#6796)
* Mark several C macros imported from <float.h> as deprecated. These macros all have straightforward replacements in terms of static properties on FloatingPoint or BinaryFloatingPoint. It is necessary to add 1 in a few places because of differences between how C and Swift count significand bits and normalize the significand, but this is expected to have minimal impact on code in practice (and when it does have impact, using the Swift definition is generally simpler). * Address review notes from @moiseev. This fixes <rdar://problem/27871465>
1 parent 3df2643 commit 6ff95da

File tree

7 files changed

+181
-64
lines changed

7 files changed

+181
-64
lines changed

stdlib/public/Platform/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set(swift_platform_sources
77
tgmath.swift.gyb)
88

99
add_swift_library(swiftDarwin ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
10-
Darwin.swift
10+
Darwin.swift.gyb
1111
${swift_platform_sources}
1212
POSIXError.swift
1313
MachError.swift
@@ -18,7 +18,7 @@ add_swift_library(swiftDarwin ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
1818
API_NOTES_NON_OVERLAY)
1919

2020
add_swift_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
21-
Glibc.swift
21+
Glibc.swift.gyb
2222
${swift_platform_sources}
2323

2424
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"

stdlib/public/Platform/Darwin.swift

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import Darwin // Clang module
14+
15+
public let MAP_FAILED =
16+
UnsafeMutableRawPointer(bitPattern: -1)! as UnsafeMutableRawPointer!
17+
18+
// Constants defined by <math.h>
19+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.")
20+
public let M_PI = Double.pi
21+
22+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting.")
23+
public let M_PI_2 = Double.pi / 2
24+
25+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 4' or '.pi / 4' to get the value of correct type and avoid casting.")
26+
public let M_PI_4 = Double.pi / 4
27+
28+
@available(swift, deprecated: 3.0, message: "Please use '2.squareRoot()'.")
29+
public let M_SQRT2 = 2.squareRoot()
30+
31+
@available(swift, deprecated: 3.0, message: "Please use '0.5.squareRoot()'.")
32+
public let M_SQRT1_2 = 0.5.squareRoot()
33+
34+
// Constants defined by <float.h>
35+
@available(swift, deprecated: 3.0, message: "Please use 'T.radix' to get the radix of a FloatingPoint type 'T'.")
36+
public let FLT_RADIX = Double.radix
37+
38+
%for type, prefix in [('Float', 'FLT'), ('Double', 'DBL'), ('Float80', 'LDBL')]:
39+
% if type == "Float80":
40+
#if arch(i386) || arch(x86_64)
41+
% end
42+
// Where does the 1 come from? C counts the usually-implicit leading
43+
// significand bit, but Swift does not. Neither is really right or wrong.
44+
@available(swift, deprecated: 3.0, message: "Please use '${type}.significandBitCount + 1'.")
45+
public let ${prefix}_MANT_DIG = ${type}.significandBitCount + 1
46+
47+
// Where does the 1 come from? C models floating-point numbers as having a
48+
// significand in [0.5, 1), but Swift (following IEEE 754) considers the
49+
// significand to be in [1, 2). This rationale applies to ${prefix}_MIN_EXP
50+
// as well.
51+
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude.exponent + 1'.")
52+
public let ${prefix}_MAX_EXP = ${type}.greatestFiniteMagnitude.exponent + 1
53+
54+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude.exponent + 1'.")
55+
public let ${prefix}_MIN_EXP = ${type}.leastNormalMagnitude.exponent + 1
56+
57+
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.")
58+
public let ${prefix}_MAX = ${type}.greatestFiniteMagnitude
59+
60+
@available(swift, deprecated: 3.0, message: "Please use '${type}.ulpOfOne' or '.ulpOfOne'.")
61+
public let ${prefix}_EPSILON = ${type}.ulpOfOne
62+
63+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude' or '.leastNormalMagnitude'.")
64+
public let ${prefix}_MIN = ${type}.leastNormalMagnitude
65+
66+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.")
67+
public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude
68+
69+
% if type == "Float80":
70+
#endif
71+
% end
72+
%end

stdlib/public/Platform/Glibc.swift

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import SwiftGlibc // Clang module
14+
15+
public let MAP_FAILED =
16+
UnsafeMutableRawPointer(bitPattern: -1)! as UnsafeMutableRawPointer!
17+
18+
// Constants defined by <math.h>
19+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.")
20+
public let M_PI = Double.pi
21+
22+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting.")
23+
public let M_PI_2 = Double.pi / 2
24+
25+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 4' or '.pi / 4' to get the value of correct type and avoid casting.")
26+
public let M_PI_4 = Double.pi / 4
27+
28+
@available(swift, deprecated: 3.0, message: "Please use '2.squareRoot()'.")
29+
public let M_SQRT2 = 2.squareRoot()
30+
31+
@available(swift, deprecated: 3.0, message: "Please use '0.5.squareRoot()'.")
32+
public let M_SQRT1_2 = 0.5.squareRoot()
33+
34+
// Constants defined by <float.h>
35+
@available(swift, deprecated: 3.0, message: "Please use 'T.radix' to get the radix of a FloatingPoint type 'T'.")
36+
public let FLT_RADIX = Double.radix
37+
38+
%for type, prefix in [('Float', 'FLT'), ('Double', 'DBL'), ('Float80', 'LDBL')]:
39+
% if type == "Float80":
40+
#if arch(i386) || arch(x86_64)
41+
% end
42+
// Where does the 1 come from? C counts the usually-implicit leading
43+
// significand bit, but Swift does not. Neither is really right or wrong.
44+
@available(swift, deprecated: 3.0, message: "Please use '${type}.significandBitCount + 1'.")
45+
public let ${prefix}_MANT_DIG = ${type}.significandBitCount + 1
46+
47+
// Where does the 1 come from? C models floating-point numbers as having a
48+
// significand in [0.5, 1), but Swift (following IEEE 754) considers the
49+
// significand to be in [1, 2). This rationale applies to ${prefix}_MIN_EXP
50+
// as well.
51+
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude.exponent + 1'.")
52+
public let ${prefix}_MAX_EXP = ${type}.greatestFiniteMagnitude.exponent + 1
53+
54+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude.exponent + 1'.")
55+
public let ${prefix}_MIN_EXP = ${type}.leastNormalMagnitude.exponent + 1
56+
57+
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.")
58+
public let ${prefix}_MAX = ${type}.greatestFiniteMagnitude
59+
60+
@available(swift, deprecated: 3.0, message: "Please use '${type}.ulpOfOne' or '.ulpOfOne'.")
61+
public let ${prefix}_EPSILON = ${type}.ulpOfOne
62+
63+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude' or '.leastNormalMagnitude'.")
64+
public let ${prefix}_MIN = ${type}.leastNormalMagnitude
65+
66+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.")
67+
public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude
68+
69+
% if type == "Float80":
70+
#endif
71+
% end
72+
%end

test/stdlib/FloatConstants.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
4+
import Darwin
5+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
6+
import Glibc
7+
#endif
8+
9+
_ = FLT_RADIX // expected-warning {{is deprecated}}
10+
11+
_ = FLT_MANT_DIG // expected-warning {{is deprecated}}
12+
_ = FLT_MIN_EXP // expected-warning {{is deprecated}}
13+
_ = FLT_MAX_EXP // expected-warning {{is deprecated}}
14+
_ = FLT_MAX // expected-warning {{is deprecated}}
15+
_ = FLT_EPSILON // expected-warning {{is deprecated}}
16+
_ = FLT_MIN // expected-warning {{is deprecated}}
17+
_ = FLT_TRUE_MIN // expected-warning {{is deprecated}}
18+
19+
_ = DBL_MANT_DIG // expected-warning {{is deprecated}}
20+
_ = DBL_MIN_EXP // expected-warning {{is deprecated}}
21+
_ = DBL_MAX_EXP // expected-warning {{is deprecated}}
22+
_ = DBL_MAX // expected-warning {{is deprecated}}
23+
_ = DBL_EPSILON // expected-warning {{is deprecated}}
24+
_ = DBL_MIN // expected-warning {{is deprecated}}
25+
_ = DBL_TRUE_MIN // expected-warning {{is deprecated}}
26+
27+
#if arch(i386) || arch (x86_64)
28+
_ = LDBL_MANT_DIG // expected-warning {{is deprecated}}
29+
_ = LDBL_MIN_EXP // expected-warning {{is deprecated}}
30+
_ = LDBL_MAX_EXP // expected-warning {{is deprecated}}
31+
_ = LDBL_MAX // expected-warning {{is deprecated}}
32+
_ = LDBL_EPSILON // expected-warning {{is deprecated}}
33+
_ = LDBL_MIN // expected-warning {{is deprecated}}
34+
_ = LDBL_TRUE_MIN // expected-warning {{is deprecated}}
35+
#endif
File renamed without changes.

0 commit comments

Comments
 (0)