decode_arm64.s 12 KB

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