Skip to content

Commit 0183dce

Browse files
author
Max Moiseev
committed
[integers] introducing minBitsRequired
1 parent 0b16f2d commit 0183dce

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

test/Prototypes/Integers.swift.gyb

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// RUN: rm -rf %t && mkdir -p %t && %gyb -DWORD_BITS=%target-ptrsize %s -o %t/out.swift
1313
// RUN: %line-directive %t/out.swift -- %target-build-swift -parse-stdlib %t/out.swift -o %t/a.out -Onone
1414
// RUN: %line-directive %t/out.swift -- %target-run %t/a.out
15-
// --stdlib-unittest-filter DoubleWidth/
15+
// --stdlib-unittest-filter minBitsRequired
1616

1717
// REQUIRES: executable_test
1818

@@ -310,10 +310,10 @@ public protocol Integer : ${IntegerBase} {
310310
/// Will be constant for fixed-width integer types.
311311
var bitWidth : Word { get }
312312

313-
/// If `self` is negative, returns the index of the least significant bit of
314-
/// our representation such that all more-significant bits are 1.
315-
/// Has the value -1 if `self` is 0.
316-
var signBitIndex: Word { get }
313+
/// Returns the number of bits required to represent the value of `self` in a
314+
/// signed type using two's completment representation.
315+
/// The minimum value for this property should naturally be 1.
316+
var minBitsRequiredForSignedRepresentation: Word { get }
317317

318318
% for x in binaryArithmetic['Integer']:
319319
// defaulted using an in-place counterpart, but can be used as an
@@ -707,7 +707,7 @@ extension UnsignedInteger where Self : FixedWidthInteger {
707707
public init<T : Integer>(_ source: T) {
708708
_assertCond(
709709
source >= 0, "negative value \(source) not representable by \(Self.self)")
710-
let requiredBits = source.signBitIndex + 1
710+
let requiredBits = source.minBitsRequiredForSignedRepresentation - 1
711711
_assertCond(
712712
requiredBits <= Self.bitWidth,
713713
"\(Self.self) cannot store all \(requiredBits) bits "
@@ -719,7 +719,7 @@ extension UnsignedInteger where Self : FixedWidthInteger {
719719
public init?<T : Integer>(exactly source: T) {
720720
_assertCond(
721721
source >= 0, "negative value \(source) not representable by \(Self.self)")
722-
let requiredBits = source.signBitIndex + 1
722+
let requiredBits = source.minBitsRequiredForSignedRepresentation - 1
723723
if requiredBits > Self.bitWidth {
724724
return nil
725725
}
@@ -759,7 +759,7 @@ extension SignedInteger {
759759
extension SignedInteger where Self : FixedWidthInteger {
760760
@_transparent
761761
public init<T : Integer>(_ source: T) {
762-
let requiredBits = source.signBitIndex + (source >= 0 ? 2 : 1)
762+
let requiredBits = source.minBitsRequiredForSignedRepresentation
763763
_assertCond(
764764
requiredBits <= Self.bitWidth,
765765
"\(Self.self) cannot store all \(requiredBits) bits "
@@ -769,7 +769,7 @@ extension SignedInteger where Self : FixedWidthInteger {
769769

770770
@_transparent
771771
public init?<T : Integer>(exactly source: T) {
772-
let requiredBits = source.signBitIndex + (source >= 0 ? 2 : 1)
772+
let requiredBits = source.minBitsRequiredForSignedRepresentation
773773
if requiredBits > Self.bitWidth {
774774
return nil
775775
}
@@ -887,12 +887,12 @@ public struct ${Self}
887887
public var bitWidth: Word { return ${bits} }
888888

889889
@_transparent
890-
public var signBitIndex: Word {
890+
public var minBitsRequiredForSignedRepresentation: Word {
891891
% if signed:
892892
let x = self < 0 ? ~self : self
893-
return ${Self}.bitWidth - 1 - x.countLeadingZeros()
893+
return (x == 0) ? 1 : (${Self}.bitWidth - x.countLeadingZeros() + 1)
894894
% else:
895-
return ${Self}.bitWidth - 1 - self.countLeadingZeros()
895+
return (self == 0) ? 1 : (${Self}.bitWidth - self.countLeadingZeros() + 1)
896896
% end
897897
}
898898

@@ -1047,7 +1047,8 @@ public struct DoubleWidth<
10471047
return 2 * T.bitWidth
10481048
}
10491049

1050-
public var signBitIndex: Word {
1050+
public var minBitsRequiredForSignedRepresentation: Word {
1051+
// FIXME: WRONG !!!!!!!!!!!!!!!!!!!!!!
10511052
return (self == DoubleWidth<T>()) ? -1 : (bitWidth - 1)
10521053
}
10531054

@@ -1244,8 +1245,8 @@ public struct ${Self}
12441245

12451246
public var bitWidth: Word { return 128 }
12461247

1247-
public var signBitIndex: Word {
1248-
return _storage.signBitIndex
1248+
public var minBitsRequiredForSignedRepresentation: Word {
1249+
return _storage.minBitsRequiredForSignedRepresentation
12491250
}
12501251

12511252
public func countLeadingZeros() -> Word {
@@ -1485,12 +1486,12 @@ tests.test("CountLeadingZeros") {
14851486
expectEqual(0, Int8.min.countLeadingZeros())
14861487
}
14871488

1488-
tests.test("signBitIndex") {
1489-
expectEqual(7, UInt8.max.signBitIndex)
1490-
expectEqual(-1, UInt8.min.signBitIndex)
1491-
expectEqual(6, Int8.max.signBitIndex)
1492-
expectEqual(-1, (0 as Int8).signBitIndex)
1493-
expectEqual(6, Int8.min.signBitIndex)
1489+
tests.test("minBitsRequiredForSignedRepresentation") {
1490+
expectEqual(9, UInt8.max.minBitsRequiredForSignedRepresentation)
1491+
expectEqual(1, UInt8.min.minBitsRequiredForSignedRepresentation)
1492+
expectEqual(8, Int8.max.minBitsRequiredForSignedRepresentation)
1493+
expectEqual(1, (0 as Int8).minBitsRequiredForSignedRepresentation)
1494+
expectEqual(8, Int8.min.minBitsRequiredForSignedRepresentation)
14941495
}
14951496

14961497
tests.test("Conversion8to16") {
@@ -1864,9 +1865,9 @@ tests.test("DoubleWidth/${SType}/subtracting beyond min") {
18641865
% end
18651866
% end
18661867

1867-
tests.test("DoubleWidth/signBitIndex") {
1868-
expectEqual(15, DoubleWidth<UInt8>.max.signBitIndex)
1869-
expectEqual(-1, DoubleWidth<UInt8>.min.signBitIndex)
1868+
tests.test("DoubleWidth/minBitsRequiredForSignedRepresentation") {
1869+
expectEqual(15, DoubleWidth<UInt8>.max.minBitsRequiredForSignedRepresentation)
1870+
expectEqual(-1, DoubleWidth<UInt8>.min.minBitsRequiredForSignedRepresentation)
18701871
}
18711872

18721873
tests.test("DoubleWidth/UInt64/nthWord") {

0 commit comments

Comments
 (0)