decode_amd64.s 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. // +build gc
  5. #include "textflag.h"
  6. // func decode(dst, src []byte) int
  7. //
  8. // The asm code generally follows the pure Go code in decode_other.go, except
  9. // where marked with a "!!!".
  10. //
  11. // All local variables fit into registers. The non-zero stack size is only to
  12. // spill registers and push args when issuing a CALL. The register allocation:
  13. // - AX scratch
  14. // - BX scratch
  15. // - CX length or x
  16. // - DX offset
  17. // - SI &src[s]
  18. // - DI &dst[d]
  19. // + R8 dst_base
  20. // + R9 dst_len
  21. // + R10 dst_base + dst_len
  22. // + R11 src_base
  23. // + R12 src_len
  24. // + R13 src_base + src_len
  25. // - R14 used by doCopy
  26. // - R15 used by doCopy
  27. //
  28. // The registers R8-R13 (marked with a "+") are set at the start of the
  29. // function, and after a CALL returns, and are not otherwise modified.
  30. //
  31. // The d variable is implicitly DI - R8, and len(dst)-d is R10 - DI.
  32. // The s variable is implicitly SI - R11, and len(src)-s is R13 - SI.
  33. TEXT ·decode(SB), NOSPLIT, $48-56
  34. // Initialize SI, DI and R8-R13.
  35. MOVQ dst_base+0(FP), R8
  36. MOVQ dst_len+8(FP), R9
  37. MOVQ R8, DI
  38. MOVQ R8, R10
  39. ADDQ R9, R10
  40. MOVQ src_base+24(FP), R11
  41. MOVQ src_len+32(FP), R12
  42. MOVQ R11, SI
  43. MOVQ R11, R13
  44. ADDQ R12, R13
  45. loop:
  46. // for s < len(src)
  47. CMPQ SI, R13
  48. JEQ end
  49. // CX = uint32(src[s])
  50. //
  51. // switch src[s] & 0x03
  52. MOVBLZX (SI), CX
  53. MOVL CX, BX
  54. ANDL $3, BX
  55. CMPL BX, $1
  56. JAE tagCopy
  57. // ----------------------------------------
  58. // The code below handles literal tags.
  59. // case tagLiteral:
  60. // x := uint32(src[s] >> 2)
  61. // switch
  62. SHRL $2, CX
  63. CMPL CX, $60
  64. JAE tagLit60Plus
  65. // case x < 60:
  66. // s++
  67. INCQ SI
  68. doLit:
  69. // This is the end of the inner "switch", when we have a literal tag.
  70. //
  71. // We assume that CX == x and x fits in a uint32, where x is the variable
  72. // used in the pure Go decode_other.go code.
  73. // length = int(x) + 1
  74. //
  75. // Unlike the pure Go code, we don't need to check if length <= 0 because
  76. // CX can hold 64 bits, so the increment cannot overflow.
  77. INCQ CX
  78. // Prepare to check if copying length bytes will run past the end of dst or
  79. // src.
  80. //
  81. // AX = len(dst) - d
  82. // BX = len(src) - s
  83. MOVQ R10, AX
  84. SUBQ DI, AX
  85. MOVQ R13, BX
  86. SUBQ SI, BX
  87. // !!! Try a faster technique for short (16 or fewer bytes) copies.
  88. //
  89. // if length > 16 || len(dst)-d < 16 || len(src)-s < 16 {
  90. // goto callMemmove // Fall back on calling runtime·memmove.
  91. // }
  92. //
  93. // The C++ snappy code calls this TryFastAppend. It also checks len(src)-s
  94. // against 21 instead of 16, because it cannot assume that all of its input
  95. // is contiguous in memory and so it needs to leave enough source bytes to
  96. // read the next tag without refilling buffers, but Go's Decode assumes
  97. // contiguousness (the src argument is a []byte).
  98. CMPQ CX, $16
  99. JGT callMemmove
  100. CMPQ AX, $16
  101. JLT callMemmove
  102. CMPQ BX, $16
  103. JLT callMemmove
  104. // !!! Implement the copy from src to dst as a 16-byte load and store.
  105. // (Decode's documentation says that dst and src must not overlap.)
  106. //
  107. // This always copies 16 bytes, instead of only length bytes, but that's
  108. // OK. If the input is a valid Snappy encoding then subsequent iterations
  109. // will fix up the overrun. Otherwise, Decode returns a nil []byte (and a
  110. // non-nil error), so the overrun will be ignored.
  111. //
  112. // Note that on amd64, it is legal and cheap to issue unaligned 8-byte or
  113. // 16-byte loads and stores. This technique probably wouldn't be as
  114. // effective on architectures that are fussier about alignment.
  115. MOVOU 0(SI), X0
  116. MOVOU X0, 0(DI)
  117. // d += length
  118. // s += length
  119. ADDQ CX, DI
  120. ADDQ CX, SI
  121. JMP loop
  122. callMemmove:
  123. // if length > len(dst)-d || length > len(src)-s { etc }
  124. CMPQ CX, AX
  125. JGT errCorrupt
  126. CMPQ CX, BX
  127. JGT errCorrupt
  128. // copy(dst[d:], src[s:s+length])
  129. //
  130. // This means calling runtime·memmove(&dst[d], &src[s], length), so we push
  131. // DI, SI and CX as arguments. Coincidentally, we also need to spill those
  132. // three registers to the stack, to save local variables across the CALL.
  133. MOVQ DI, 0(SP)
  134. MOVQ SI, 8(SP)
  135. MOVQ CX, 16(SP)
  136. MOVQ DI, 24(SP)
  137. MOVQ SI, 32(SP)
  138. MOVQ CX, 40(SP)
  139. CALL runtime·memmove(SB)
  140. // Restore local variables: unspill registers from the stack and
  141. // re-calculate R8-R13.
  142. MOVQ 24(SP), DI
  143. MOVQ 32(SP), SI
  144. MOVQ 40(SP), CX
  145. MOVQ dst_base+0(FP), R8
  146. MOVQ dst_len+8(FP), R9
  147. MOVQ R8, R10
  148. ADDQ R9, R10
  149. MOVQ src_base+24(FP), R11
  150. MOVQ src_len+32(FP), R12
  151. MOVQ R11, R13
  152. ADDQ R12, R13
  153. // d += length
  154. // s += length
  155. ADDQ CX, DI
  156. ADDQ CX, SI
  157. JMP loop
  158. tagLit60Plus:
  159. // !!! This fragment does the
  160. //
  161. // s += x - 58; if uint(s) > uint(len(src)) { etc }
  162. //
  163. // checks. In the asm version, we code it once instead of once per switch case.
  164. ADDQ CX, SI
  165. SUBQ $58, SI
  166. MOVQ SI, BX
  167. SUBQ R11, BX
  168. CMPQ BX, R12
  169. JA errCorrupt
  170. // case x == 60:
  171. CMPL CX, $61
  172. JEQ tagLit61
  173. JA tagLit62Plus
  174. // x = uint32(src[s-1])
  175. MOVBLZX -1(SI), CX
  176. JMP doLit
  177. tagLit61:
  178. // case x == 61:
  179. // x = uint32(src[s-2]) | uint32(src[s-1])<<8
  180. MOVWLZX -2(SI), CX
  181. JMP doLit
  182. tagLit62Plus:
  183. CMPL CX, $62
  184. JA tagLit63
  185. // case x == 62:
  186. // x = uint32(src[s-3]) | uint32(src[s-2])<<8 | uint32(src[s-1])<<16
  187. MOVWLZX -3(SI), CX
  188. MOVBLZX -1(SI), BX
  189. SHLL $16, BX
  190. ORL BX, CX
  191. JMP doLit
  192. tagLit63:
  193. // case x == 63:
  194. // x = uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24
  195. MOVL -4(SI), CX
  196. JMP doLit
  197. // The code above handles literal tags.
  198. // ----------------------------------------
  199. // The code below handles copy tags.
  200. tagCopy2:
  201. // case tagCopy2:
  202. // s += 3
  203. ADDQ $3, SI
  204. // if uint(s) > uint(len(src)) { etc }
  205. MOVQ SI, BX
  206. SUBQ R11, BX
  207. CMPQ BX, R12
  208. JA errCorrupt
  209. // length = 1 + int(src[s-3])>>2
  210. SHRQ $2, CX
  211. INCQ CX
  212. // offset = int(src[s-2]) | int(src[s-1])<<8
  213. MOVWQZX -2(SI), DX
  214. JMP doCopy
  215. tagCopy:
  216. // We have a copy tag. We assume that:
  217. // - BX == src[s] & 0x03
  218. // - CX == src[s]
  219. CMPQ BX, $2
  220. JEQ tagCopy2
  221. JA errUC4T
  222. // case tagCopy1:
  223. // s += 2
  224. ADDQ $2, SI
  225. // if uint(s) > uint(len(src)) { etc }
  226. MOVQ SI, BX
  227. SUBQ R11, BX
  228. CMPQ BX, R12
  229. JA errCorrupt
  230. // offset = int(src[s-2])&0xe0<<3 | int(src[s-1])
  231. MOVQ CX, DX
  232. ANDQ $0xe0, DX
  233. SHLQ $3, DX
  234. MOVBQZX -1(SI), BX
  235. ORQ BX, DX
  236. // length = 4 + int(src[s-2])>>2&0x7
  237. SHRQ $2, CX
  238. ANDQ $7, CX
  239. ADDQ $4, CX
  240. doCopy:
  241. // This is the end of the outer "switch", when we have a copy tag.
  242. //
  243. // We assume that:
  244. // - CX == length && CX > 0
  245. // - DX == offset
  246. // if offset <= 0 { etc }
  247. CMPQ DX, $0
  248. JLE errCorrupt
  249. // if d < offset { etc }
  250. MOVQ DI, BX
  251. SUBQ R8, BX
  252. CMPQ BX, DX
  253. JLT errCorrupt
  254. // if length > len(dst)-d { etc }
  255. MOVQ R10, BX
  256. SUBQ DI, BX
  257. CMPQ CX, BX
  258. JGT errCorrupt
  259. // forwardCopy(dst[d:d+length], dst[d-offset:]); d += length
  260. //
  261. // Set:
  262. // - R14 = len(dst)-d
  263. // - R15 = &dst[d-offset]
  264. MOVQ R10, R14
  265. SUBQ DI, R14
  266. MOVQ DI, R15
  267. SUBQ DX, R15
  268. // !!! Try a faster technique for short (16 or fewer bytes) forward copies.
  269. //
  270. // First, try using two 8-byte load/stores, similar to the doLit technique
  271. // above. Even if dst[d:d+length] and dst[d-offset:] can overlap, this is
  272. // still OK if offset >= 8. Note that this has to be two 8-byte load/stores
  273. // and not one 16-byte load/store, and the first store has to be before the
  274. // second load, due to the overlap if offset is in the range [8, 16).
  275. //
  276. // if length > 16 || offset < 8 || len(dst)-d < 16 {
  277. // goto slowForwardCopy
  278. // }
  279. // copy 16 bytes
  280. // d += length
  281. CMPQ CX, $16
  282. JGT slowForwardCopy
  283. CMPQ DX, $8
  284. JLT slowForwardCopy
  285. CMPQ R14, $16
  286. JLT slowForwardCopy
  287. MOVQ 0(R15), AX
  288. MOVQ AX, 0(DI)
  289. MOVQ 8(R15), BX
  290. MOVQ BX, 8(DI)
  291. ADDQ CX, DI
  292. JMP loop
  293. slowForwardCopy:
  294. // !!! If the forward copy is longer than 16 bytes, or if offset < 8, we
  295. // can still try 8-byte load stores, provided we can overrun up to 10 extra
  296. // bytes. As above, the overrun will be fixed up by subsequent iterations
  297. // of the outermost loop.
  298. //
  299. // The C++ snappy code calls this technique IncrementalCopyFastPath. Its
  300. // commentary says:
  301. //
  302. // ----
  303. //
  304. // The main part of this loop is a simple copy of eight bytes at a time
  305. // until we've copied (at least) the requested amount of bytes. However,
  306. // if d and d-offset are less than eight bytes apart (indicating a
  307. // repeating pattern of length < 8), we first need to expand the pattern in
  308. // order to get the correct results. For instance, if the buffer looks like
  309. // this, with the eight-byte <d-offset> and <d> patterns marked as
  310. // intervals:
  311. //
  312. // abxxxxxxxxxxxx
  313. // [------] d-offset
  314. // [------] d
  315. //
  316. // a single eight-byte copy from <d-offset> to <d> will repeat the pattern
  317. // once, after which we can move <d> two bytes without moving <d-offset>:
  318. //
  319. // ababxxxxxxxxxx
  320. // [------] d-offset
  321. // [------] d
  322. //
  323. // and repeat the exercise until the two no longer overlap.
  324. //
  325. // This allows us to do very well in the special case of one single byte
  326. // repeated many times, without taking a big hit for more general cases.
  327. //
  328. // The worst case of extra writing past the end of the match occurs when
  329. // offset == 1 and length == 1; the last copy will read from byte positions
  330. // [0..7] and write to [4..11], whereas it was only supposed to write to
  331. // position 1. Thus, ten excess bytes.
  332. //
  333. // ----
  334. //
  335. // That "10 byte overrun" worst case is confirmed by Go's
  336. // TestSlowForwardCopyOverrun, which also tests the fixUpSlowForwardCopy
  337. // and finishSlowForwardCopy algorithm.
  338. //
  339. // if length > len(dst)-d-10 {
  340. // goto verySlowForwardCopy
  341. // }
  342. SUBQ $10, R14
  343. CMPQ CX, R14
  344. JGT verySlowForwardCopy
  345. makeOffsetAtLeast8:
  346. // !!! As above, expand the pattern so that offset >= 8 and we can use
  347. // 8-byte load/stores.
  348. //
  349. // for offset < 8 {
  350. // copy 8 bytes from dst[d-offset:] to dst[d:]
  351. // length -= offset
  352. // d += offset
  353. // offset += offset
  354. // // The two previous lines together means that d-offset, and therefore
  355. // // R15, is unchanged.
  356. // }
  357. CMPQ DX, $8
  358. JGE fixUpSlowForwardCopy
  359. MOVQ (R15), BX
  360. MOVQ BX, (DI)
  361. SUBQ DX, CX
  362. ADDQ DX, DI
  363. ADDQ DX, DX
  364. JMP makeOffsetAtLeast8
  365. fixUpSlowForwardCopy:
  366. // !!! Add length (which might be negative now) to d (implied by DI being
  367. // &dst[d]) so that d ends up at the right place when we jump back to the
  368. // top of the loop. Before we do that, though, we save DI to AX so that, if
  369. // length is positive, copying the remaining length bytes will write to the
  370. // right place.
  371. MOVQ DI, AX
  372. ADDQ CX, DI
  373. finishSlowForwardCopy:
  374. // !!! Repeat 8-byte load/stores until length <= 0. Ending with a negative
  375. // length means that we overrun, but as above, that will be fixed up by
  376. // subsequent iterations of the outermost loop.
  377. CMPQ CX, $0
  378. JLE loop
  379. MOVQ (R15), BX
  380. MOVQ BX, (AX)
  381. ADDQ $8, R15
  382. ADDQ $8, AX
  383. SUBQ $8, CX
  384. JMP finishSlowForwardCopy
  385. verySlowForwardCopy:
  386. // verySlowForwardCopy is a simple implementation of forward copy. In C
  387. // parlance, this is a do/while loop instead of a while loop, since we know
  388. // that length > 0. In Go syntax:
  389. //
  390. // for {
  391. // dst[d] = dst[d - offset]
  392. // d++
  393. // length--
  394. // if length == 0 {
  395. // break
  396. // }
  397. // }
  398. MOVB (R15), BX
  399. MOVB BX, (DI)
  400. INCQ R15
  401. INCQ DI
  402. DECQ CX
  403. JNZ verySlowForwardCopy
  404. JMP loop
  405. // The code above handles copy tags.
  406. // ----------------------------------------
  407. end:
  408. // This is the end of the "for s < len(src)".
  409. //
  410. // if d != len(dst) { etc }
  411. CMPQ DI, R10
  412. JNE errCorrupt
  413. // return 0
  414. MOVQ $0, ret+48(FP)
  415. RET
  416. errCorrupt:
  417. // return decodeErrCodeCorrupt
  418. MOVQ $1, ret+48(FP)
  419. RET
  420. errUC4T:
  421. // return decodeErrCodeUnsupportedCopy4Tag
  422. MOVQ $3, ret+48(FP)
  423. RET