Files
swift-mirror/validation-test/stdlib/FixedPoint.swift.gyb
Dmitri Hrybenko bbf79427ac stdlib: remove bitwise operations on Bool
Bitwise operations on Bool are redundant with other logic operations
that stdlib already provides.  The only reason to have them was to avoid
branching in the short-circuiting && and ||.

rdar://19340952

Surprisingly, replacing & and | in the standard library with && and ||
brought performance improvements and no significant performance
regressions:

RecursiveOwnedParameter 1.14
SelectionSort 1.19

Swift SVN r24674
2015-01-23 03:09:55 +00:00

367 lines
8.1 KiB
Plaintext

// RUN: rm -rf %t && mkdir -p %t && %S/../../utils/gyb %s -o %t/FixedPoint.swift
// RUN: %S/../../utils/line-directive %t/FixedPoint.swift -- %target-build-swift %t/FixedPoint.swift -o %t/a.out
// RUN: %target-run %t/a.out
import StdlibUnittest
var FixedPoint = TestSuite("FixedPoint")
%{
import gyb
test_bit_patterns = [
0x0000000000000000,
0x0000000000000001,
0xffffffffffffffff,
0x00ffffffffffffff,
0x0000ffffffffffff,
0x000000ffffffffff,
0x00000000ffffffff,
0x0000000000ffffff,
0x000000000000ffff,
0x00000000000000ff,
0xfe123456789abcde,
0x00fe123456789abc,
0x0000fe123456789a,
0x000000fe12345678,
0x00000000fe123456,
0x0000000000fe1234,
0x000000000000fe12,
0x00000000000000fe,
0x7f123456789abcde,
0x007f123456789abc,
0x00007f123456789a,
0x0000007f12345678,
0x000000007f123456,
0x00000000007f1234,
0x0000000000007f12,
0x000000000000007f,
]
def prepare_bit_pattern(bit_pattern, dst_bits, dst_signed):
mask = ((1 << dst_bits) - 1)
dst = bit_pattern & mask
if not dst_signed:
return dst
if dst <= ((1 << (dst_bits - 1)) - 1):
return dst
return dst - mask - 1
def get_fixed_point_hash(bit_pattern, src_bits, word_bits):
if src_bits <= word_bits:
bit_pattern = prepare_bit_pattern(bit_pattern, src_bits, True)
return prepare_bit_pattern(bit_pattern, word_bits, True)
if src_bits == word_bits * 2:
return prepare_bit_pattern(
bit_pattern ^ (bit_pattern >> 32), word_bits, True)
}%
//===----------------------------------------------------------------------===//
// 'Int(truncatingBitPattern:)' initializer
//===----------------------------------------------------------------------===//
%{
truncating_bit_pattern_test_template = gyb.parseTemplate("truncating_bit_pattern",
"""
% from SwiftIntTypes import *
% for dst_ty in all_integer_types(word_bits):
% Dst = dst_ty.stdlib_name
FixedPoint.test("${Dst}(truncatingBitPattern:)") {
% for src_ty in all_integer_types(word_bits):
% Src = src_ty.stdlib_name
% if should_define_truncating_bit_pattern_init(src_ty=src_ty, dst_ty=dst_ty):
%
% for bit_pattern in test_bit_patterns:
if true {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
% input = prepare_bit_pattern(input, src_ty.bits, False)
let output = get${Dst}(${Dst}(truncatingBitPattern: input))
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, output)
}
% end
// This comment prevents gyb from miscompiling this file.
// <rdar://problem/17548877> gyb miscompiles a certain for loop
% end
// This comment prevents gyb from miscompiling this file.
// <rdar://problem/17548877> gyb miscompiles a certain for loop
% end
}
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.executeTemplate(
truncating_bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64)
${gyb.executeTemplate(
truncating_bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
//===----------------------------------------------------------------------===//
// 'Int(bitPattern:)' initializer
//===----------------------------------------------------------------------===//
%{
bit_pattern_test_template = gyb.parseTemplate("bit_pattern",
"""
% from SwiftIntTypes import *
% for dst_ty in all_integer_types(word_bits):
% Dst = dst_ty.stdlib_name
% # Source is the same as destination, but of different signedness.
% src_ty = dst_ty.get_opposite_signedness()
% Src = src_ty.stdlib_name
FixedPoint.test("${Dst}(bitPattern: ${Src})") {
% for bit_pattern in test_bit_patterns:
if true {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
% input = prepare_bit_pattern(input, src_ty.bits, False)
let output = get${Dst}(${Dst}(bitPattern: input))
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, output)
}
if true {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
// Pass a literal directly.
let literalOutput = get${Dst}(${Dst}(bitPattern: ${input}))
let output = get${Dst}(${Dst}(bitPattern: input))
% input = prepare_bit_pattern(input, src_ty.bits, False)
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, literalOutput)
expectEqual(expected, output)
}
% end
}
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.executeTemplate(
bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64)
${gyb.executeTemplate(
bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
//===----------------------------------------------------------------------===//
// 'Int.hashValue' property
//===----------------------------------------------------------------------===//
%{
hash_value_test_template = gyb.parseTemplate("hash_value",
"""
% from SwiftIntTypes import *
% for self_ty in all_integer_types(word_bits):
% Self = self_ty.stdlib_name
FixedPoint.test("${Self}.hashValue") {
% for bit_pattern in test_bit_patterns:
if true {
% input = prepare_bit_pattern(bit_pattern, self_ty.bits, self_ty.is_signed)
let input = get${Self}(${input})
let output = getInt(input.hashValue)
let expected = getInt(${get_fixed_point_hash(input, self_ty.bits, word_bits)})
expectEqual(expected, output)
}
% end
}
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.executeTemplate(
hash_value_test_template,
prepare_bit_pattern=prepare_bit_pattern,
get_fixed_point_hash=get_fixed_point_hash,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64)
${gyb.executeTemplate(
hash_value_test_template,
prepare_bit_pattern=prepare_bit_pattern,
get_fixed_point_hash=get_fixed_point_hash,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
var BoolTestSuite = TestSuite("Bool")
BoolTestSuite.test("literals") {
if true {
var v = false
expectType(Bool.self, &v)
}
if true {
var v = true
expectType(Bool.self, &v)
}
}
BoolTestSuite.test("init()") {
let v = Bool()
expectFalse(v)
}
struct Booleanish : BooleanType {
let boolValue: Bool
}
BoolTestSuite.test("init<T : BooleanType>(_:)") {
if true {
let v = Bool(Booleanish(boolValue: false))
expectFalse(v)
}
if true {
let v = Bool(Booleanish(boolValue: true))
expectTrue(v)
}
}
BoolTestSuite.test("BooleanType") {
if true {
var v: Bool = false
expectIsBooleanType(&v)
expectFalse(v.boolValue)
}
if true {
var v: Bool = true
expectTrue(v.boolValue)
}
}
BoolTestSuite.test("Printable") {
if true {
let v: Bool = false
expectEqual("false", toString(v))
}
if true {
let v: Bool = true
expectEqual("true", toString(v))
}
}
BoolTestSuite.test("Equatable,Hashable") {
checkHashable(true, false, false)
checkHashable(false, false, true)
checkHashable(true, true, true)
expectNotEqual(false.hashValue, true.hashValue)
}
BoolTestSuite.test("!") {
if true {
let v: Bool = false
var r = !v
expectType(Bool.self, &r)
expectTrue(r)
}
if true {
let v: Bool = true
var r = !v
expectType(Bool.self, &r)
expectFalse(r)
}
}
BoolTestSuite.test("!") {
let v: Bool = false
if true {
var r = (v == v)
expectType(Bool.self, &r)
}
if true {
var r = (v != v)
expectType(Bool.self, &r)
}
if true {
var r = !v
expectType(Bool.self, &r)
}
}
runAllTests()