Dont rely on compiler smartness to replace / and % by shifting and masking. Make use of constants more consistence.

This commit is contained in:
Sascha L. Teichmann 2014-08-18 19:58:10 +02:00
parent ff14b83b1e
commit 066675896d

View File

@ -11,8 +11,8 @@ import (
const ( const (
numBitsPerComponent = 12 numBitsPerComponent = 12
maxPositive = 2048 modulo = 1 << numBitsPerComponent
modulo = 4096 maxPositive = modulo / 2
) )
type ( type (
@ -92,7 +92,9 @@ func InterleavedToXYZ(c int64) (x, y, z int16) {
} }
func XYZToPlain(x, y, z int16) int64 { func XYZToPlain(x, y, z int16) int64 {
return int64(z)<<24 + int64(y)<<12 + int64(x) return int64(z)<<(2*numBitsPerComponent) +
int64(y)<<numBitsPerComponent +
int64(x)
} }
func unsignedToSigned(i int16) int16 { func unsignedToSigned(i int16) int16 {
@ -104,18 +106,19 @@ func unsignedToSigned(i int16) int16 {
// To match C++ code. // To match C++ code.
func pythonModulo(i int16) int16 { func pythonModulo(i int16) int16 {
const mask = modulo - 1
if i >= 0 { if i >= 0 {
return i % modulo return i & mask
} }
return modulo - (-i)%modulo return modulo - -i&mask
} }
// Only to match the C++ code. // Only to match the C++ code.
func PlainToXYZ(i int64) (x, y, z int16) { func PlainToXYZ(i int64) (x, y, z int16) {
x = unsignedToSigned(pythonModulo(int16(i))) x = unsignedToSigned(pythonModulo(int16(i)))
i = (i - int64(x)) / modulo i = (i - int64(x)) >> numBitsPerComponent
y = unsignedToSigned(pythonModulo(int16(i))) y = unsignedToSigned(pythonModulo(int16(i)))
i = (i - int64(y)) / modulo i = (i - int64(y)) >> numBitsPerComponent
z = unsignedToSigned(pythonModulo(int16(i))) z = unsignedToSigned(pythonModulo(int16(i)))
return return
} }