/src/ffmpeg/libavutil/crc_internal.h
Line | Count | Source |
1 | | /* |
2 | | * This file is part of FFmpeg. |
3 | | * |
4 | | * FFmpeg is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * FFmpeg is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | * Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with FFmpeg; if not, write to the Free Software |
16 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | | */ |
18 | | |
19 | | #ifndef AVUTIL_CRC_INTERNAL_H |
20 | | #define AVUTIL_CRC_INTERNAL_H |
21 | | |
22 | | #include <stdint.h> |
23 | | #include "libavutil/reverse.h" |
24 | | |
25 | | static uint64_t reverse(uint64_t p, unsigned int deg) |
26 | 0 | { |
27 | 0 | uint64_t ret = 0; |
28 | 0 | int i; |
29 | 0 | for (i = 0; i < (deg / 8); i += 1) { |
30 | 0 | ret = (ret << 8) | (ff_reverse[p & 0xff]); |
31 | 0 | p >>= 8; |
32 | 0 | } |
33 | 0 | int rem = (deg + 1) - 8 * i; |
34 | 0 | ret = (ret << rem) | (ff_reverse[p & 0xff] >> (8 - rem)); |
35 | 0 | return ret; |
36 | 0 | } |
37 | | |
38 | | static uint64_t xnmodp(unsigned n, uint64_t poly, unsigned deg, uint64_t *div, int bitreverse) |
39 | 0 | { |
40 | 0 | uint64_t mod, mask, high; |
41 | |
|
42 | 0 | if (n < deg) { |
43 | 0 | *div = 0; |
44 | 0 | return poly; |
45 | 0 | } |
46 | 0 | mask = ((uint64_t)1 << deg) - 1; |
47 | 0 | poly &= mask; |
48 | 0 | mod = poly; |
49 | 0 | *div = 1; |
50 | 0 | deg--; |
51 | 0 | while (--n > deg) { |
52 | 0 | high = (mod >> deg) & 1; |
53 | 0 | *div = (*div << 1) | high; |
54 | 0 | mod <<= 1; |
55 | 0 | if (high) |
56 | 0 | mod ^= poly; |
57 | 0 | } |
58 | 0 | uint64_t ret = mod & mask; |
59 | 0 | if (bitreverse) { |
60 | 0 | *div = reverse(*div, deg) << 1; |
61 | 0 | return reverse(ret, deg) << 1; |
62 | 0 | } |
63 | 0 | return ret; |
64 | 0 | } |
65 | | |
66 | | #endif /* AVUTIL_CRC_INTERNAL_H */ |