Skip to content

Commit dbea482

Browse files
authored
Refactor FixedWidthInteger init (swiftlang#36485)
1 parent 63ae329 commit dbea482

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

stdlib/public/core/Integers.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,20 +3160,25 @@ extension FixedWidthInteger {
31603160
self = Self(_truncatingBits: source._lowWord)
31613161
}
31623162
else {
3163-
let neg = source < (0 as T)
3164-
var result: Self = neg ? ~0 : 0
3165-
var shift: Self = 0
3166-
let width = Self(_truncatingBits: Self.bitWidth._lowWord)
3167-
for word in source.words {
3168-
guard shift < width else { break }
3169-
// Masking shift is OK here because we have already ensured
3170-
// that shift < Self.bitWidth. Not masking results in
3171-
// infinite recursion.
3172-
result ^= Self(_truncatingBits: neg ? ~word : word) &<< shift
3173-
shift += Self(_truncatingBits: Int.bitWidth._lowWord)
3174-
}
3175-
self = result
3163+
self = Self._truncatingInit(source)
3164+
}
3165+
}
3166+
3167+
@_alwaysEmitIntoClient
3168+
internal static func _truncatingInit<T: BinaryInteger>(_ source: T) -> Self {
3169+
let neg = source < (0 as T)
3170+
var result: Self = neg ? ~0 : 0
3171+
var shift: Self = 0
3172+
let width = Self(_truncatingBits: Self.bitWidth._lowWord)
3173+
for word in source.words {
3174+
guard shift < width else { break }
3175+
// Masking shift is OK here because we have already ensured
3176+
// that shift < Self.bitWidth. Not masking results in
3177+
// infinite recursion.
3178+
result ^= Self(_truncatingBits: neg ? ~word : word) &<< shift
3179+
shift += Self(_truncatingBits: Int.bitWidth._lowWord)
31763180
}
3181+
return result
31773182
}
31783183

31793184
@_transparent
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-frontend -O -parse-as-library -emit-sil -enable-ossa-modules %s | %FileCheck %s
2+
// REQUIRES: PTRSIZE=32,swift_stdlib_asserts
3+
4+
import Swift
5+
6+
// CHECK-LABEL: sil [noinline] @$s14jumpthreadtest3fooys6UInt64Vs5UInt8VF :
7+
// CHECK: bb0
8+
// CHECK: [[FUNC:%.*]] = function_ref @$ss17FixedWidthIntegerPsE15_truncatingInityxqd__SzRd__lFZs6UInt64V_s5UInt8VTgq5Tf4nd_n :
9+
// CHECK: apply [[FUNC]]
10+
// CHECK-NOT: bb1
11+
// CHECK-LABEL: } // end sil function '$s14jumpthreadtest3fooys6UInt64Vs5UInt8VF'
12+
@inlinable
13+
@inline(never)
14+
public func foo(_ p:UInt8) -> UInt64 {
15+
let q = UInt64(truncatingIfNeeded: p)
16+
return q
17+
}

0 commit comments

Comments
 (0)