Skip to content

Instantly share code, notes, and snippets.

@vizee
Created March 20, 2020 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vizee/5679e08d9a027e1222dc5d7fc2efd978 to your computer and use it in GitHub Desktop.
Save vizee/5679e08d9a027e1222dc5d7fc2efd978 to your computer and use it in GitHub Desktop.
search indirect tag by binary search
#include "textflag.h"
// func searchTag(m *Message, tag int) int
TEXT ·searchTag(SB), NOSPLIT, $0-24
MOVQ m+0(FP), BX
MOVQ tag+8(FP), R9
MOVQ 16(BX), DI
MOVQ 24(BX), CX
MOVQ 40(BX), SI
XORQ DX, DX
LEAQ -1(CX), CX
loop:
CMPQ DX, CX
JG notfound
LEAQ (DX)(CX*1), BX
SHRQ $1, AX
MOVQ (SI)(BX*8), AX
MOVQ (DI)(AX*8), R8
CMPQ R9, (R8)
JZ return
JL right
LEAQ 1(BX), DX
JMP loop
right:
LEAQ -1(BX), CX
JMP loop
notfound:
XORQ AX, AX
NOTQ AX
JMP return
return:
MOVQ AX, ret+16(FP)
RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment