decode_amd64.s 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. #include "textflag.h"
  5. // func decode(dst, src []byte) int
  6. //
  7. // The asm code generally follows the pure Go code in decode_other.go, except
  8. // where marked with a "!!!".
  9. //
  10. // All local variables fit into registers. The non-zero stack size is only to
  11. // spill registers and push args when issuing a CALL. The register allocation:
  12. // - AX scratch
  13. // - BX scratch
  14. // - CX length or x
  15. // - DX offset
  16. // - SI &src[s]
  17. // - DI &dst[d]
  18. // + R8 dst_base
  19. // + R9 dst_len
  20. // + R10 dst_base + dst_len
  21. // + R11 src_base
  22. // + R12 src_len
  23. // + R13 src_base + src_len
  24. // - R14 unused
  25. // - R15 used by doCopy
  26. //
  27. // The registers R8-R13 (marked with a "+") are set at the start of the
  28. // function, and after a CALL returns, and are not otherwise modified.
  29. //
  30. // The d variable is implicitly DI - R8, and len(dst)-d is R10 - DI.
  31. // The s variable is implicitly SI - R11, and len(src)-s is R13 - SI.
  32. TEXT ·decode(SB), NOSPLIT, $48-56
  33. // Initialize SI, DI and R8-R13.
  34. MOVQ dst_base+0(FP), R8
  35. MOVQ dst_len+8(FP), R9
  36. MOVQ R8, DI
  37. MOVQ R8, R10
  38. ADDQ R9, R10
  39. MOVQ src_base+24(FP), R11
  40. MOVQ src_len+32(FP), R12
  41. MOVQ R11, SI
  42. MOVQ R11, R13
  43. ADDQ R12, R13
  44. loop:
  45. // for s < len(src)
  46. CMPQ SI, R13
  47. JEQ end
  48. // CX = uint32(src[s])
  49. //
  50. // switch src[s] & 0x03
  51. MOVBLZX (SI), CX
  52. MOVL CX, BX
  53. ANDL $3, BX
  54. CMPL BX, $1
  55. JAE tagCopy
  56. // ----------------------------------------
  57. // The code below handles literal tags.
  58. // case tagLiteral:
  59. // x := uint32(src[s] >> 2)
  60. // switch
  61. SHRL $2, CX
  62. CMPL CX, $60
  63. JAE tagLit60Plus
  64. // case x < 60:
  65. // s++
  66. INCQ SI
  67. doLit:
  68. // This is the end of the inner "switch", when we have a literal tag.
  69. //
  70. // We assume that CX == x and x fits in a uint32, where x is the variable
  71. // used in the pure Go decode_other.go code.
  72. // length = int(x) + 1
  73. //
  74. // Unlike the pure Go code, we don't need to check if length <= 0 because
  75. // CX can hold 64 bits, so the increment cannot overflow.
  76. INCQ CX
  77. // Prepare to check if copying length bytes will run past the end of dst or
  78. // src.
  79. //
  80. // AX = len(dst) - d
  81. // BX = len(src) - s
  82. MOVQ R10, AX
  83. SUBQ DI, AX
  84. MOVQ R13, BX
  85. SUBQ SI, BX
  86. // !!! Try a faster technique for short (16 or fewer bytes) copies.
  87. //
  88. // if length > 16 || len(dst)-d < 16 || len(src)-s < 16 {
  89. // goto callMemmove // Fall back on calling runtime·memmove.
  90. // }
  91. //
  92. // The C++ snappy code calls this TryFastAppend. It also checks len(src)-s
  93. // against 21 instead of 16, because it cannot assume that all of its input
  94. // is contiguous in memory and so it needs to leave enough source bytes to
  95. // read the next tag without refilling buffers, but Go's Decode assumes
  96. // contiguousness (the src argument is a []byte).
  97. CMPQ CX, $16
  98. JGT callMemmove
  99. CMPQ AX, $16
  100. JLT callMemmove
  101. CMPQ BX, $16
  102. JLT callMemmove
  103. // !!! Implement the copy from src to dst as two 8-byte loads and stores.
  104. // (Decode's documentation says that dst and src must not overlap.)
  105. //
  106. // This always copies 16 bytes, instead of only length bytes, but that's
  107. // OK. If the input is a valid Snappy encoding then subsequent iterations
  108. // will fix up the overrun. Otherwise, Decode returns a nil []byte (and a
  109. // non-nil error), so the overrun will be ignored.
  110. //
  111. // Note that on amd64, it is legal and cheap to issue unaligned 8-byte
  112. // loads and stores. This technique probably wouldn't be as effective on
  113. // architectures that are fussier about alignment.
  114. MOVQ 0(SI), AX
  115. MOVQ AX, 0(DI)
  116. MOVQ 8(SI), BX
  117. MOVQ BX, 8(DI)
  118. // d += length
  119. // s += length
  120. ADDQ CX, DI
  121. ADDQ CX, SI
  122. JMP loop
  123. callMemmove:
  124. // if length > len(dst)-d || length > len(src)-s { etc }
  125. CMPQ CX, AX
  126. JGT errCorrupt
  127. CMPQ CX, BX
  128. JGT errCorrupt
  129. // copy(dst[d:], src[s:s+length])
  130. //
  131. // This means calling runtime·memmove(&dst[d], &src[s], length), so we push
  132. // DI, SI and CX as arguments. Coincidentally, we also need to spill those
  133. // three registers to the stack, to save local variables across the CALL.
  134. MOVQ DI, 0(SP)
  135. MOVQ SI, 8(SP)
  136. MOVQ CX, 16(SP)
  137. MOVQ DI, 24(SP)
  138. MOVQ SI, 32(SP)
  139. MOVQ CX, 40(SP)
  140. CALL runtime·memmove(SB)
  141. // Restore local variables: unspill registers from the stack and
  142. // re-calculate R8-R13.
  143. MOVQ 24(SP), DI
  144. MOVQ 32(SP), SI
  145. MOVQ 40(SP), CX
  146. MOVQ dst_base+0(FP), R8
  147. MOVQ dst_len+8(FP), R9
  148. MOVQ R8, R10
  149. ADDQ R9, R10
  150. MOVQ src_base+24(FP), R11
  151. MOVQ src_len+32(FP), R12
  152. MOVQ R11, R13
  153. ADDQ R12, R13
  154. // d += length
  155. // s += length
  156. ADDQ CX, DI
  157. ADDQ CX, SI
  158. JMP loop
  159. tagLit60Plus:
  160. // !!! This fragment does the
  161. //
  162. // s += x - 58; if uint(s) > uint(len(src)) { etc }
  163. //
  164. // checks. In the asm version, we code it once instead of once per switch case.
  165. ADDQ CX, SI
  166. SUBQ $58, SI
  167. MOVQ SI, BX
  168. SUBQ R11, BX
  169. CMPQ BX, R12
  170. JA errCorrupt
  171. // case x == 60:
  172. CMPL CX, $61
  173. JEQ tagLit61
  174. JA tagLit62Plus
  175. // x = uint32(src[s-1])
  176. MOVBLZX -1(SI), CX
  177. JMP doLit
  178. tagLit61:
  179. // case x == 61:
  180. // x = uint32(src[s-2]) | uint32(src[s-1])<<8
  181. MOVWLZX -2(SI), CX
  182. JMP doLit
  183. tagLit62Plus:
  184. CMPL CX, $62
  185. JA tagLit63
  186. // case x == 62:
  187. // x = uint32(src[s-3]) | uint32(src[s-2])<<8 | uint32(src[s-1])<<16
  188. MOVWLZX -3(SI), CX
  189. MOVBLZX -1(SI), BX
  190. SHLL $16, BX
  191. ORL BX, CX
  192. JMP doLit
  193. tagLit63:
  194. // case x == 63:
  195. // x = uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24
  196. MOVL -4(SI), CX
  197. JMP doLit
  198. // The code above handles literal tags.
  199. // ----------------------------------------
  200. // The code below handles copy tags.
  201. tagCopy2:
  202. // case tagCopy2:
  203. // s += 3
  204. ADDQ $3, SI
  205. // if uint(s) > uint(len(src)) { etc }
  206. MOVQ SI, BX
  207. SUBQ R11, BX
  208. CMPQ BX, R12
  209. JA errCorrupt
  210. // length = 1 + int(src[s-3])>>2
  211. SHRQ $2, CX
  212. INCQ CX
  213. // offset = int(src[s-2]) | int(src[s-1])<<8
  214. MOVWQZX -2(SI), DX
  215. JMP doCopy
  216. tagCopy:
  217. // We have a copy tag. We assume that:
  218. // - BX == src[s] & 0x03
  219. // - CX == src[s]
  220. CMPQ BX, $2
  221. JEQ tagCopy2
  222. JA errUC4T
  223. // case tagCopy1:
  224. // s += 2
  225. ADDQ $2, SI
  226. // if uint(s) > uint(len(src)) { etc }
  227. MOVQ SI, BX
  228. SUBQ R11, BX
  229. CMPQ BX, R12
  230. JA errCorrupt
  231. // offset = int(src[s-2])&0xe0<<3 | int(src[s-1])
  232. MOVQ CX, DX
  233. ANDQ $0xe0, DX
  234. SHLQ $3, DX
  235. MOVBQZX -1(SI), BX
  236. ORQ BX, DX
  237. // length = 4 + int(src[s-2])>>2&0x7
  238. SHRQ $2, CX
  239. ANDQ $7, CX
  240. ADDQ $4, CX
  241. doCopy:
  242. // This is the end of the outer "switch", when we have a copy tag.
  243. //
  244. // We assume that:
  245. // - CX == length && CX > 0
  246. // - DX == offset
  247. // if offset <= 0 { etc }
  248. CMPQ DX, $0
  249. JLE errCorrupt
  250. // if d < offset { etc }
  251. MOVQ DI, BX
  252. SUBQ R8, BX
  253. CMPQ BX, DX
  254. JLT errCorrupt
  255. // if length > len(dst)-d { etc }
  256. MOVQ R10, BX
  257. SUBQ DI, BX
  258. CMPQ CX, BX
  259. JGT errCorrupt
  260. // forwardCopy(dst[d:d+length], dst[d-offset:]); d += length
  261. //
  262. // Set:
  263. // - R15 = &dst[d-offset]
  264. MOVQ DI, R15
  265. SUBQ DX, R15
  266. verySlowForwardCopy:
  267. // verySlowForwardCopy is a simple implementation of forward copy. In C
  268. // parlance, this is a do/while loop instead of a while loop, since we know
  269. // that length > 0. In Go syntax:
  270. //
  271. // for {
  272. // dst[d] = dst[d - offset]
  273. // d++
  274. // length--
  275. // if length == 0 {
  276. // break
  277. // }
  278. // }
  279. MOVB (R15), BX
  280. MOVB BX, (DI)
  281. INCQ R15
  282. INCQ DI
  283. DECQ CX
  284. JNZ verySlowForwardCopy
  285. JMP loop
  286. // The code above handles copy tags.
  287. // ----------------------------------------
  288. end:
  289. // This is the end of the "for s < len(src)".
  290. //
  291. // if d != len(dst) { etc }
  292. CMPQ DI, R10
  293. JNE errCorrupt
  294. // return 0
  295. MOVQ $0, ret+48(FP)
  296. RET
  297. errCorrupt:
  298. // return decodeErrCodeCorrupt
  299. MOVQ $1, ret+48(FP)
  300. RET
  301. errUC4T:
  302. // return decodeErrCodeUnsupportedCopy4Tag
  303. MOVQ $3, ret+48(FP)
  304. RET