2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 This file contains arm64 hand optimized implementation of WKdm memory page decompressor.
32 void WKdm_decompress (WK_word* src_buf, WK_word* dest_buf, WK_word *scratch, __unused__ unsigned int words);
35 src_buf : address of input compressed data buffer
36 dest_buf : address of output decompressed buffer
37 scratch : an 8-k bytes scratch mempro provided by the caller
38 words : this argument is not used in the implementation
39 (The 4th argument is, in fact, used by the Mostly Zero Value decoder)
43 the input buffer is decompressed and the dest_buf is written with decompressed data.
45 Am algorithm description of the WKdm compress and bit stream format can be found in the WKdm Compress arm64 assembly code WKdmCompress.s
47 The bit stream (*src_buf) consists of
49 b. 256 bytes for 1024 packed tags
50 c. (varying number of) words for new words not matched to dictionary word.
51 d. (varying number of) 32-bit words for packed 4-bit dict_indices (for class 1 and 3)
52 e. (varying number of) 32-bit words for packed 10-bit low bits (for class 1)
54 where the header (of 3 words) specifies the ending boundaries (in 32-bit words) of the bit stream of c,d,e, respectively.
56 The decompressor 1st unpacking the bit stream component b/d/e into temorary buffers. Then it sequentially decodes the decompressed word as follows
58 for (i=0;i<1024;i++) {
61 case 0 : *dest_buf++ = 0; break;
62 case 1 : dict_word = dictionary[*dict_index]; dictionary[*dict_index++] = *dest_buf++ = dict_word&0xfffffc00 | *LowBits++; break;
63 case 2 : x = *new_word++; k = (x>>10)&255; k = hashTable[k]; dictionary[k] = *dest_buf++ = x; break;
64 case 3 : *dest_buf++ = dictionary[*dict_index++]; break;
69 Added zero page, single value page, sparse page, early abort optimizations
73 #define MZV_MAGIC 17185 // magic value used to identify MZV page encoding
75 #ifndef PAGES_SIZE_IN_KBYTES
76 #define PAGES_SIZE_IN_KBYTES 4
79 #if !((PAGES_SIZE_IN_KBYTES==4) || (PAGES_SIZE_IN_KBYTES==16))
80 #error "Only PAGES_SIZE_IN_KBYTES = 4 or 16 is supported"
83 #define scale (PAGES_SIZE_IN_KBYTES/4)
90 void WKdm_decompress (WK_word* src_buf, WK_word* dest_buf, WK_word* scratch, unsigned int bytes);
93 .globl _WKdm_decompress_4k
97 -------- symbolizing registers --------
98 the arm64 code was ported from x86_64 so we name some registers that are used as temp variables with x86_64 register names.
105 #define dictionary sp
114 #define tags_counter x7
124 ------ scratch memory for local variables ---------
127 [scratch,#0] : tempTagsArray
128 [scratch,#1024] : tempQPosArray
129 [scratch,#2048] : tempLowBitsArray
136 st1.4s {v0,v1,v2},[rax],#48
137 st1.4s {v3,v4,v5},[rax],#48
142 ldr eax, [src_buf] // read the 1st word from the header
144 cmp eax, ecx // is the alternate packer used (i.e. is MZV page)?
145 b.ne L_default_decompressor // default decompressor was used
147 // Mostly Zero Page Handling...
149 add src_buf, src_buf, 4 // skip the header
151 mov rcx, #(PAGES_SIZE_IN_KBYTES*1024) // number of bytes to zero out
153 dc zva, rax // zero 64 bytes. since dest_buf is a page, it will be 4096 or 16384 byte aligned
164 mov r12, #4 // current byte position in src to read from
167 ldr eax, [src_buf], #4 // get the word
168 ldrh edx, [src_buf], #2 // get the index
169 str eax, [dest_buf, rdx] // store non-0 word in the destination buffer
170 add r12, r12, #6 // 6 more bytes processed
171 cmp r12, n_bytes // finished processing all the bytes?
176 L_default_decompressor:
179 ---------------------- set up registers and PRELOAD_DICTIONARY ---------------------------------
181 // NOTE: ALL THE DICTIONARY VALUES MUST BE INITIALIZED TO ZERO TO MIRROR THE COMPRESSOR
182 adrp rbx, _table_2bits@GOTPAGE
183 stp xzr, xzr, [dictionary, #0]
184 add r10, src_buf, #(12+256*scale) // TAGS_AREA_END
185 stp xzr, xzr, [dictionary, #16]
186 add rax, src_buf, #12 // TAGS_AREA_START
187 ldr rbx, [rbx, _table_2bits@GOTPAGEOFF]
188 stp xzr, xzr, [dictionary, #32]
189 mov rcx, scratch // tempTagsArray
190 stp xzr, xzr, [dictionary, #48]
195 ------------------------------ unpacking bit stream ----------------------------------
198 // WK_unpack_2bits(TAGS_AREA_START(src_buf), TAGS_AREA_END(src_buf), tempTagsArray);
200 unpacking 16 2-bit tags (from a 32-bit word) into 16 bytes
201 for arm64, this can be done by
202 1. read the input 32-bit word into GPR w
203 2. duplicate GPR into 4 elements in a vector register v0
204 3. ushl.4s vd, v0, vshift where vshift = {0, -2, -4, -6}
205 4. and.4s vd, vd, vmask where vmask = 0x03030303030303030303030303030303
209 ldr q5, [rax], #16 // read 4 32-bit words for 64 2-bit tags
210 dup.4s v2, v5[0] // duplicate to 4 elements
211 dup.4s v3, v5[1] // duplicate to 4 elements
212 dup.4s v4, v5[2] // duplicate to 4 elements
213 dup.4s v5, v5[3] // duplicate to 4 elements
214 ushl.4s v2, v2, v0 // v1 = {0, -2, -4, -6}
215 ushl.4s v3, v3, v0 // v1 = {0, -2, -4, -6}
216 ushl.4s v4, v4, v0 // v1 = {0, -2, -4, -6}
217 ushl.4s v5, v5, v0 // v1 = {0, -2, -4, -6}
218 and.16b v2, v2, v1 // v2 = {3,3,...,3}
219 and.16b v3, v3, v1 // v2 = {3,3,...,3}
220 and.16b v4, v4, v1 // v2 = {3,3,...,3}
221 and.16b v5, v5, v1 // v2 = {3,3,...,3}
222 cmp r10, rax // TAGS_AREA_END vs TAGS_AREA_START
223 st1.4s {v2,v3,v4,v5}, [rcx], #64 // write 64 tags into tempTagsArray
224 b.hi L_WK_unpack_2bits // if not reach TAGS_AREA_END, repeat L_WK_unpack_2bits
227 // WK_unpack_4bits(QPOS_AREA_START(src_buf), QPOS_AREA_END(src_buf), tempQPosArray);
229 ldp w8, w9, [src_buf] // WKdm header qpos start and end
230 adrp rbx, _table_4bits@GOTPAGE
231 subs x14, r9, r8 // x14 = (QPOS_AREA_END - QPOS_AREA_START)/4
232 add r8, src_buf, r8, lsl #2 // QPOS_AREA_START
233 add r9, src_buf, r9, lsl #2 // QPOS_AREA_END
235 b.ls 1f // if QPOS_AREA_END <= QPOS_AREA_START, skip L_WK_unpack_4bits
236 ldr rbx, [rbx, _table_4bits@GOTPAGEOFF]
237 add rcx, scratch, #(1024*scale) // tempQPosArray
240 b.ls 2f // do loop of 2 only if w14 >= 5
242 ldr d2, [r8], #8 // read a 32-bit word for 8 4-bit positions
245 ushl.4s v2, v2, v0 // v1 = {0, -4, 0, -4}
246 and.16b v2, v2, v1 // v2 = {15,15,...,15}
248 b.hi L_WK_unpack_4bits
253 ldr s3, [r8], #4 // read a 32-bit word for 8 4-bit positions
254 dup.2s v2, v3[0] // duplicate to 2 elements
255 ushl.2s v2, v2, v0 // v1 = {0, -4}
256 and.8b v2, v2, v1 // v2 = {15,15,...,15}
257 str d2, [rcx], #8 // write 16 tags into tempTagsArray
261 // WK_unpack_3_tenbits(LOW_BITS_AREA_START(src_buf), LOW_BITS_AREA_END(src_buf), tempLowBitsArray);
263 ldr eax, [src_buf,#8] // LOW_BITS_AREA_END offset
264 add r8, src_buf, rax, lsl #2 // LOW_BITS_AREA_END
265 add rcx, scratch, #(2048*scale) // tempLowBitsArray
267 add r11, scratch, #(4096*scale-2) // final tenbits for the rare case
269 add r11, scratch, #(4096*scale) // final tenbits for the rare case
272 subs r8, r8, r9 // LOW_BITS_AREA_START vs LOW_BITS_AREA_END
273 b.ls 1f // if START>=END, skip L_WK_unpack_3_tenbits
275 adrp rbx, _table_10bits@GOTPAGE
276 ldr rbx, [rbx, _table_10bits@GOTPAGEOFF]
277 ld1.4s {v0,v1,v2,v3},[rbx]
280 a very rare case : 1024 tenbits, 1023 + 1 -> 341 + final 1 that is padded with 2 zeros
281 since the scratch memory is 4k (2k for this section), we need to pay attention to the last case
282 so we don't overwrite to the scratch memory
284 we 1st do a single 3_tenbits, followed by 2x_3_tenbits loop, and detect whether the last 3_tenbits
288 subs r8, r8, #4 // pre-decrement by 8
289 ldr s4, [r9], #4 // read 32-bit words for 3 low 10-bits
290 zip1.4s v4, v4, v4 // bits 0-63 contain first triplet twice, bits 64-127 contain second triplet twice.
291 ushl.4s v5, v4, v0 // v0 = {6, 0, 6, 0}, places second element of triplets into bits 16-25 and 80-89.
292 ushl.4s v4, v4, v1 // v1 = {0, -20, 0, -20}, places third element of triplets into bits 32-41 and 96-105.
293 and.16b v5, v5, v2 // v2 = {0, 1023, 0, 0, 0, 1023, 0, 0}, isolate second element of triplets.
294 and.16b v4, v4, v3 // v3 = {1023, 0, 1023, 0, 1023, 0, 1023, 0}, isolate first and third elements of triplets
295 orr.16b v4, v4, v5 // combine data
296 str d4, [rcx], #6 // write 3 low 10-bits
300 subs r8, r8, #8 // pre-decrement by 8
301 b.lt L_WK_unpack_3_tenbits
303 L_WK_unpack_2x_3_tenbits:
304 ldr d4, [r9], #8 // read 2 32-bit words for a pair of 3 low 10-bits
305 zip1.4s v4, v4, v4 // bits 0-63 contain first triplet twice, bits 64-127 contain second triplet twice.
306 ushl.4s v5, v4, v0 // v0 = {6, 0, 6, 0}, places second element of triplets into bits 16-25 and 80-89.
307 ushl.4s v4, v4, v1 // v1 = {0, -20, 0, -20}, places third element of triplets into bits 32-41 and 96-105.
308 and.16b v5, v5, v2 // v2 = {0, 1023, 0, 0, 0, 1023, 0, 0}, isolate second element of triplets.
309 and.16b v4, v4, v3 // v3 = {1023, 0, 1023, 0, 1023, 0, 1023, 0}, isolate first and third elements of triplets
310 orr.16b v4, v4, v5 // combine data
312 str d4, [rcx], #6 // write 3 low 10-bits
313 str d5, [rcx], #6 // write 3 low 10-bits
316 b.ge L_WK_unpack_2x_3_tenbits // repeat loop if LOW_BITS_AREA_END > next_word
321 L_WK_unpack_3_tenbits:
322 ldr s4, [r9] // read 32-bit words for 3 low 10-bits
323 zip1.4s v4, v4, v4 // bits 0-63 contain first triplet twice, bits 64-127 contain second triplet twice.
324 ushl.4s v5, v4, v0 // v0 = {6, 0, 6, 0}, places second element of triplets into bits 16-25 and 80-89.
325 ushl.4s v4, v4, v1 // v1 = {0, -20, 0, -20}, places third element of triplets into bits 32-41 and 96-105.
326 and.16b v5, v5, v2 // v2 = {0, 1023, 0, 0, 0, 1023, 0, 0}, isolate second element of triplets.
327 and.16b v4, v4, v3 // v3 = {1023, 0, 1023, 0, 1023, 0, 1023, 0}, isolate first and third elements of triplets
328 orr.16b v4, v4, v5 // combine data
330 str d4, [rcx] // write 3 low 10-bits
334 str d4, [rcx] // write 3 low 10-bits
337 str h4, [rcx] // write final 1 low 10-bits
342 set up before going to the main decompress loop
344 mov next_tag, scratch // tempTagsArray
345 add r8, scratch, #(1024*scale) // next_qpos
346 add r11, scratch, #(2048*scale) // tempLowBitsArray
347 adrp rbx, _hashLookupTable@GOTPAGE
348 mov tags_counter, #(1024*scale) // tag_area_end
349 ldr rbx, [rbx, _hashLookupTable@GOTPAGEOFF]
356 we can only get here if w9 = 0, meaning this is a zero tag
359 str w9, [dest_buf], #4 // *dest_buf++ = 0
360 subs tags_counter, tags_counter, #1 // next_tag vs tag_area_end
361 b.ls L_done // if next_tag >= tag_area_end, we're done
363 /* WKdm decompress main loop */
365 ldrb w9, [next_tag], #1 // new tag
367 cmp w9, #2 // partial match tag ?
373 this is a partial match:
374 dict_word = dictionary[*dict_index];
375 dictionary[*dict_index++] = *dest_buf++ = dict_word&0xfffffc00 | *LowBits++;
378 ldrb edx, [r8], #1 // qpos = *next_qpos++
379 ldrh ecx, [r11], #2 // lower 10-bits from *next_low_bits++
380 ldr eax, [dictionary, rdx, lsl #2] // read dictionary word
381 bfm eax, ecx, #0, #9 // pad the lower 10-bits from *next_low_bits
382 str eax, [dictionary,rdx,lsl #2] // *dict_location = newly formed word
383 str eax, [dest_buf], #4 // *dest_buf++ = newly formed word
384 subs tags_counter, tags_counter, #1 // next_tag vs tag_area_end
385 b.gt L_next // repeat loop until next_tag==tag_area_end
389 // release stack memory, restore registers, and return
390 add sp, sp, #64 // deallocate for dictionary
392 ld1.4s {v0,v1,v2},[sp],#48
393 ld1.4s {v3,v4,v5},[sp],#48
400 this is a dictionary miss.
404 dictionary[k] = *dest_buf++ = x;
406 ldr eax, [r10], #4 // w = *next_full_patt++
407 ubfm edx, eax, #10, #17 // 8-bit hash table index
408 str eax, [dest_buf], #4 // *dest_buf++ = word
409 ldrb edx, [rbx, rdx] // qpos
410 str eax, [dictionary,rdx] // dictionary[qpos] = word
411 subs tags_counter, tags_counter, #1 // next_tag vs tag_area_end
412 b.gt L_next // repeat the loop
413 b L_done // if next_tag >= tag_area_end, we're done
418 this is an exact match;
419 *dest_buf++ = dictionary[*dict_index++];
421 ldrb eax, [r8], #1 // qpos = *next_qpos++
422 ldr eax, [dictionary,rax,lsl #2] // w = dictionary[qpos]
423 str eax, [dest_buf], #4 // *dest_buf++ = w
424 subs tags_counter, tags_counter, #1 // next_tag vs tag_area_end
425 b.gt L_next // repeat the loop
426 b L_done // if next_tag >= tag_area_end, we're done