// run // Copyright 2013 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. // Test division of variables. Generate many test cases, // compute correct answer using shift and subtract, // and then compare against results from division and // modulus operators. // // Primarily useful for testing software div/mod. package main const long = false func main() { if long { // About 3e9 test cases (calls to checkdiv3). // Too long for everyday testing. gen2(3, 64, 2, 64, checkdiv1) println(ntest) } else { // About 4e6 test cases (calls to checkdiv3). // Runs for 8 seconds on ARM chromebook, much faster elsewhere. gen2(2, 64, 1, 64, checkdiv1) } } // generate all uint64 values x where x has at most n bits set in the low w // and call f(x) for each. func gen1(n, w int, f func(uint64)) { gen(0, 0, n, w-1, f) } func gen(val uint64, nbits, maxbits, pos int, f func(uint64)) { if pos < 0 { f(val) return } gen(val, nbits, maxbits, pos-1, f) if nbits < maxbits { gen(val|1< y && y+y <= x { sh++ y <<= 1 } for ; sh >= 0; sh-- { q <<= 1 if x >= y { x -= y q |= 1 } y >>= 1 } return q, x } // signed divide and mod: do unsigned and adjust signs. func idiv(x, y int64) (q, r int64) { // special case for minint / -1 = minint if x-1 > x && y == -1 { return x, 0 } ux := uint64(x) uy := uint64(y) if x < 0 { ux = -ux } if y < 0 { uy = -uy } uq, ur := udiv(ux, uy) q = int64(uq) r = int64(ur) if x < 0 { r = -r } if (x < 0) != (y < 0) { q = -q } return q, r }