comparison mupdf-source/thirdparty/zxing-cpp/core/src/GenericGF.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 * Copyright 2016 Nu-book Inc.
3 * Copyright 2016 ZXing authors
4 */
5 // SPDX-License-Identifier: Apache-2.0
6
7 #pragma once
8
9 #include "GenericGFPoly.h"
10 #include "ZXConfig.h"
11
12 #include <stdexcept>
13 #include <vector>
14
15 namespace ZXing {
16
17 /**
18 * <p>This class contains utility methods for performing mathematical operations over
19 * the Galois Fields. Operations use a given primitive polynomial in calculations.</p>
20 *
21 * <p>Throughout this package, elements of the GF are represented as an {@code int}
22 * for convenience and speed (but at the cost of memory).
23 * </p>
24 *
25 * @author Sean Owen
26 * @author David Olivier
27 */
28 class GenericGF
29 {
30 const int _size;
31 int _generatorBase;
32 std::vector<short> _expTable;
33 std::vector<short> _logTable;
34
35 /**
36 * Create a representation of GF(size) using the given primitive polynomial.
37 *
38 * @param primitive irreducible polynomial whose coefficients are represented by
39 * the bits of an int, where the least-significant bit represents the constant
40 * coefficient
41 * @param size the size of the field (m = log2(size) is called the word size of the encoding)
42 * @param b the factor b in the generator polynomial can be 0- or 1-based
43 * (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
44 * In most cases it should be 1, but for QR code it is 0.
45 */
46 GenericGF(int primitive, int size, int b);
47
48 public:
49 static const GenericGF& AztecData12();
50 static const GenericGF& AztecData10();
51 static const GenericGF& AztecData6();
52 static const GenericGF& AztecParam();
53 static const GenericGF& QRCodeField256();
54 static const GenericGF& DataMatrixField256();
55 static const GenericGF& AztecData8();
56 static const GenericGF& MaxiCodeField64();
57
58 // note: replaced addOrSubstract calls with '^' / '^='. everyone trying to understand this code needs to look into
59 // Galois Fields with characteristic 2 and will then understand that XOR is addition/subtraction. And those
60 // operators are way more readable than a noisy member function name
61
62 /**
63 * @return 2 to the power of a in GF(size)
64 */
65 int exp(int a) const {
66 return _expTable.at(a);
67 }
68
69 /**
70 * @return base 2 log of a in GF(size)
71 */
72 int log(int a) const {
73 if (a == 0) {
74 throw std::invalid_argument("a == 0");
75 }
76 return _logTable.at(a);
77 }
78
79 /**
80 * @return multiplicative inverse of a
81 */
82 int inverse(int a) const {
83 return _expTable[_size - log(a) - 1];
84 }
85
86 /**
87 * @return product of a and b in GF(size)
88 */
89 int multiply(int a, int b) const noexcept {
90 if (a == 0 || b == 0)
91 return 0;
92
93 #ifdef ZX_REED_SOLOMON_USE_MORE_MEMORY_FOR_SPEED
94 return _expTable[_logTable[a] + _logTable[b]];
95 #else
96 auto fast_mod = [](const int input, const int ceil) {
97 // avoid using the '%' modulo operator => ReedSolomon computation is more than twice as fast
98 // see also https://stackoverflow.com/a/33333636/2088798
99 return input < ceil ? input : input - ceil;
100 };
101 return _expTable[fast_mod(_logTable[a] + _logTable[b], _size - 1)];
102 #endif
103 }
104
105 int size() const noexcept {
106 return _size;
107 }
108
109 int generatorBase() const noexcept {
110 return _generatorBase;
111 }
112 };
113
114 } // namespace ZXing