Source file src/cmd/compile/internal/ssarewrite/rewritegeneric/generic_helpers_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rewritegeneric
     6  
     7  import "testing"
     8  
     9  func TestModularMultiplicativeInverse(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	// We've got 63 bits of phase space for the Multiplier
    13  	// Needless to say this is too much to bruteforce here.
    14  	// I've randomly picked a range of 1<<24 because it runs in 0.03s on my machine which isn't too slow.
    15  	// We test both sides of the wrapping point (0 and math.MaxUint64) since we need to test something and it's a usual place to have bugs.
    16  	const halfRange = 1 << 23
    17  	for i := -int64(halfRange) - 1; i < halfRange; i += 2 { // odd only, a bit after to a bit before the wrapping point
    18  		mmi := modularMultiplicativeInverse(uint64(i))
    19  
    20  		if uint64(i)*mmi != 1 {
    21  			t.Errorf("%d * modularMultiplicativeInverse(%d) != 1; modularMultiplicativeInverse(%d) == %d", i, i, i, mmi)
    22  		}
    23  	}
    24  }
    25  

View as plain text