lib/lowleveljpeg: round cosines table to nearest

This commit is contained in:
Nigel Tao committed 2026-04-19 12:04:16 +10:00
1 parent f4f46bd395
commit 32b741db7f
3 files changed
+27 -27

No files matched your search

+10 -10
View File
@@ -262,30 +262,30 @@ func (b *BlockI16) IsValid() bool {
// These are "1 / √2" and "1 / 2" as 16.16 fixed point values.
const (
fixedPointInvSqrt2 = +0xB504
fixedPointInvSqrt2 = +0xB505
fixedPointHalf = +0x8000
)
// cosines is a cosine table as 16.16 fixed point values.
var cosines = [32]int32{
var cosines = [16]int32{
+0x10000, // cos(π/8 * 0)
+0x0EC83, // cos(π/8 * 1)
+0x0B504, // cos(π/8 * 2)
+0x061F7, // cos(π/8 * 3)
+0x0B505, // cos(π/8 * 2)
+0x061F8, // cos(π/8 * 3)
+0x00000, // cos(π/8 * 4)
-0x061F7, // cos(π/8 * 5)
-0x0B504, // cos(π/8 * 6)
-0x061F8, // cos(π/8 * 5)
-0x0B505, // cos(π/8 * 6)
-0x0EC83, // cos(π/8 * 7)
-0x10000, // cos(π/8 * 8)
-0x0EC83, // cos(π/8 * 9)
-0x0B504, // cos(π/8 * 10)
-0x061F7, // cos(π/8 * 11)
-0x0B505, // cos(π/8 * 10)
-0x061F8, // cos(π/8 * 11)
+0x00000, // cos(π/8 * 12)
+0x061F7, // cos(π/8 * 13)
+0x0B504, // cos(π/8 * 14)
+0x061F8, // cos(π/8 * 13)
+0x0B505, // cos(π/8 * 14)
+0x0EC83, // cos(π/8 * 15)
}
+1 -1
View File
@@ -34,7 +34,7 @@ func TestRoundTrip(tt *testing.T) {
want := BlockI16{
+0x00EA, +0x0034, +0x003C, +0x005F,
+0x0082, +0x006E, -0x0064, -0x005D,
-0x00A3, +0x0057, -0x0033, +0x000C,
-0x00A3, +0x0057, -0x0034, +0x000C,
-0x0007, +0x0013, +0x0014, +0x0019,
}
if got != want {
+16 -16
View File
@@ -282,40 +282,40 @@ const (
// cosines is a cosine table as 16.16 fixed point values.
var cosines = [32]int32{
+0x10000, // cos(π/16 * 0)
+0x0FB14, // cos(π/16 * 1)
+0x0FB15, // cos(π/16 * 1)
+0x0EC83, // cos(π/16 * 2)
+0x0D4DB, // cos(π/16 * 3)
+0x0B504, // cos(π/16 * 4)
+0x08E39, // cos(π/16 * 5)
+0x061F7, // cos(π/16 * 6)
+0x0B505, // cos(π/16 * 4)
+0x08E3A, // cos(π/16 * 5)
+0x061F8, // cos(π/16 * 6)
+0x031F1, // cos(π/16 * 7)
+0x00000, // cos(π/16 * 8)
-0x031F1, // cos(π/16 * 9)
-0x061F7, // cos(π/16 * 10)
-0x08E39, // cos(π/16 * 11)
-0x0B504, // cos(π/16 * 12)
-0x061F8, // cos(π/16 * 10)
-0x08E3A, // cos(π/16 * 11)
-0x0B505, // cos(π/16 * 12)
-0x0D4DB, // cos(π/16 * 13)
-0x0EC83, // cos(π/16 * 14)
-0x0FB14, // cos(π/16 * 15)
-0x0FB15, // cos(π/16 * 15)
-0x10000, // cos(π/16 * 16)
-0x0FB14, // cos(π/16 * 17)
-0x0FB15, // cos(π/16 * 17)
-0x0EC83, // cos(π/16 * 18)
-0x0D4DB, // cos(π/16 * 19)
-0x0B504, // cos(π/16 * 20)
-0x08E39, // cos(π/16 * 21)
-0x061F7, // cos(π/16 * 22)
-0x0B505, // cos(π/16 * 20)
-0x08E3A, // cos(π/16 * 21)
-0x061F8, // cos(π/16 * 22)
-0x031F1, // cos(π/16 * 23)
+0x00000, // cos(π/16 * 24)
+0x031F1, // cos(π/16 * 25)
+0x061F7, // cos(π/16 * 26)
+0x08E39, // cos(π/16 * 27)
+0x0B504, // cos(π/16 * 28)
+0x061F8, // cos(π/16 * 26)
+0x08E3A, // cos(π/16 * 27)
+0x0B505, // cos(π/16 * 28)
+0x0D4DB, // cos(π/16 * 29)
+0x0EC83, // cos(π/16 * 30)
+0x0FB14, // cos(π/16 * 31)
+0x0FB15, // cos(π/16 * 31)
}
// biasAndClamp[x & 1023] is (x + 0x80), clamped to the range [0x00, 0xFF], for