mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
CGFloat is 32-bit on 32-bit architectures and 64-bit on 64-bit architectures for historical reasons. Rather than having it alias either Float (32-bit) or Double (64-bit), introduce a distinct struct type for CGFloat. CGFloat provides a complete set of comparisons and arithmetic operators (including tgmath functions), initializers allows explicit conversion between it an Int, UInt, Float, and Double, as well as conforming to all of the protocols that Float/Double do. This formulation of CGFloat makes use of CGFloat architecture-independent, although it still requires a number of casts. Fixes <rdar://problem/17224725> Swift SVN r19689
306 lines
8.1 KiB
Swift
306 lines
8.1 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', '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', 'sqrt',
|
|
'ceil', 'floor', 'nearbyint', 'rint', 'round', 'trunc',
|
|
]
|
|
|
|
# (T, T) -> T
|
|
BinaryFunctions = ['atan2', 'hypot', 'pow',
|
|
# FIXME: rdar://17275152 call to fmodf causes deserialization crash
|
|
# FIXME: When we fix this, also add fmod for CGFloat to CoreGraphics
|
|
# '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}, UnsafePointer<CInt>) -> ${CT}
|
|
@transparent public
|
|
func lgamma(x: ${T}) -> (${T}, Int) {
|
|
var sign = CInt(0)
|
|
let value = withUnsafePointer(&sign) {
|
|
(signp: UnsafePointer<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:
|