Skip to content

Commit

Permalink
Yams: add an alias to explicitly dispatch pow (#281)
Browse files Browse the repository at this point in the history
We must disambiguate `Double.pow(_: Double, _: Double) -> Double` and
`__C.pow(_: Double, _: Double) -> Double` as the Swift for TensorFlow
branch adds the former into the standard library.  The explicit module
dispatch ensures that we make the overload resolution unambiguous
allowing building Yams.
  • Loading branch information
compnerd authored Oct 26, 2020
1 parent ae37c14 commit 682d498
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Sources/Yams/Representer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ import Foundation
import ucrt
#endif

#if os(iOS) || os(macOS) || os(watchOS) || os(tvOS)
import Darwin
fileprivate let cpow: (_: Double, _: Double) -> Double = Darwin.pow
#elseif os(Windows)
import ucrt
fileprivate let cpow: (_: Double, _: Double) -> Double = ucrt.pow
#else
import Glibc
fileprivate let cpow: (_: Double, _: Double) -> Double = Glibc.pow
#endif

public extension Node {
/// Initialize a `Node` with a value of `NodeRepresentable`.
///
Expand Down Expand Up @@ -130,13 +141,11 @@ private extension TimeInterval {
func separateFractionalSecond(withPrecision precision: Int) -> (integral: TimeInterval, fractional: Int) {
var integral = 0.0
let fractional = modf(self, &integral)
// Can't use `pow` free function due to https://bugs.swift.org/browse/TF-1203.
// TODO: Remove condition after that bug is fixed.
#if canImport(TensorFlow)
let radix = Double.pow(10.0, Double(precision))
#else
let radix = pow(10.0, Double(precision))
#endif

// TODO(TF-1203): Can't use `pow` free function due to
// https://bugs.swift.org/browse/TF-1203.
let radix = cpow(10.0, Double(precision))

let rounded = Int((fractional * radix).rounded())
let quotient = rounded / Int(radix)
return quotient != 0 ? // carry-up?
Expand Down

0 comments on commit 682d498

Please sign in to comment.