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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
//===--- tgmath.swift.gyb -------------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// -*- swift -*-
// RUN: %empty-directory(%t)
// RUN: %gyb %s -o %t/tgmath.swift
// RUN: %line-directive %t/tgmath.swift -- %target-build-swift %t/tgmath.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %line-directive %t/tgmath.swift -- %target-run %t/a.out
// REQUIRES: executable_test
// REQUIRES: rdar94452524
#if canImport(Darwin)
import Darwin.C.tgmath
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
#else
#error("Unsupported platform")
#endif
#if (arch(i386) || arch(x86_64)) && !os(Windows)
typealias TestLiteralType = Float80
#else
typealias TestLiteralType = Double
#endif
import StdlibUnittest
let MathTests = TestSuite("TGMath")
func expectEqualWithTolerance<T>(_ expected: TestLiteralType, _ actual: T,
ulps allowed: T = 3,
file: String = #file, line: UInt = #line)
where T: BinaryFloatingPoint {
if actual == T(expected) || actual.isNaN && expected.isNaN {
return
}
// Compute error in ulp, compare to tolerance.
let absoluteError = T(abs(TestLiteralType(actual) - expected))
let ulpError = absoluteError / T(expected).ulp
expectTrue(ulpError <= allowed,
"\(actual) != \(expected) as \(T.self)" +
"\n \(ulpError)-ulp error exceeds \(allowed)-ulp tolerance.",
file: file, line: line)
}
%{
unary = [
'acos', 'asin', 'atan',
'cos', 'sin', 'tan',
'acosh', 'asinh', 'atanh',
'cosh', 'sinh', 'tanh',
'exp', 'exp2', 'expm1',
'log', 'log2', 'log1p', 'log10', 'logb',
'fabs', 'cbrt', 'sqrt',
'erf', 'erfc',
'tgamma',
'ceil', 'floor', 'nearbyint', 'rint', 'trunc',
]
binary = [
'atan2', 'hypot', 'pow', 'fmod', 'copysign', 'nextafter',
'fdim', 'fmin', 'fmax'
]
}%
internal protocol TGMath: BinaryFloatingPoint {
%for f in unary:
static func _${f}(_ x: Self) -> Self
%end
%for f in binary:
static func _${f}(_ x: Self, _ y: Self) -> Self
%end
static func _remquo(_ x: Self, _ y: Self) -> (Self, Int)
static func _fma(_ x: Self, _ y: Self, _ z: Self) -> Self
#if !os(Windows) && !os(OpenBSD)
static func _lgamma(_ x: Self) -> (Self, Int)
#endif
static func _modf(_ x: Self) -> (Self, Self)
static func _scalbn(_ x: Self, _ n: Int) -> Self
static func _frexp(_ x: Self) -> (Self, Int)
static func _ilogb(_ x: Self) -> Int
}
internal extension TGMath {
static func allTests() {
/* Default tolerance is 3 ulps unless specified otherwise. It's OK to relax
* this as needed for new platforms, as these tests are *not* intended to
* validate the math library--they are only intended to check that the
* Swift bindings are calling the right functions in the math library. */
expectEqualWithTolerance(1.1863995522992575361931268186727044683, Self._acos(0.375))
expectEqualWithTolerance(0.3843967744956390830381948729670469737, Self._asin(0.375))
expectEqualWithTolerance(0.3587706702705722203959200639264604997, Self._atan(0.375))
expectEqualWithTolerance(0.9305076219123142911494767922295555080, Self._cos(0.375))
expectEqualWithTolerance(0.3662725290860475613729093517162641571, Self._sin(0.375))
expectEqualWithTolerance(0.3936265759256327582294137871012180981, Self._tan(0.375))
expectEqualWithTolerance(0.4949329230945269058895630995767185785, Self._acosh(1.125))
expectEqualWithTolerance(0.9670596312833237113713762009167286709, Self._asinh(1.125))
expectEqualWithTolerance(0.7331685343967135223291211023213964500, Self._atanh(0.625))
expectEqualWithTolerance(1.0711403467045867672994980155670160493, Self._cosh(0.375))
expectEqualWithTolerance(0.3838510679136145687542956764205024589, Self._sinh(0.375))
expectEqualWithTolerance(0.3583573983507859463193602315531580424, Self._tanh(0.375))
expectEqualWithTolerance(1.4549914146182013360537936919875185083, Self._exp(0.375))
expectEqualWithTolerance(1.2968395546510096659337541177924511598, Self._exp2(0.375))
expectEqualWithTolerance(0.4549914146182013360537936919875185083, Self._expm1(0.375))
expectEqualWithTolerance(-0.980829253011726236856451127452003999, Self._log(0.375))
expectEqualWithTolerance(-1.415037499278843818546261056052183491, Self._log2(0.375))
expectEqualWithTolerance(0.3184537311185346158102472135905995955, Self._log1p(0.375))
expectEqualWithTolerance(-0.425968732272281148346188780918363771, Self._log10(0.375))
expectEqual(-2, Self._logb(0.375))
expectEqual(0.375, Self._fabs(-0.375))
expectEqualWithTolerance(0.7211247851537041911608191553900547941, Self._cbrt(0.375))
expectEqualWithTolerance(0.6123724356957945245493210186764728479, Self._sqrt(0.375))
expectEqualWithTolerance(0.4041169094348222983238250859191217675, Self._erf(0.375))
expectEqualWithTolerance(0.5958830905651777016761749140808782324, Self._erfc(0.375))
expectEqualWithTolerance(2.3704361844166009086464735041766525098, Self._tgamma(0.375))
#if !os(Windows) && !os(OpenBSD)
expectEqualWithTolerance( -0.11775527074107877445136203331798850, Self._lgamma(1.375).0, ulps: 16)
expectEqual(1, Self._lgamma(1.375).1)
#endif
expectEqual(1, Self._ceil(0.375))
expectEqual(0, Self._floor(0.375))
expectEqual(0, Self._nearbyint(0.375))
expectEqual(0, Self._rint(0.375))
expectEqual(0, Self._trunc(0.375))
expectEqual(0, Self._ceil(-0.625))
expectEqual(-1, Self._floor(-0.625))
expectEqual(-1, Self._nearbyint(-0.625))
expectEqual(-1, Self._rint(-0.625))
expectEqual(0, Self._trunc(-0.625))
expectEqual(0, Self._ceil(-0.5))
expectEqual(-1, Self._floor(-0.5))
expectEqual(-0.0, Self._nearbyint(-0.5))
expectEqual(-0.0, Self._rint(-0.5))
expectEqual(0, Self._trunc(-0.5))
expectEqualWithTolerance(0.54041950027058415544357836460859991, Self._atan2(0.375, 0.625))
expectEqualWithTolerance(0.72886898685566255885926910969319788, Self._hypot(0.375, 0.625))
expectEqualWithTolerance(0.54171335479545025876069682133938570, Self._pow(0.375, 0.625))
expectEqual(0.375, Self._fmod(1, 0.625))
expectEqual(-0.375, Self._copysign(0.375, -0.625))
expectEqual(Self(0.375).nextUp, Self._nextafter(0.375, 1))
expectEqual(Self(0.375).nextDown, Self._nextafter(0.375, 0))
expectEqual(0, Self._fdim(0.375, 0.625))
expectEqual(0.375, Self._fmin(0.375, 0.625))
expectEqual(0.625, Self._fmax(0.375, 0.625))
expectEqual(-Self.ulpOfOne*Self.ulpOfOne,
Self._fma(1 + .ulpOfOne, 1 - .ulpOfOne, -1))
expectEqual((1.0, 0.125), Self._modf(1.125))
expectEqual(2.5, Self._scalbn(0.625, 2))
expectEqual((0.625, 2), Self._frexp(2.5))
expectEqual(1, Self._ilogb(2.5))
#if os(Linux) && arch(x86_64)
// FIXME: https://github.com/apple/swift/issues/49782
// double-precision remquo is broken in the glibc in 14.04. Disable this
// test for all Linux in the short-term to un-FAIL the build.
if Self.significandBitCount != 52 {
expectEqual(-0.25, Self._remquo(16, 0.625).0)
expectEqual(2, Self._remquo(16, 0.625).1 & 7)
}
#else
expectEqual(-0.25, Self._remquo(16, 0.625).0)
expectEqual(2, Self._remquo(16, 0.625).1 & 7)
#endif
}
}
%for T in ['Float', 'Double', 'CGFloat', 'Float80']:
% if T == 'Float80':
#if (arch(i386) || arch(x86_64)) && !os(Windows)
% elif T == 'CGFloat':
#if _runtime(_ObjC) && canImport(CoreGraphics)
import CoreGraphics
% end
extension ${T}: TGMath {
% for f in unary:
static func _${f}(_ x: ${T}) -> ${T} { return ${f}(x) }
% end
%for f in binary:
static func _${f}(_ x: ${T}, _ y: ${T}) -> ${T} { return ${f}(x, y) }
%end
static func _remquo(_ x: ${T}, _ y: ${T}) -> (${T}, Int) { return remquo(x, y) }
static func _fma(_ x: ${T}, _ y: ${T}, _ z: ${T}) -> ${T} { return fma(x, y, z) }
#if !os(Windows) && !os(OpenBSD)
static func _lgamma(_ x: ${T}) -> (${T}, Int) { return lgamma(x) }
#endif
static func _modf(_ x: ${T}) -> (${T}, ${T}) { return modf(x) }
static func _scalbn(_ x: ${T}, _ n: Int) -> ${T} { return scalbn(x, n) }
static func _frexp(_ x: ${T}) -> (${T}, Int) { return frexp(x) }
static func _ilogb(_ x: ${T}) -> Int { return ilogb(x) }
}
MathTests.test("${T}") {
${T}.allTests()
% if T in ['Double','CGFloat']:
// Functions that are defined only for Double and CGFloat
expectEqualWithTolerance(0.99750156206604, j0(0.1), ulps: 16)
expectEqualWithTolerance(0.049937526036242, j1(0.1), ulps: 16)
expectEqualWithTolerance(1.2229926610356451e-22, jn(11, 0.1), ulps: 16)
expectEqualWithTolerance(-1.5342386513503667, y0(0.1), ulps: 16)
expectEqualWithTolerance(-6.458951094702027, y1(0.1), ulps: 16)
expectEqualWithTolerance(-2.3662012944869576e+20, yn(11, 0.1), ulps: 16)
% end
}
% if T in ['CGFloat', 'Float80']:
#endif
% end
%end
runAllTests()
|