mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This reapplies commit r22864 - it is not changing the public api as we initially thought. sqrt() was never available without importing Darwin. This change only changes where sqrt() gets "forwarded" to. Before 'sqrt' called the builtin '_sqrt' defined in BuiltinMath now it just calls the math library's 'sqrt' function. I also added a stdlib test. rdar://18371371 Swift SVN r22870
303 lines
8.0 KiB
Swift
303 lines
8.0 KiB
Swift
//===--- tgmath.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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
%{
|
|
|
|
# Don't need 64-bit (Double/CDouble) overlays. The ordinary C imports work fine.
|
|
# FIXME: need 80-bit (Float80/long double) versions when long double is imported
|
|
overlayFloatBits = [32] # 80
|
|
allFloatBits = [32, 64] # 80
|
|
|
|
def floatName(bits):
|
|
if bits == 32:
|
|
return 'Float'
|
|
if bits == 64:
|
|
return 'Double'
|
|
if bits == 80:
|
|
return 'Float80'
|
|
|
|
def cFloatName(bits):
|
|
if bits == 32:
|
|
return 'CFloat'
|
|
if bits == 64:
|
|
return 'CDouble'
|
|
if bits == 80:
|
|
return 'CLongDouble'
|
|
|
|
def cFuncSuffix(bits):
|
|
if bits == 32:
|
|
return 'f'
|
|
if bits == 64:
|
|
return ''
|
|
if bits == 80:
|
|
return 'l'
|
|
|
|
# Each of the following lists is ordered to match math.h
|
|
|
|
# (T) -> T
|
|
# These functions do not have a corresponding LLVM intrinsic
|
|
UnaryFunctions = ['acos', 'asin', 'atan', 'tan',
|
|
'acosh', 'asinh', 'atanh', 'cosh', 'sinh', 'tanh',
|
|
'expm1',
|
|
'log1p', 'logb',
|
|
'cbrt', 'sqrt', 'erf', 'erfc', 'tgamma',
|
|
]
|
|
|
|
# These functions have a corresponding LLVM intrinsic
|
|
# We call this intrinsic via the Builtin method so keep this list in
|
|
# sync with core/BuiltinMath.swift.gyb
|
|
UnaryIntrinsicFunctions = ['cos', 'sin',
|
|
'exp', 'exp2',
|
|
'log', 'log10', 'log2',
|
|
'fabs',
|
|
'ceil', 'floor', 'nearbyint', 'rint', 'round', 'trunc',
|
|
]
|
|
|
|
# (T, T) -> T
|
|
BinaryFunctions = ['atan2', 'hypot', 'pow', 'fmod',
|
|
'remainder', 'copysign', 'nextafter', 'fdim', 'fmax', 'fmin']
|
|
|
|
# These functions have special implementations.
|
|
OtherFunctions = ['fpclassify',
|
|
'isnormal', 'isfinite', 'isinf', 'isnan', 'signbit',
|
|
'modf', 'ldexp', 'frexp', 'ilogb', 'scalbn', 'lgamma',
|
|
'remquo', 'nan', 'fma',
|
|
'jn', 'yn']
|
|
|
|
# These functions are imported correctly as-is.
|
|
OkayFunctions = ['j0', 'j1', 'y0', 'y1']
|
|
|
|
# These functions are not supported for various reasons.
|
|
UnhandledFunctions = ['math_errhandling', 'scalbln',
|
|
'lrint', 'lround', 'llrint', 'llround', 'nexttoward',
|
|
'isgreater', 'isgreaterequal', 'isless', 'islessequal',
|
|
'islessgreater', 'isunordered', '__exp10',
|
|
'__sincos', '__cospi', '__sinpi', '__tanpi', '__sincospi']
|
|
|
|
|
|
def AllFloatTypes():
|
|
for bits in allFloatBits:
|
|
yield floatName(bits), cFloatName(bits), cFuncSuffix(bits)
|
|
|
|
def OverlayFloatTypes():
|
|
for bits in overlayFloatBits:
|
|
yield floatName(bits), cFloatName(bits), cFuncSuffix(bits)
|
|
|
|
def TypedUnaryFunctions():
|
|
for ufunc in UnaryFunctions:
|
|
for bits in overlayFloatBits:
|
|
yield floatName(bits), cFloatName(bits), cFuncSuffix(bits), ufunc
|
|
|
|
def TypedUnaryIntrinsicFunctions():
|
|
for ufunc in UnaryIntrinsicFunctions:
|
|
for bits in allFloatBits:
|
|
yield floatName(bits), ufunc
|
|
|
|
def TypedBinaryFunctions():
|
|
for bfunc in BinaryFunctions:
|
|
for bits in overlayFloatBits:
|
|
yield floatName(bits), cFloatName(bits), cFuncSuffix(bits), bfunc
|
|
|
|
}%
|
|
|
|
|
|
// Unary functions
|
|
// Note these do not have a corresponding LLVM intrinsic
|
|
% for T, CT, f, ufunc in TypedUnaryFunctions():
|
|
@transparent
|
|
public func ${ufunc}(x: ${T}) -> ${T} {
|
|
return ${T}(${ufunc}${f}(${CT}(x)))
|
|
}
|
|
|
|
% end
|
|
|
|
// Unary intrinsic functions
|
|
// Note these have a corresponding LLVM intrinsic
|
|
% for T, ufunc in TypedUnaryIntrinsicFunctions():
|
|
@transparent
|
|
public func ${ufunc}(x: ${T}) -> ${T} {
|
|
return _${ufunc}(x)
|
|
}
|
|
|
|
% end
|
|
|
|
// Binary functions
|
|
|
|
% for T, CT, f, bfunc in TypedBinaryFunctions():
|
|
@transparent
|
|
public func ${bfunc}(lhs: ${T}, rhs: ${T}) -> ${T} {
|
|
return ${T}(${bfunc}${f}(${CT}(lhs), ${CT}(rhs)))
|
|
}
|
|
|
|
% end
|
|
|
|
// Other functions
|
|
|
|
% # These are AllFloatTypes not OverlayFloatTypes because of the Int return.
|
|
% for T, CT, f in AllFloatTypes():
|
|
% if f == '':
|
|
% f = 'd'
|
|
@transparent
|
|
public func fpclassify(x: ${T}) -> Int {
|
|
return Int(__fpclassify${f}(${CT}(x)))
|
|
}
|
|
|
|
% end
|
|
|
|
% # These are AllFloatTypes not OverlayFloatTypes because we need to cover
|
|
% # them all because C's declarations are compiler builtins.
|
|
% for T, CT, f in AllFloatTypes():
|
|
@transparent
|
|
public func isnormal(value: ${T}) -> Bool {
|
|
return value.isNormal
|
|
}
|
|
|
|
@transparent
|
|
public func isfinite(value: ${T}) -> Bool {
|
|
return value.isFinite
|
|
}
|
|
|
|
@transparent
|
|
public func isinf(value: ${T}) -> Bool {
|
|
return value.isInfinite
|
|
}
|
|
|
|
@transparent
|
|
public func isnan(value: ${T}) -> Bool {
|
|
return value.isNaN
|
|
}
|
|
|
|
@transparent
|
|
public func signbit(value: ${T}) -> Int {
|
|
return value.isSignMinus ? 1 : 0
|
|
}
|
|
|
|
% end
|
|
|
|
% # These are AllFloatTypes not OverlayFloatTypes because of the tuple return.
|
|
% for T, CT, f in AllFloatTypes():
|
|
@transparent
|
|
public func modf(value: ${T}) -> (${T}, ${T}) {
|
|
var ipart = ${CT}(0)
|
|
let fpart = modf${f}(${CT}(value), &ipart)
|
|
return (${T}(ipart), ${T}(fpart))
|
|
}
|
|
|
|
% end
|
|
|
|
% # This is AllFloatTypes not OverlayFloatTypes because of the Int parameter.
|
|
% for T, CT, f in AllFloatTypes():
|
|
@transparent
|
|
public func ldexp(x: ${T}, n: Int) -> ${T} {
|
|
return ${T}(ldexp${f}(${CT}(x), CInt(n)))
|
|
}
|
|
|
|
% end
|
|
|
|
% # This is AllFloatTypes not OverlayFloatTypes because of the tuple return.
|
|
% for T, CT, f in AllFloatTypes():
|
|
@transparent
|
|
public func frexp(value: ${T}) -> (${T}, Int) {
|
|
var exp = CInt(0)
|
|
let frac = frexp${f}(${CT}(value), &exp)
|
|
return (${T}(frac), Int(exp))
|
|
}
|
|
|
|
% end
|
|
|
|
% # This would be AllFloatTypes not OverlayFloatTypes because of the Int return.
|
|
% # ... except we need an asmname to avoid an overload ambiguity.
|
|
% for T, CT, f in OverlayFloatTypes():
|
|
@transparent
|
|
public func ilogb(x: ${T}) -> Int {
|
|
return Int(ilogb${f}(${CT}(x)))
|
|
}
|
|
|
|
% end
|
|
@asmname("ilogb")
|
|
func _swift_Darwin_ilogb(value: CDouble) -> CInt
|
|
@transparent
|
|
public func ilogb(x: Double) -> Int {
|
|
return Int(_swift_Darwin_ilogb(CDouble(x)))
|
|
}
|
|
|
|
|
|
% # This is AllFloatTypes not OverlayFloatTypes because of the Int parameter.
|
|
% for T, CT, f in AllFloatTypes():
|
|
@transparent
|
|
public func scalbn(x: ${T}, n: Int) -> ${T} {
|
|
return ${T}(scalbn${f}(${CT}(x), CInt(n)))
|
|
}
|
|
|
|
% end
|
|
|
|
% # This is AllFloatTypes not OverlayFloatTypes because of the tuple return.
|
|
% for T, CT, f in AllFloatTypes():
|
|
% # The real lgamma_r is not imported because it hides behind macro _REENTRANT.
|
|
@asmname("lgamma${f}_r")
|
|
func _swift_Darwin_lgamma${f}_r(${CT}, UnsafeMutablePointer<CInt>) -> ${CT}
|
|
@transparent
|
|
public func lgamma(x: ${T}) -> (${T}, Int) {
|
|
var sign = CInt(0)
|
|
let value = withUnsafeMutablePointer(&sign) {
|
|
(signp: UnsafeMutablePointer<CInt>) -> ${CT} in
|
|
return _swift_Darwin_lgamma${f}_r(${CT}(x), signp)
|
|
}
|
|
return (${T}(value), Int(sign))
|
|
}
|
|
|
|
% end
|
|
|
|
% # This is AllFloatTypes not OverlayFloatTypes because of the tuple return.
|
|
% for T, CT, f in AllFloatTypes():
|
|
@transparent
|
|
public func remquo(x: ${T}, y: ${T}) -> (${T}, Int) {
|
|
var quo = CInt(0)
|
|
let rem = remquo${f}(${CT}(x), ${CT}(y), &quo)
|
|
return (${T}(rem), Int(quo))
|
|
}
|
|
|
|
% end
|
|
|
|
% for T, CT, f in OverlayFloatTypes():
|
|
@transparent
|
|
public func nan(tag: String) -> ${T} {
|
|
return ${T}(nan${f}(tag))
|
|
}
|
|
|
|
% end
|
|
|
|
% for T, CT, f in OverlayFloatTypes():
|
|
@transparent
|
|
public func fma(x: ${T}, y: ${T}, z: ${T}) -> ${T} {
|
|
return ${T}(fma${f}(${CT}(x), ${CT}(y), ${CT}(z)))
|
|
}
|
|
|
|
% end
|
|
|
|
% # These C functions only support double. The overlay fixes the Int parameter.
|
|
@transparent
|
|
public func jn(n: Int, x: Double) -> Double {
|
|
return jn(CInt(n), x)
|
|
}
|
|
|
|
@transparent
|
|
public func yn(n: Int, x: Double) -> Double {
|
|
return yn(CInt(n), x)
|
|
}
|
|
|
|
% end
|
|
|
|
// ${'Local Variables'}:
|
|
// eval: (read-only-mode 1)
|
|
// End:
|