summaryrefslogtreecommitdiff
path: root/neozip/arch/arm/crc32_armv8_p.h
blob: e72c4c0ad160ccbf07d2198f245b95515d5ac87a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* crc32_armv8_p.h -- Private shared inline ARMv8 CRC32 functions
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

#ifndef CRC32_ARMV8_P_H
#define CRC32_ARMV8_P_H

#include "zbuild.h"
#include "acle_intrins.h"

Z_FORCEINLINE static Z_TARGET_CRC uint32_t crc32_armv8_align(uint32_t crc, uint8_t **dst, const uint8_t **buf,
                                                             size_t *len, uintptr_t align_diff, const int COPY) {
    if (*len && (align_diff & 1)) {
        uint8_t val = **buf;
        if (COPY) {
            **dst = val;
            *dst += 1;
        }
        crc = __crc32b(crc, val);
        *buf += 1;
        *len -= 1;
    }

    if (*len >= 2 && (align_diff & 2)) {
        uint16_t val = *((uint16_t*)*buf);
        if (COPY) {
            memcpy(*dst, &val, 2);
            *dst += 2;
        }
        crc = __crc32h(crc, val);
        *buf += 2;
        *len -= 2;
    }

    if (*len >= 4 && (align_diff & 4)) {
        uint32_t val = *((uint32_t*)*buf);
        if (COPY) {
            memcpy(*dst, &val, 4);
            *dst += 4;
        }
        crc = __crc32w(crc, val);
        *buf += 4;
        *len -= 4;
    }

    if (*len >= 8 && (align_diff & 8)) {
        uint64_t val = *((uint64_t*)*buf);
        if (COPY) {
            memcpy(*dst, &val, 8);
            *dst += 8;
        }
        crc = __crc32d(crc, val);
        *buf += 8;
        *len -= 8;
    }

    return crc;
}

Z_FORCEINLINE static Z_TARGET_CRC uint32_t crc32_armv8_tail(uint32_t crc, uint8_t *dst, const uint8_t *buf,
                                                            size_t len, const int COPY) {
    while (len >= 8) {
        uint64_t val = *((uint64_t*)buf);
        if (COPY) {
            memcpy(dst, &val, 8);
            dst += 8;
        }
        crc = __crc32d(crc, val);
        buf += 8;
        len -= 8;
    }

    if (len & 4) {
        uint32_t val = *((uint32_t*)buf);
        if (COPY) {
            memcpy(dst, &val, 4);
            dst += 4;
        }
        crc = __crc32w(crc, val);
        buf += 4;
    }

    if (len & 2) {
        uint16_t val = *((uint16_t*)buf);
        if (COPY) {
            memcpy(dst, &val, 2);
            dst += 2;
        }
        crc = __crc32h(crc, val);
        buf += 2;
    }

    if (len & 1) {
        uint8_t val = *buf;
        if (COPY)
            *dst = val;
        crc = __crc32b(crc, val);
    }

    return ~crc;
}

#endif /* CRC32_ARMV8_P_H */