decode_amd64.s 12 KB

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