mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 03:00:26 +01:00
Dont rely on compiler smartness to replace / and % by shifting and masking. Make use of constants more consistence.
This commit is contained in:
parent
ff14b83b1e
commit
066675896d
|
@ -11,8 +11,8 @@ import (
|
|||
|
||||
const (
|
||||
numBitsPerComponent = 12
|
||||
maxPositive = 2048
|
||||
modulo = 4096
|
||||
modulo = 1 << numBitsPerComponent
|
||||
maxPositive = modulo / 2
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -92,7 +92,9 @@ func InterleavedToXYZ(c int64) (x, y, z int16) {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
@ -104,18 +106,19 @@ func unsignedToSigned(i int16) int16 {
|
|||
|
||||
// To match C++ code.
|
||||
func pythonModulo(i int16) int16 {
|
||||
const mask = modulo - 1
|
||||
if i >= 0 {
|
||||
return i % modulo
|
||||
return i & mask
|
||||
}
|
||||
return modulo - (-i)%modulo
|
||||
return modulo - -i&mask
|
||||
}
|
||||
|
||||
// Only to match the C++ code.
|
||||
func PlainToXYZ(i int64) (x, y, z int16) {
|
||||
x = unsignedToSigned(pythonModulo(int16(i)))
|
||||
i = (i - int64(x)) / modulo
|
||||
i = (i - int64(x)) >> numBitsPerComponent
|
||||
y = unsignedToSigned(pythonModulo(int16(i)))
|
||||
i = (i - int64(y)) / modulo
|
||||
i = (i - int64(y)) >> numBitsPerComponent
|
||||
z = unsignedToSigned(pythonModulo(int16(i)))
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user