Skip to content

Commit a787b6c

Browse files
committed
codeone: Coefficients via fifocache
1 parent fff76a1 commit a787b6c

1 file changed

Lines changed: 66 additions & 33 deletions

File tree

src/codeone.ps.src

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
% IN THE SOFTWARE.
3333

3434
% --BEGIN ENCODER codeone--
35-
% --REQUIRES preamble raiseerror processoptions parseinput setanycolor rendertext renmatrix--
35+
% --REQUIRES preamble raiseerror processoptions parseinput setanycolor fifocache rendertext renmatrix--
3636
% --DESC: Code One
3737
% --EXAM: Code One
3838
% --EXOP:
@@ -44,6 +44,7 @@ true setglobal
4444
dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
4545
dup /processoptions dup /uk.co.terryburton.bwipp findresource put
4646
dup /parseinput dup /uk.co.terryburton.bwipp findresource put
47+
dup /fifocache dup /uk.co.terryburton.bwipp findresource put
4748
dup /renmatrix dup /uk.co.terryburton.bwipp findresource put
4849
begin
4950

@@ -303,6 +304,67 @@ begin
303304
exch get /preload known {//codeone.latevars /init get exec} if
304305
} {pop} ifelse
305306

307+
%
308+
% Function to calculate the product in the field
309+
%
310+
/codeone.rsprod {
311+
dup 0 ne { 1 index 0 ne {
312+
rslog exch get exch rslog exch get add gf-1 mod rsalog exch get
313+
} { pop pop 0 } ifelse } { pop pop 0 } ifelse
314+
} bind def
315+
316+
%
317+
% Generate Reed-Solomon coefficients for given ecpb
318+
%
319+
/codeone.gencoeffs {
320+
/coeffs [ 1 ecpb {0} repeat ] def
321+
0 1 ecpb 1 sub { % i
322+
rsalog 1 index get % ai = rsalog[i]
323+
exch dup 1 add % ai i i+1
324+
coeffs exch 2 copy 1 sub get put % ai i ; coeffs[i+1] = coeffs[i]
325+
-1 1 { % j
326+
coeffs exch 2 copy get % ... coeffs j coeffs[j]
327+
3 index % ... ai
328+
//codeone.rsprod exec % p = coeffs[j] * ai
329+
coeffs 2 index 1 sub get % ... coeffs[j-1]
330+
xor put % coeffs[j] = p ^ coeffs[j-1]
331+
} for
332+
coeffs 0 2 copy get % ... coeffs 0 coeffs[0]
333+
3 index % ... ai
334+
//codeone.rsprod exec put % coeffs[0] = coeffs[0] * ai
335+
pop % ai
336+
} for
337+
coeffs 0 coeffs length 1 sub getinterval
338+
} bind def
339+
340+
%
341+
% FIFO cache for ECC polynomials
342+
%
343+
% 9 unique keys: S-type GF31 (4,8,12) + standard GF128 (10,16,22,26,44,70)
344+
%
345+
/codeone.coeffscachemax 9 def % Override with global_ctx.codeone.coeffscachemax
346+
/codeone.coeffscachelimit 212 def % Override with global_ctx.codeone.coeffscachelimit
347+
/uk.co.terryburton.bwipp.global_ctx dup where {
348+
exch get
349+
dup /codeone.coeffscachemax 2 copy known {get /codeone.coeffscachemax exch def} {pop pop} ifelse
350+
/codeone.coeffscachelimit 2 copy known {get /codeone.coeffscachelimit exch def} {pop pop} ifelse
351+
} {pop} ifelse
352+
353+
/codeone.coeffscache codeone.coeffscachemax codeone.coeffscachelimit //fifocache exec def
354+
355+
/codeone.coeffscachefetch {
356+
/rsbt exch def
357+
/ecpb exch def
358+
/rslog rstables rsbt get 0 get def
359+
/rsalog rstables rsbt get 1 get def
360+
/gf rsalog length def
361+
/gf-1 gf 1 sub def
362+
rsbt 10000 mul ecpb add % key = rsbt * 10000 + ecpb
363+
{ //codeone.gencoeffs exec }
364+
{ length }
365+
//codeone.coeffscache /fetch get exec
366+
} bind def
367+
306368
/codeone {
307369

308370
128 dict begin
@@ -934,39 +996,10 @@ begin
934996
} for
935997

936998
%
937-
% Get log and anti-log tables from pre-calculated tables
999+
% Fetch the coefficients
9381000
%
9391001
/rsbt stype {5} {8} ifelse def
940-
/rslog rstables rsbt get 0 get def
941-
/rsalog rstables rsbt get 1 get def
942-
/gf rsalog length def
943-
/gf-1 gf 1 sub def
944-
945-
/rsprod {
946-
dup 0 ne { 1 index 0 ne {
947-
rslog exch get exch rslog exch get add gf-1 mod rsalog exch get
948-
} { pop pop 0 } ifelse } { pop pop 0 } ifelse
949-
} def
950-
951-
%
952-
% Generate the coefficients for the Reed-Solomon algorithm
953-
%
954-
/coeffs [ 1 ecpb {0} repeat ] def
955-
0 1 ecpb 1 sub { % i
956-
rsalog 1 index get % ai = rsalog[i]
957-
exch dup 1 add % ai i i+1
958-
coeffs exch 2 copy 1 sub get put % ai i ; coeffs[i+1] = coeffs[i]
959-
-1 1 { % j
960-
coeffs exch 2 copy get % ... coeffs j coeffs[j]
961-
3 index % ... ai
962-
rsprod % p = coeffs[j] * ai
963-
coeffs 2 index 1 sub get % ... coeffs[j-1]
964-
xor put % coeffs[j] = p ^ coeffs[j-1]
965-
} for
966-
coeffs 0 2 copy get 3 index rsprod put % coeffs[0] = coeffs[0] * ai
967-
pop % ai
968-
} for
969-
/coeffs coeffs 0 coeffs length 1 sub getinterval def
1002+
/coeffs ecpb rsbt //codeone.coeffscachefetch exec def
9701003

9711004
%
9721005
% Reed-Solomon algorithm to derive the error correction codewords
@@ -980,7 +1013,7 @@ begin
9801013
rscws 3 index 2 index add 1 add 2 copy get % ... rscws m+j+1 rscws[m+j+1]
9811014
coeffs ecpb 6 -1 roll sub 1 sub get % ... coeffs[ecpb-j-1]
9821015
4 index % ... k
983-
rsprod % p = coeffs[ecpb-j-1] * k
1016+
//codeone.rsprod exec % p = coeffs[ecpb-j-1] * k
9841017
xor put % rscws[m+j+1] = rscws[m+j+1] ^ p
9851018
} for
9861019
pop % k

0 commit comments

Comments
 (0)