12
12
// RUN: rm -rf %t && mkdir -p %t && %gyb -DWORD_BITS=%target-ptrsize %s -o %t/out.swift
13
13
// RUN: %line-directive %t/out.swift -- %target-build-swift -parse-stdlib %t/out.swift -o %t/a.out -Onone
14
14
// RUN: %line-directive %t/out.swift -- %target-run %t/a.out
15
- // --stdlib-unittest-filter DoubleWidth/
15
+ // --stdlib-unittest-filter minBitsRequired
16
16
17
17
// REQUIRES: executable_test
18
18
@@ -310,10 +310,10 @@ public protocol Integer : ${IntegerBase} {
310
310
/// Will be constant for fixed-width integer types.
311
311
var bitWidth : Word { get }
312
312
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 }
317
317
318
318
% for x in binaryArithmetic [ 'Integer'] :
319
319
// defaulted using an in-place counterpart, but can be used as an
@@ -707,7 +707,7 @@ extension UnsignedInteger where Self : FixedWidthInteger {
707
707
public init< T : Integer > ( _ source: T ) {
708
708
_assertCond (
709
709
source >= 0 , " negative value \( source) not representable by \( Self . self) " )
710
- let requiredBits = source. signBitIndex + 1
710
+ let requiredBits = source. minBitsRequiredForSignedRepresentation - 1
711
711
_assertCond (
712
712
requiredBits <= Self . bitWidth,
713
713
" \( Self . self) cannot store all \( requiredBits) bits "
@@ -719,7 +719,7 @@ extension UnsignedInteger where Self : FixedWidthInteger {
719
719
public init?< T : Integer> ( exactly source: T) {
720
720
_assertCond (
721
721
source >= 0 , " negative value \( source) not representable by \( Self . self) " )
722
- let requiredBits = source. signBitIndex + 1
722
+ let requiredBits = source. minBitsRequiredForSignedRepresentation - 1
723
723
if requiredBits > Self . bitWidth {
724
724
return nil
725
725
}
@@ -759,7 +759,7 @@ extension SignedInteger {
759
759
extension SignedInteger where Self : FixedWidthInteger {
760
760
@_transparent
761
761
public init < T : Integer > ( _ source: T ) {
762
- let requiredBits = source. signBitIndex + ( source >= 0 ? 2 : 1 )
762
+ let requiredBits = source. minBitsRequiredForSignedRepresentation
763
763
_assertCond (
764
764
requiredBits <= Self . bitWidth,
765
765
" \( Self . self) cannot store all \( requiredBits) bits "
@@ -769,7 +769,7 @@ extension SignedInteger where Self : FixedWidthInteger {
769
769
770
770
@_transparent
771
771
public init ? < T : Integer > ( exactly source: T ) {
772
- let requiredBits = source. signBitIndex + ( source >= 0 ? 2 : 1 )
772
+ let requiredBits = source. minBitsRequiredForSignedRepresentation
773
773
if requiredBits > Self . bitWidth {
774
774
return nil
775
775
}
@@ -887,12 +887,12 @@ public struct ${Self}
887
887
public var bitWidth: Word { return ${ bits} }
888
888
889
889
@_transparent
890
- public var signBitIndex : Word {
890
+ public var minBitsRequiredForSignedRepresentation : Word {
891
891
% if signed:
892
892
let x = self < 0 ? ~ self : self
893
- return ${ Self} . bitWidth - 1 - x . countLeadingZeros( )
893
+ return ( x == 0 ) ? 1 : ( ${ Self} . bitWidth - x. countLeadingZeros ( ) + 1 )
894
894
% else:
895
- return ${ Self} . bitWidth - 1 - self . countLeadingZeros ( )
895
+ return ( self == 0 ) ? 1 : ( ${ Self} . bitWidth - self . countLeadingZeros ( ) + 1 )
896
896
% end
897
897
}
898
898
@@ -1047,7 +1047,8 @@ public struct DoubleWidth<
1047
1047
return 2 * T. bitWidth
1048
1048
}
1049
1049
1050
- public var signBitIndex : Word {
1050
+ public var minBitsRequiredForSignedRepresentation : Word {
1051
+ // FIXME: WRONG !!!!!!!!!!!!!!!!!!!!!!
1051
1052
return ( self == DoubleWidth < T > ( ) ) ? - 1 : ( bitWidth - 1 )
1052
1053
}
1053
1054
@@ -1244,8 +1245,8 @@ public struct ${Self}
1244
1245
1245
1246
public var bitWidth: Word { return 128 }
1246
1247
1247
- public var signBitIndex : Word {
1248
- return _storage. signBitIndex
1248
+ public var minBitsRequiredForSignedRepresentation : Word {
1249
+ return _storage. minBitsRequiredForSignedRepresentation
1249
1250
}
1250
1251
1251
1252
public func countLeadingZeros( ) - > Word {
@@ -1485,12 +1486,12 @@ tests.test("CountLeadingZeros") {
1485
1486
expectEqual ( 0 , Int8 . min. countLeadingZeros ( ) )
1486
1487
}
1487
1488
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 )
1494
1495
}
1495
1496
1496
1497
tests. test ( " Conversion8to16 " ) {
@@ -1864,9 +1865,9 @@ tests.test("DoubleWidth/${SType}/subtracting beyond min") {
1864
1865
% end
1865
1866
% end
1866
1867
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 )
1870
1871
}
1871
1872
1872
1873
tests. test ( " DoubleWidth/UInt64/nthWord " ) {
0 commit comments