forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatingPointOperations.swift.gyb
120 lines (92 loc) · 3.08 KB
/
FloatingPointOperations.swift.gyb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===--- FloatingPointOperations.swift.gyb --------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from SwiftIntTypes import *
# Number of bits in the Builtin.Word type
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
}%
/// The set of possible IEEE 754 "classes"
public enum FloatingPointClassification {
case SignalingNaN
case QuietNaN
case NegativeInfinity
case NegativeNormal
case NegativeSubnormal
case NegativeZero
case PositiveZero
case PositiveSubnormal
case PositiveNormal
case PositiveInfinity
}
extension FloatingPointClassification : Equatable {}
public
func ==(lhs: FloatingPointClassification, rhs: FloatingPointClassification) -> Bool {
switch (lhs, rhs) {
case (.SignalingNaN, .SignalingNaN),
(.QuietNaN, .QuietNaN),
(.NegativeInfinity, .NegativeInfinity),
(.NegativeNormal, .NegativeNormal),
(.NegativeSubnormal, .NegativeSubnormal),
(.NegativeZero, .NegativeZero),
(.PositiveZero, .PositiveZero),
(.PositiveSubnormal, .PositiveSubnormal),
(.PositiveNormal, .PositiveNormal),
(.PositiveInfinity, .PositiveInfinity):
return true
default:
return false
}
}
/// A set of common requirements for Swift's floating point types.
public protocol FloatingPointType : Strideable {
typealias _BitsType
@warn_unused_result
static func _fromBitPattern(bits: _BitsType) -> Self
@warn_unused_result
func _toBitPattern() -> _BitsType
% for src_ty in all_integer_types(word_bits):
/// Create an instance initialized to `value`.
init(_ value: ${src_ty.stdlib_name})
% end
/// The positive infinity.
static var infinity: Self { get }
/// A quiet NaN.
static var NaN: Self { get }
/// A quiet NaN.
static var quietNaN: Self { get }
//
// IEEE 754-2008 Non-computational operations.
//
/// The IEEE 754 "class" of this type.
var floatingPointClass: FloatingPointClassification { get }
/// `true` iff `self` is negative.
var isSignMinus: Bool { get }
/// `true` iff `self` is normal (not zero, subnormal, infinity, or
/// NaN).
var isNormal: Bool { get }
/// `true` iff `self` is zero, subnormal, or normal (not infinity
/// or NaN).
var isFinite: Bool { get }
/// `true` iff `self` is +0.0 or -0.0.
var isZero: Bool { get }
/// `true` iff `self` is subnormal.
var isSubnormal: Bool { get }
/// `true` iff `self` is infinity.
var isInfinite: Bool { get }
/// `true` iff `self` is NaN.
var isNaN: Bool { get }
/// `true` iff `self` is a signaling NaN.
var isSignaling: Bool { get }
// Not implemented, because it only makes sense for decimal floating point.
// Binary floating point numbers are always canonical.
// func isCanonical() -> Bool
}