Skip to content

Commit cf0d049

Browse files
committed
rsecbinary: Inline common rsprod; reverse coeff to simplify inner loop
1 parent 0423914 commit cf0d049

8 files changed

Lines changed: 56 additions & 25 deletions

File tree

CHANGES

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
XXXX-XX-XX
1+
2026-05-31
22

3-
* The PDF417, MicroPDF417 and Aztec Code encoders were amended to avoid quadratic-time sequence copying in their encoding optimisers.
3+
* The PDF417, MicroPDF417 and Aztec Code encoding was optimised.
4+
* Reed-Solomon error correction for the QR Code, Aztec Code, MaxiCode, Data Matrix, Code One and Han Xin Code symbologies was made more efficient.
45

56

67
2026-05-29

src/azteccode.ps.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ begin
934934
(ecc.coeffs) //azteccode.after exec
935935

936936
/lfsr rsnc array def
937-
{rscws exch get} rscws length coeffs rsnc lfsr /rsprod load //rsecbinary exec
937+
{rscws exch get} rscws length coeffs rsnc lfsr rslog rsalog rsgf-1 //rsecbinary exec
938938
lfsr
939939

940940
(ecc.cws) //azteccode.after exec

src/codeone.ps.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ begin
11061106
/blk exch def
11071107
ecbs blk
11081108
/lfsr ecpb array def
1109-
{cws exch rsbl mul blk add get} dcpb coeffs ecpb lfsr //codeone.rsprod //rsecbinary exec
1109+
{cws exch rsbl mul blk add get} dcpb coeffs ecpb lfsr rslog rsalog gf-1 //rsecbinary exec
11101110
lfsr
11111111
put
11121112
} for

src/datamatrix.ps.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ begin
10521052
} ifelse
10531053
ecbs blk
10541054
/lfsr ecpb array def
1055-
{cws exch rsbl mul blk add get} cwbsize coeffs ecpb lfsr //datamatrix.rsprod //rsecbinary exec
1055+
{cws exch rsbl mul blk add get} cwbsize coeffs ecpb lfsr rslog rsalog 255 //rsecbinary exec
10561056
lfsr
10571057
put
10581058
} for

src/hanxin.ps.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ begin
972972
%
973973
(ecc.cws) //hanxin.before exec
974974
/lfsr rsnc array def
975-
{rscws exch get} rscws length coeffs rsnc lfsr //hanxin.rsprod //rsecbinary exec
975+
{rscws exch get} rscws length coeffs rsnc lfsr rslog rsalog rsgf-1 //rsecbinary exec
976976
lfsr
977977
(ecc.cws) //hanxin.after exec
978978

src/maxicode.ps.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ begin
10811081
% Calculate the error correction codewords
10821082
%
10831083
/lfsr rsnum array def
1084-
{cwb exch get} cwb length coeffs rsnum lfsr //maxicode.rsprod //rsecbinary exec
1084+
{cwb exch get} cwb length coeffs rsnum lfsr rslog rsalog 63 //rsecbinary exec
10851085
lfsr
10861086

10871087
} def

src/qrcode.ps.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ begin
13521352
/rscodes {
13531353
/rscws exch def
13541354
/lfsr ecpb array def
1355-
{rscws exch get} rscws length coeffs ecpb lfsr //qrcode.rsprod //rsecbinary exec
1355+
{rscws exch get} rscws length coeffs ecpb lfsr rslog rsalog 255 //rsecbinary exec
13561356
lfsr
13571357
} def
13581358

src/rsecbinary.ps.src

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,65 @@ begin
4444
%
4545
% Runs a streaming LFSR over nd data codewords using the generator polynomial
4646
% coeffs. The caller-owned lfsr is the shift register; on return, its first
47-
% nc cells are the ECC codewords. Addition is XOR; multiplication is delegated
48-
% to the caller-supplied rsprod procedure (typically a log/alog table lookup).
47+
% nc cells are the ECC codewords. Addition is XOR; multiplication is inlined
48+
% via the caller-supplied log/alog tables. coeffs is reversed once and log(fb)
49+
% is hoisted out of the inner loop, so each iteration is a single get,
50+
% log-add-mod, alog get, xor and put.
4951
%
5052
/rsecbinary {
5153

52-
7 dict begin
54+
10 dict begin
5355

54-
/rsprod exch def % Implements GF multiplication.
56+
/gfm1 exch def % GF size minus one (modulus for log-sum)
57+
/rsalog exch def % Anti-log table
58+
/rslog exch def % Log table
5559
/lfsr exch def % Workspace of length at least nc
5660
/nc exch def % Number of ECC codewords to produce
5761
/coeffs exch def % Generator polynomial coefficients
5862
/nd exch def % Number of data codewords
5963
/getdata exch def % Implements fetching nth codeword
6064

61-
0 1 nc 1 sub {lfsr exch 0 put} for
65+
0 1 nc 1 sub {lfsr exch 0 put} for % Zero LFSR
66+
67+
%
68+
% Reverse coeffs to simplify the inner loop
69+
%
70+
/coeffsrev nc array def
71+
0 1 nc 1 sub {
72+
/j exch def
73+
coeffsrev j coeffs nc 1 sub j sub get put
74+
} for
6275

6376
0 1 nd 1 sub { % m
64-
getdata exec lfsr 0 get xor % fb = data[m] ^ S[0]
65-
0 1 nc 2 sub { % j
66-
lfsr exch dup 1 add % ... lfsr j j+1
67-
2 index exch get % ... lfsr j S[j+1]
68-
coeffs nc 3 index sub 1 sub get % ... coeffs[nc-1-j]
69-
4 index % ... fb
70-
rsprod exec % p = coeffs[nc-1-j] * fb
71-
xor put % S[j] = S[j+1] ^ p
72-
} for
73-
lfsr nc 1 sub coeffs 0 get 3 index % S[nc-1] = coeffs[0] * fb
74-
rsprod exec put
75-
pop % fb
77+
getdata exec lfsr 0 get xor % fb = data[m] ^ S[0]
78+
dup 0 ne { % fb != 0
79+
rslog exch get % log_fb (replaces fb on stack)
80+
0 1 nc 2 sub { % j
81+
lfsr exch dup 1 add % ... log_fb lfsr j j+1
82+
2 index exch get % ... log_fb lfsr j S[j+1]
83+
coeffsrev 2 index get % ... log_fb lfsr j S[j+1] coeffsrev[j]
84+
dup 0 ne {
85+
rslog exch get 4 index add gfm1 mod rsalog exch get
86+
} {
87+
pop 0
88+
} ifelse % ... log_fb lfsr j S[j+1] p
89+
xor put % S[j] = S[j+1] ^ p
90+
} for
91+
lfsr exch nc 1 sub exch % ... lfsr nc-1 log_fb
92+
coeffsrev nc 1 sub get % ... lfsr nc-1 log_fb coeffsrev[nc-1]
93+
dup 0 ne {
94+
rslog exch get exch add gfm1 mod rsalog exch get
95+
} {
96+
pop pop 0
97+
} ifelse
98+
put % S[nc-1] = coeffsrev[nc-1] * fb
99+
} { % fb == 0
100+
pop % drop fb
101+
0 1 nc 2 sub {
102+
lfsr exch dup 1 add 2 index exch get put
103+
} for
104+
lfsr nc 1 sub 0 put
105+
} ifelse
76106
} for
77107

78108
end

0 commit comments

Comments
 (0)