]> Cypherpunks repositories - gostls13.git/commitdiff
image/jpeg: make TestDCT faster.
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Thu, 18 Oct 2012 19:28:04 +0000 (21:28 +0200)
committerRémy Oudompheng <oudomphe@phare.normalesup.org>
Thu, 18 Oct 2012 19:28:04 +0000 (21:28 +0200)
The value of cosines are cached in a global array
instead of being recomputed each time.
The test was terribly slow on arm.

R=golang-dev, dave, nigeltao
CC=golang-dev
https://golang.org/cl/6733046

src/pkg/image/jpeg/dct_test.go

index 770e274bac0123a8c7fbf272918154386ea4c2d0..52d0c10dee25115ac90bb1d202d4474b100f62d3 100644 (file)
@@ -112,6 +112,14 @@ func alpha(i int) float64 {
        return math.Sqrt2
 }
 
+var cosines [32]float64 // cosines[k] = cos(π/2 * k/8)
+
+func init() {
+       for k := range cosines {
+               cosines[k] = math.Cos(math.Pi * float64(k) / 16)
+       }
+}
+
 // slowFDCT performs the 8*8 2-dimensional forward discrete cosine transform:
 //
 //     dst[u,v] = (1/8) * Σ_x Σ_y alpha(u) * alpha(v) * src[x,y] *
@@ -129,8 +137,8 @@ func slowFDCT(b *block) {
                        for y := 0; y < 8; y++ {
                                for x := 0; x < 8; x++ {
                                        sum += alpha(u) * alpha(v) * float64(b[8*y+x]) *
-                                               math.Cos(math.Pi*float64((2*x+1)*u)/16) *
-                                               math.Cos(math.Pi*float64((2*y+1)*v)/16)
+                                               cosines[((2*x+1)*u)%32] *
+                                               cosines[((2*y+1)*v)%32]
                                }
                        }
                        dst[8*v+u] = sum / 8
@@ -159,8 +167,8 @@ func slowIDCT(b *block) {
                        for v := 0; v < 8; v++ {
                                for u := 0; u < 8; u++ {
                                        sum += alpha(u) * alpha(v) * float64(b[8*v+u]) *
-                                               math.Cos(math.Pi*float64((2*x+1)*u)/16) *
-                                               math.Cos(math.Pi*float64((2*y+1)*v)/16)
+                                               cosines[((2*x+1)*u)%32] *
+                                               cosines[((2*y+1)*v)%32]
                                }
                        }
                        dst[8*y+x] = sum / 8