Skip to content

Commit 9ff6aa5

Browse files
committed
Make _resigned generic over fixed width + unsigned
1 parent 48af9bc commit 9ff6aa5

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

Sources/RandomKit/Extensions/Swift/Integer+RandomKit.swift

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,15 @@ extension Int8: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, Rando
574574

575575
}
576576

577+
#if swift(>=3.2)
578+
extension UnsignedInteger where Self: FixedWidthInteger {
579+
fileprivate var _resigned: Self {
580+
let bits = Self(extendingOrTruncating: MemoryLayout<Self>.size * 8 - 1)
581+
return self ^ (1 &<< bits)
582+
}
583+
}
584+
#endif
585+
577586
extension UInt: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, RandomThroughValue, RandomInRange, RandomInClosedRange, RandomWithMaxWidth, RandomWithExactWidth {
578587

579588
/// Generates a random value of `Self` using `randomGenerator`.
@@ -585,6 +594,7 @@ extension UInt: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, Rando
585594
}
586595
}
587596

597+
#if !swift(>=3.2)
588598
fileprivate var _resigned: UInt {
589599
let bits: UInt
590600
#if arch(i386) || arch(arm)
@@ -594,12 +604,9 @@ extension UInt: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, Rando
594604
#else
595605
bits = UInt(bitPattern: MemoryLayout<UInt>.size * 8 - 1)
596606
#endif
597-
#if swift(>=3.2)
598-
return self ^ (1 &<< bits)
599-
#else
600-
return self ^ (1 << bits)
601-
#endif
607+
return self ^ (1 << bits)
602608
}
609+
#endif
603610

604611
}
605612

@@ -610,13 +617,11 @@ extension UInt64: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, Ran
610617
return randomGenerator.random64()
611618
}
612619

620+
#if !swift(>=3.2)
613621
fileprivate var _resigned: UInt64 {
614-
#if swift(>=3.2)
615-
return self ^ (1 &<< 63)
616-
#else
617-
return self ^ (1 << 63)
618-
#endif
622+
return self ^ (1 << 63)
619623
}
624+
#endif
620625

621626
}
622627

@@ -627,13 +632,11 @@ extension UInt32: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, Ran
627632
return randomGenerator.random32()
628633
}
629634

635+
#if !swift(>=3.2)
630636
fileprivate var _resigned: UInt32 {
631-
#if swift(>=3.2)
632-
return self ^ (1 &<< 31)
633-
#else
634-
return self ^ (1 << 31)
635-
#endif
637+
return self ^ (1 << 31)
636638
}
639+
#endif
637640

638641
}
639642

@@ -644,13 +647,11 @@ extension UInt16: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, Ran
644647
return randomGenerator.random16()
645648
}
646649

650+
#if !swift(>=3.2)
647651
fileprivate var _resigned: UInt16 {
648-
#if swift(>=3.2)
649-
return self ^ (1 &<< 15)
650-
#else
651-
return self ^ (1 << 15)
652-
#endif
652+
return self ^ (1 << 15)
653653
}
654+
#endif
654655

655656
}
656657

@@ -661,12 +662,10 @@ extension UInt8: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, Rand
661662
return randomGenerator.random8()
662663
}
663664

665+
#if !swift(>=3.2)
664666
fileprivate var _resigned: UInt8 {
665-
#if swift(>=3.2)
666-
return self ^ (1 &<< 7)
667-
#else
668-
return self ^ (1 << 7)
669-
#endif
667+
return self ^ (1 << 7)
670668
}
669+
#endif
671670

672671
}

0 commit comments

Comments
 (0)