decode_amd64.s 14 KB

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