summaryrefslogtreecommitdiff
path: root/neozip/arch/loongarch/crc32_la.c
blob: f1bd314e6592a4e5b2b9a039d8fd5ec81017ab7a (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
/* crc32_la.c - LoongArch version of crc32
 * Copyright (C) 2025 Vladislav Shchapov <vladislav@shchapov.ru>
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

#ifdef LOONGARCH_CRC

#include "zbuild.h"

#include <larchintrin.h>

Z_INTERNAL uint32_t crc32_loongarch64(uint32_t crc, const uint8_t *buf, size_t len) {
    uint32_t c = ~crc;

    if (UNLIKELY(len == 1)) {
        c = (uint32_t)__crc_w_b_w((char)(*buf), (int)c);
        c = ~c;
        return c;
    }

    uintptr_t align_diff = ALIGN_DIFF(buf, 8);
    if (align_diff) {
        if (len && (align_diff & 1)) {
            c = (uint32_t)__crc_w_b_w((char)(*buf++), (int)c);
            len--;
        }

        if (len >= 2 && (align_diff & 2)) {
            c = (uint32_t)__crc_w_h_w((short)*((uint16_t*)buf), (int)c);
            buf += 2;
            len -= 2;
        }

        if (len >= 4 && (align_diff & 4)) {
            c = (uint32_t)__crc_w_w_w((int)*((uint32_t*)buf), (int)c);
            len -= 4;
            buf += 4;
        }

    }

    while (len >= 8) {
        c = (uint32_t)__crc_w_d_w((long int)*((uint64_t*)buf), (int)c);
        len -= 8;
        buf += 8;
    }

    if (len & 4) {
        c = (uint32_t)__crc_w_w_w((int)*((uint32_t*)buf), (int)c);
        buf += 4;
    }

    if (len & 2) {
        c = (uint32_t)__crc_w_h_w((short)*((uint16_t*)buf), (int)c);
        buf += 2;
    }

    if (len & 1) {
        c = (uint32_t)__crc_w_b_w((char)(*buf), (int)c);
    }

    c = ~c;
    return c;
}

Z_INTERNAL uint32_t crc32_copy_loongarch64(uint32_t crc, uint8_t *dst, const uint8_t *src, size_t len) {
    crc = crc32_loongarch64(crc, src, len);
    memcpy(dst, src, len);
    return crc;
}
#endif