From f2517f394022698514ecd7e42f31ee8fed72a6a3 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 12 Jul 2012 20:49:12 +1000 Subject: [PATCH] freetype/truetype: odd/even opcodes. Also re-arrange some of the switch cases to be in opcode order. R=bsiegert CC=golang-dev http://codereview.appspot.com/6352097 --- freetype/truetype/hint.go | 24 ++++++++++++++---------- freetype/truetype/hint_test.go | 20 ++++++++++++++++++++ freetype/truetype/opcodes.go | 6 +++--- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/freetype/truetype/hint.go b/freetype/truetype/hint.go index 4098ee0..921c1fa 100644 --- a/freetype/truetype/hint.go +++ b/freetype/truetype/hint.go @@ -251,16 +251,9 @@ func (h *hinter) run(program []byte) error { top-- h.stack[top-1] = bool2int32(h.stack[top-1] != h.stack[top]) - case opAND: - top-- - h.stack[top-1] = bool2int32(h.stack[top-1] != 0 && h.stack[top] != 0) - - case opOR: - top-- - h.stack[top-1] = bool2int32(h.stack[top-1]|h.stack[top] != 0) - - case opNOT: - h.stack[top-1] = bool2int32(h.stack[top-1] == 0) + case opODD, opEVEN: + i := h.round(f26dot6(h.stack[top-1])) >> 6 + h.stack[top-1] = int32(i&1) ^ int32(opcode-opODD) case opIF: top-- @@ -272,6 +265,17 @@ func (h *hinter) run(program []byte) error { case opEIF: // No-op. + case opAND: + top-- + h.stack[top-1] = bool2int32(h.stack[top-1] != 0 && h.stack[top] != 0) + + case opOR: + top-- + h.stack[top-1] = bool2int32(h.stack[top-1]|h.stack[top] != 0) + + case opNOT: + h.stack[top-1] = bool2int32(h.stack[top-1] == 0) + case opADD: top-- h.stack[top-1] += h.stack[top] diff --git a/freetype/truetype/hint_test.go b/freetype/truetype/hint_test.go index b8c9d14..5039a6c 100644 --- a/freetype/truetype/hint_test.go +++ b/freetype/truetype/hint_test.go @@ -187,6 +187,26 @@ func TestBytecode(t *testing.T) { []int32{1, 0}, "", }, + { + "odd/even", + // Calculate odd(2+31/64), odd(2+32/64), even(2), even(1). + []byte{ + opPUSHB000, // [159] + 159, + opODD, // [0] + opPUSHB000, // [0, 160] + 160, + opODD, // [0, 1] + opPUSHB000, // [0, 1, 128] + 128, + opEVEN, // [0, 1, 1] + opPUSHB000, // [0, 1, 1, 64] + 64, + opEVEN, // [0, 1, 1, 0] + }, + []int32{0, 1, 1, 0}, + "", + }, { "if true", []byte{ diff --git a/freetype/truetype/opcodes.go b/freetype/truetype/opcodes.go index c20cc70..8a3b38f 100644 --- a/freetype/truetype/opcodes.go +++ b/freetype/truetype/opcodes.go @@ -96,8 +96,8 @@ const ( opGTEQ = 0x53 // Greater Than or EQual opEQ = 0x54 // EQual opNEQ = 0x55 // Not EQual - opODD = 0x56 - opEVEN = 0x57 + opODD = 0x56 // ODD + opEVEN = 0x57 // EVEN opIF = 0x58 // IF test opEIF = 0x59 // End IF opAND = 0x5a // logical AND @@ -276,7 +276,7 @@ var popCount = [256]uint8{ 1, 1, 0, 2, 0, 1, 1, q, q, q, q, q, q, q, q, q, // 0x20 - 0x2f q, q, q, q, q, q, q, q, q, q, q, q, q, 0, q, q, // 0x30 - 0x3f 0, 0, 2, 1, q, q, q, q, q, q, q, q, q, q, q, 0, // 0x40 - 0x4f - 2, 2, 2, 2, 2, 2, q, q, 1, 0, 2, 2, 1, q, q, q, // 0x50 - 0x5f + 2, 2, 2, 2, 2, 2, 1, 1, 1, 0, 2, 2, 1, q, q, q, // 0x50 - 0x5f 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6f q, q, q, q, q, q, 1, 1, 2, 2, 0, q, 0, 0, 1, 1, // 0x70 - 0x7f q, q, q, q, q, q, q, q, q, 1, 3, 2, 2, q, q, q, // 0x80 - 0x8f