mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 19:28:57 +01:00
internal/atlas: optimize adjustDestinationPixel
``` go test -bench=BenchmarkAdjustPixel -run=^$ -count=5 ./internal/atlas/ ``` ``` name old time/op new time/op delta AdjustPixel-8 2.59ns ± 1% 2.12ns ± 1% -18.16% (p=0.008 n=5+5) ```
This commit is contained in:
parent
9319266c01
commit
31945563d8
@ -17,7 +17,6 @@ package atlas
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"math"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -805,7 +804,11 @@ func adjustDestinationPixel(x float32) float32 {
|
|||||||
// float32(math.Floor((float64(x)+1.0/6.0)*3) / 3)
|
// float32(math.Floor((float64(x)+1.0/6.0)*3) / 3)
|
||||||
//
|
//
|
||||||
// The actual implementation is more optimized than the above implementation.
|
// The actual implementation is more optimized than the above implementation.
|
||||||
ix := float32(math.Floor(float64(x)))
|
var ix float32
|
||||||
|
ix = float32(int(x))
|
||||||
|
if x < 0 && x != ix {
|
||||||
|
ix -= 1
|
||||||
|
}
|
||||||
frac := x - ix
|
frac := x - ix
|
||||||
switch {
|
switch {
|
||||||
case frac < 3.0/16.0:
|
case frac < 3.0/16.0:
|
||||||
|
@ -737,6 +737,40 @@ func TestImageWritePixelsModify(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAdjustPixel(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
X float32
|
||||||
|
Y float32
|
||||||
|
Delta float32
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
X: -0.1,
|
||||||
|
Y: 0.9,
|
||||||
|
Delta: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
X: -1,
|
||||||
|
Y: 0,
|
||||||
|
Delta: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
X: -1.9,
|
||||||
|
Y: 1.1,
|
||||||
|
Delta: 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
X: -2,
|
||||||
|
Y: 1,
|
||||||
|
Delta: 3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range tests {
|
||||||
|
if rx, ry := atlas.AdjustDestinationPixelForTesting(tc.X)+tc.Delta, atlas.AdjustDestinationPixelForTesting(tc.Y); rx != ry {
|
||||||
|
t.Errorf("adjustDestinationPixel(%f) + 1 must equal to adjustDestinationPixel(%f) but not (%f vs %f)", tc.X, tc.Y, rx, ry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkAdjustPixel(b *testing.B) {
|
func BenchmarkAdjustPixel(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
atlas.AdjustDestinationPixelForTesting(float32(i) / 17)
|
atlas.AdjustDestinationPixelForTesting(float32(i) / 17)
|
||||||
|
Loading…
Reference in New Issue
Block a user