Source file src/cmd/compile/internal/ssarewrite/rewritedivmod/divmod_helpers.go

     1  // Copyright 2015 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 rewritedivmod
     6  
     7  import "cmd/compile/internal/ssa"
     8  
     9  // logX returns logarithm of n base 2.
    10  // n must be a positive power of 2 (isPowerOfTwoX returns true).
    11  func log8(n int8) int64 { return ssa.Log8u(uint8(n)) }
    12  
    13  func smagic16(c int16) ssa.SmagicData { return ssa.Smagic(16, int64(c)) }
    14  
    15  func smagic32(c int32) ssa.SmagicData { return ssa.Smagic(32, int64(c)) }
    16  
    17  func smagic64(c int64) ssa.SmagicData { return ssa.Smagic(64, c) }
    18  
    19  func smagic8(c int8) ssa.SmagicData { return ssa.Smagic(8, int64(c)) }
    20  
    21  func smagicOK16(c int16) bool { return ssa.SmagicOK(16, int64(c)) }
    22  
    23  func smagicOK32(c int32) bool { return ssa.SmagicOK(32, int64(c)) }
    24  
    25  func smagicOK64(c int64) bool { return ssa.SmagicOK(64, c) }
    26  
    27  // smagicOKn reports whether we should strength reduce a signed n-bit divide by c.
    28  func smagicOK8(c int8) bool { return ssa.SmagicOK(8, int64(c)) }
    29  
    30  func umagic16(c int16) ssa.UmagicData { return ssa.Umagic(16, int64(c)) }
    31  
    32  func umagic32(c int32) ssa.UmagicData { return ssa.Umagic(32, int64(c)) }
    33  
    34  // umagic32PreShifted returns the pre-shifted 64-bit magic constant for unsigned 32-bit
    35  // division by c on 64-bit targets that have a native 64x64->128-bit multiply instruction
    36  // (amd64 MULQ, arm64 UMULH, riscv64 MULHU, etc.), enabling:
    37  //
    38  //	x / c = Hmul64u(ZeroExt32to64(x), umagic32PreShifted(c))
    39  //
    40  // Given umagic32(c) returning m and s, the constant is (2^32 + m) << (32 - s).
    41  // Valid when umagicOK32(c) is true. Result always fits in uint64.
    42  func umagic32PreShifted(c int32) uint64 {
    43  	magic := umagic32(c)
    44  	return (1<<32 + magic.M) << uint(32-magic.S)
    45  }
    46  
    47  func umagic64(c int64) ssa.UmagicData { return ssa.Umagic(64, c) }
    48  
    49  func umagic8(c int8) ssa.UmagicData { return ssa.Umagic(8, int64(c)) }
    50  
    51  func umagicOK16(c int16) bool { return c&(c-1) != 0 }
    52  
    53  func umagicOK32(c int32) bool { return c&(c-1) != 0 }
    54  
    55  func umagicOK64(c int64) bool { return c&(c-1) != 0 }
    56  
    57  // umagicOKn reports whether we should strength reduce an unsigned n-bit divide by c.
    58  // We can strength reduce when c != 0 and c is not a power of two.
    59  func umagicOK8(c int8) bool { return c&(c-1) != 0 }
    60  

View as plain text