1; 7zCrcOpt.asm -- CRC32 calculation : optimized version 2; 2021-02-07 : Igor Pavlov : Public domain 3 4include 7zAsm.asm 5 6MY_ASM_START 7 8rD equ r2 9rN equ r7 10rT equ r5 11 12ifdef x64 13 num_VAR equ r8 14 table_VAR equ r9 15else 16 if (IS_CDECL gt 0) 17 crc_OFFS equ (REG_SIZE * 5) 18 data_OFFS equ (REG_SIZE + crc_OFFS) 19 size_OFFS equ (REG_SIZE + data_OFFS) 20 else 21 size_OFFS equ (REG_SIZE * 5) 22 endif 23 table_OFFS equ (REG_SIZE + size_OFFS) 24 num_VAR equ [r4 + size_OFFS] 25 table_VAR equ [r4 + table_OFFS] 26endif 27 28SRCDAT equ rD + rN * 1 + 4 * 29 30CRC macro op:req, dest:req, src:req, t:req 31 op dest, DWORD PTR [rT + src * 4 + 0400h * t] 32endm 33 34CRC_XOR macro dest:req, src:req, t:req 35 CRC xor, dest, src, t 36endm 37 38CRC_MOV macro dest:req, src:req, t:req 39 CRC mov, dest, src, t 40endm 41 42CRC1b macro 43 movzx x6, BYTE PTR [rD] 44 inc rD 45 movzx x3, x0_L 46 xor x6, x3 47 shr x0, 8 48 CRC xor, x0, r6, 0 49 dec rN 50endm 51 52MY_PROLOG macro crc_end:req 53 54 ifdef x64 55 if (IS_LINUX gt 0) 56 MY_PUSH_2_REGS 57 mov x0, REG_ABI_PARAM_0_x ; x0 = x7 58 mov rT, REG_ABI_PARAM_3 ; r5 = r1 59 mov rN, REG_ABI_PARAM_2 ; r7 = r2 60 mov rD, REG_ABI_PARAM_1 ; r2 = r6 61 else 62 MY_PUSH_4_REGS 63 mov x0, REG_ABI_PARAM_0_x ; x0 = x1 64 mov rT, REG_ABI_PARAM_3 ; r5 = r9 65 mov rN, REG_ABI_PARAM_2 ; r7 = r8 66 ; mov rD, REG_ABI_PARAM_1 ; r2 = r2 67 endif 68 else 69 MY_PUSH_4_REGS 70 if (IS_CDECL gt 0) 71 mov x0, [r4 + crc_OFFS] 72 mov rD, [r4 + data_OFFS] 73 else 74 mov x0, REG_ABI_PARAM_0_x 75 endif 76 mov rN, num_VAR 77 mov rT, table_VAR 78 endif 79 80 test rN, rN 81 jz crc_end 82 @@: 83 test rD, 7 84 jz @F 85 CRC1b 86 jnz @B 87 @@: 88 cmp rN, 16 89 jb crc_end 90 add rN, rD 91 mov num_VAR, rN 92 sub rN, 8 93 and rN, NOT 7 94 sub rD, rN 95 xor x0, [SRCDAT 0] 96endm 97 98MY_EPILOG macro crc_end:req 99 xor x0, [SRCDAT 0] 100 mov rD, rN 101 mov rN, num_VAR 102 sub rN, rD 103 crc_end: 104 test rN, rN 105 jz @F 106 CRC1b 107 jmp crc_end 108 @@: 109 if (IS_X64 gt 0) and (IS_LINUX gt 0) 110 MY_POP_2_REGS 111 else 112 MY_POP_4_REGS 113 endif 114endm 115 116MY_PROC CrcUpdateT8, 4 117 MY_PROLOG crc_end_8 118 mov x1, [SRCDAT 1] 119 align 16 120 main_loop_8: 121 mov x6, [SRCDAT 2] 122 movzx x3, x1_L 123 CRC_XOR x6, r3, 3 124 movzx x3, x1_H 125 CRC_XOR x6, r3, 2 126 shr x1, 16 127 movzx x3, x1_L 128 movzx x1, x1_H 129 CRC_XOR x6, r3, 1 130 movzx x3, x0_L 131 CRC_XOR x6, r1, 0 132 133 mov x1, [SRCDAT 3] 134 CRC_XOR x6, r3, 7 135 movzx x3, x0_H 136 shr x0, 16 137 CRC_XOR x6, r3, 6 138 movzx x3, x0_L 139 CRC_XOR x6, r3, 5 140 movzx x3, x0_H 141 CRC_MOV x0, r3, 4 142 xor x0, x6 143 add rD, 8 144 jnz main_loop_8 145 146 MY_EPILOG crc_end_8 147MY_ENDP 148 149MY_PROC CrcUpdateT4, 4 150 MY_PROLOG crc_end_4 151 align 16 152 main_loop_4: 153 movzx x1, x0_L 154 movzx x3, x0_H 155 shr x0, 16 156 movzx x6, x0_H 157 and x0, 0FFh 158 CRC_MOV x1, r1, 3 159 xor x1, [SRCDAT 1] 160 CRC_XOR x1, r3, 2 161 CRC_XOR x1, r6, 0 162 CRC_XOR x1, r0, 1 163 164 movzx x0, x1_L 165 movzx x3, x1_H 166 shr x1, 16 167 movzx x6, x1_H 168 and x1, 0FFh 169 CRC_MOV x0, r0, 3 170 xor x0, [SRCDAT 2] 171 CRC_XOR x0, r3, 2 172 CRC_XOR x0, r6, 0 173 CRC_XOR x0, r1, 1 174 add rD, 8 175 jnz main_loop_4 176 177 MY_EPILOG crc_end_4 178MY_ENDP 179 180end 181