]> Cypherpunks repositories - gostls13.git/commitdiff
test/codegen: add codegen tests for div
authorAlberto Donizetti <alb.donizetti@gmail.com>
Wed, 14 Mar 2018 10:47:34 +0000 (11:47 +0100)
committerAlberto Donizetti <alb.donizetti@gmail.com>
Wed, 14 Mar 2018 15:56:46 +0000 (15:56 +0000)
Change-Id: I6ce8981e85fd55ade6078b0946e54a9215d9deca
Reviewed-on: https://go-review.googlesource.com/100575
Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
test/codegen/arithmetic.go

index 1294cfffd970807bfe92c1fc9ad1e4571bcf17d5..20adc84beed9dd27b399b0b10847abe4dbe5be7f 100644 (file)
@@ -9,6 +9,10 @@ package codegen
 // This file contains codegen tests related to arithmetic
 // simplifications/optimizations.
 
+// -------------------- //
+//    Multiplication    //
+// -------------------- //
+
 func Pow2Muls(n1, n2 int) (int, int) {
        // amd64:"SHLQ\t[$]5",-"IMULQ"
        // 386:"SHLL\t[$]5",-"IMULL"
@@ -25,9 +29,7 @@ func Pow2Muls(n1, n2 int) (int, int) {
        return a, b
 }
 
-// ------------------ //
-//    MULs merging    //
-// ------------------ //
+// Multiplications merging tests
 
 func MergeMuls1(n int) int {
        // amd64:"IMUL3Q\t[$]46"
@@ -58,3 +60,61 @@ func MergeMuls5(a, n int) int {
        // 386:"ADDL\t[$]-19",-"IMULL\t[$]19"
        return a*n - 19*n // (a-19)n
 }
+
+// -------------- //
+//    Division    //
+// -------------- //
+
+func Pow2Divs(n1 uint, n2 int) (uint, int) {
+       // 386:"SHRL\t[$]5",-"DIVL"
+       // amd64:"SHRQ\t[$]5",-"DIVQ"
+       // arm:"SRL\t[$]5",-".*udiv"
+       // arm64:"LSR\t[$]5",-"UDIV"
+       a := n1 / 32 // unsigned
+
+       // amd64:"SARQ\t[$]6",-"IDIVQ"
+       // 386:"SARL\t[$]6",-"IDIVL"
+       // arm:"SRA\t[$]6",-".*udiv"
+       // arm64:"ASR\t[$]6",-"SDIV"
+       b := n2 / 64 // signed
+
+       return a, b
+}
+
+// Check that constant divisions get turned into MULs
+func ConstDivs(n1 uint, n2 int) (uint, int) {
+       // amd64:"MOVQ\t[$]-1085102592571150095","MULQ",-"DIVQ"
+       a := n1 / 17 // unsigned
+
+       // amd64:"MOVQ\t[$]-1085102592571150095","IMULQ",-"IDIVQ"
+       b := n2 / 17 // signed
+
+       return a, b
+}
+
+func Pow2Mods(n1 uint, n2 int) (uint, int) {
+       // 386:"ANDL\t[$]31",-"DIVL"
+       // amd64:"ANDQ\t[$]31",-"DIVQ"
+       // arm:"AND\t[$]31",-".*udiv"
+       // arm64:"AND\t[$]31",-"UDIV"
+       a := n1 % 32 // unsigned
+
+       // 386:-"IDIVL"
+       // amd64:-"IDIVQ"
+       // arm:-".*udiv"
+       // arm64:-"REM"
+       b := n2 % 64 // signed
+
+       return a, b
+}
+
+// Check that constant modulo divs get turned into MULs
+func ConstMods(n1 uint, n2 int) (uint, int) {
+       // amd64:"MOVQ\t[$]-1085102592571150095","MULQ",-"DIVQ"
+       a := n1 % 17 // unsigned
+
+       // amd64:"MOVQ\t[$]-1085102592571150095","IMULQ",-"IDIVQ"
+       b := n2 % 17 // signed
+
+       return a, b
+}