go/src/cmd/compile/internal/ssa/rewrite_test.go

106 lines
2.2 KiB
Go
Raw Normal View History

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssa
import "testing"
// TestNlzNto tests nlz/nto of the same number which is used in some of
// the rewrite rules.
func TestNlzNto(t *testing.T) {
// construct the bit pattern 000...111, nlz(x) + nto(0) = 64
var x int64
for i := int64(0); i < 64; i++ {
if got := nto(x); got != i {
t.Errorf("expected nto(0x%X) = %d, got %d", x, i, got)
}
if got := nlz(x); got != 64-i {
t.Errorf("expected nlz(0x%X) = %d, got %d", x, 64-i, got)
}
x = (x << 1) | 1
}
x = 0
// construct the bit pattern 000...111, with bit 33 set as well.
for i := int64(0); i < 64; i++ {
tx := x | (1 << 32)
// nto should be the the number of bits we've shifted on, with an extra bit
// at iter 32
ntoExp := i
if ntoExp == 32 {
ntoExp = 33
}
if got := nto(tx); got != ntoExp {
t.Errorf("expected nto(0x%X) = %d, got %d", tx, ntoExp, got)
}
// sinec bit 33 is set, nlz can be no greater than 31
nlzExp := 64 - i
if nlzExp > 31 {
nlzExp = 31
}
if got := nlz(tx); got != nlzExp {
t.Errorf("expected nlz(0x%X) = %d, got %d", tx, nlzExp, got)
}
x = (x << 1) | 1
}
}
func TestNlz(t *testing.T) {
var nlzTests = []struct {
v int64
exp int64
}{{0x00, 64},
{0x01, 63},
{0x0F, 60},
{0xFF, 56},
{0xffffFFFF, 32},
{-0x01, 0}}
for _, tc := range nlzTests {
if got := nlz(tc.v); got != tc.exp {
t.Errorf("expected nlz(0x%X) = %d, got %d", tc.v, tc.exp, got)
}
}
}
func TestNto(t *testing.T) {
var ntoTests = []struct {
v int64
exp int64
}{{0x00, 0},
{0x01, 1},
{0x0F, 4},
{0xFF, 8},
{0xffffFFFF, 32},
{-0x01, 64}}
for _, tc := range ntoTests {
if got := nto(tc.v); got != tc.exp {
t.Errorf("expected nto(0x%X) = %d, got %d", tc.v, tc.exp, got)
}
}
}
func TestLog2(t *testing.T) {
var log2Tests = []struct {
v int64
exp int64
}{{0, -1}, // nlz expects log2(0) == -1
{1, 0},
{2, 1},
{4, 2},
cmd/compile: improve tighten pass Move a value to the block which is the lowest common ancestor in the dominator tree of all of its uses. Make sure not to move a value into a loop. Makes the tighten pass on average (across go1 benchmarks) 40% slower. Still not a big contributor to overall compile time. Binary size is just a tad smaller. name old time/op new time/op delta BinaryTree17-12 2.77s ± 9% 2.76s ± 9% ~ (p=0.878 n=8+8) Fannkuch11-12 2.75s ± 1% 2.74s ± 1% ~ (p=0.232 n=8+7) FmtFprintfEmpty-12 48.9ns ± 9% 47.7ns ± 0% ~ (p=0.431 n=8+8) FmtFprintfString-12 143ns ± 8% 142ns ± 1% ~ (p=0.257 n=8+7) FmtFprintfInt-12 123ns ± 1% 122ns ± 1% -1.04% (p=0.026 n=7+8) FmtFprintfIntInt-12 195ns ± 7% 185ns ± 0% -5.32% (p=0.000 n=8+8) FmtFprintfPrefixedInt-12 194ns ± 4% 195ns ± 0% +0.81% (p=0.015 n=7+7) FmtFprintfFloat-12 267ns ± 0% 268ns ± 0% +0.37% (p=0.001 n=7+6) FmtManyArgs-12 800ns ± 0% 762ns ± 1% -4.78% (p=0.000 n=8+8) GobDecode-12 7.67ms ± 2% 7.60ms ± 2% ~ (p=0.234 n=8+8) GobEncode-12 6.55ms ± 0% 6.57ms ± 1% ~ (p=0.336 n=7+8) Gzip-12 237ms ± 0% 238ms ± 0% +0.40% (p=0.017 n=7+7) Gunzip-12 40.8ms ± 0% 40.2ms ± 0% -1.52% (p=0.000 n=7+8) HTTPClientServer-12 208µs ± 3% 209µs ± 3% ~ (p=0.955 n=8+7) JSONEncode-12 16.2ms ± 1% 17.2ms ±11% +5.80% (p=0.001 n=7+8) JSONDecode-12 57.3ms ±12% 55.5ms ± 3% ~ (p=0.867 n=8+7) Mandelbrot200-12 4.68ms ± 6% 4.46ms ± 1% ~ (p=0.442 n=8+8) GoParse-12 4.27ms ±44% 3.42ms ± 1% -19.95% (p=0.005 n=8+8) RegexpMatchEasy0_32-12 75.1ns ± 0% 75.8ns ± 1% +0.99% (p=0.002 n=7+7) RegexpMatchEasy0_1K-12 963ns ± 0% 1021ns ± 6% +5.98% (p=0.001 n=7+7) RegexpMatchEasy1_32-12 72.4ns ±11% 70.8ns ± 1% ~ (p=0.368 n=8+8) RegexpMatchEasy1_1K-12 394ns ± 1% 399ns ± 0% +1.23% (p=0.000 n=8+7) RegexpMatchMedium_32-12 114ns ± 0% 115ns ± 1% +0.63% (p=0.021 n=7+7) RegexpMatchMedium_1K-12 35.9µs ± 0% 37.6µs ± 1% +4.72% (p=0.000 n=7+8) RegexpMatchHard_32-12 1.93µs ± 2% 1.91µs ± 0% -0.91% (p=0.001 n=7+7) RegexpMatchHard_1K-12 60.2µs ± 3% 61.2µs ±10% ~ (p=0.442 n=8+8) Revcomp-12 404ms ± 1% 406ms ± 1% ~ (p=0.054 n=8+7) Template-12 64.6ms ± 1% 63.5ms ± 1% -1.66% (p=0.000 n=8+8) TimeParse-12 347ns ± 8% 309ns ± 0% -11.13% (p=0.000 n=8+7) TimeFormat-12 343ns ± 4% 331ns ± 0% -3.34% (p=0.000 n=8+7) Change-Id: Id6da1239ddd4d0cb074ff29cffb06302d1c6d08f Reviewed-on: https://go-review.googlesource.com/28712 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
2016-09-07 14:04:31 -07:00
{7, 2},
{8, 3},
{9, 3},
{1024, 10}}
for _, tc := range log2Tests {
if got := log2(tc.v); got != tc.exp {
t.Errorf("expected log2(%d) = %d, got %d", tc.v, tc.exp, got)
}
}
}