3232% IN THE SOFTWARE.
3333
3434% --BEGIN ENCODER ultracode--
35- % --REQUIRES preamble raiseerror processoptions parseinput setanycolor rendertext renmatrix--
35+ % --REQUIRES preamble raiseerror processoptions parseinput setanycolor fifocache rendertext renmatrix--
3636% --DESC: Ultracode
3737% --EXAM: Awesome colours!
3838% --EXOP: eclevel=EC2
@@ -44,6 +44,7 @@ true setglobal
4444dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
4545dup /processoptions dup /uk.co.terryburton.bwipp findresource put
4646dup /parseinput dup /uk.co.terryburton.bwipp findresource put
47+ dup /fifocache dup /uk.co.terryburton.bwipp findresource put
4748dup /renmatrix dup /uk.co.terryburton.bwipp findresource put
4849begin
4950
@@ -152,6 +153,63 @@ begin
152153 exch get /preload known {//ultracode.latevars /init get exec} if
153154} {pop} ifelse
154155
156+ %
157+ % Function to calculate the product in the field
158+ %
159+ /ultracode.rsprod {
160+ dup 0 ne { 1 index 0 ne {
161+ rslog exch get exch rslog exch get add 282 mod rsalog exch get
162+ } { pop pop 0 } ifelse } { pop pop 0 } ifelse
163+ } bind def
164+
165+ %
166+ % Generate Reed-Solomon coefficients for a given k
167+ %
168+ /ultracode.gencoeffs {
169+ /coeffs [ 1 k {0} repeat ] def
170+ 1 1 k { % i
171+ coeffs 1 index 2 copy 1 sub get put % coeffs[i] = coeffs[i-1]
172+ rsalog 1 index get % ai = rsalog[i]
173+ exch 1 sub -1 1 { % j
174+ coeffs exch 2 copy get % ... coeffs j coeffs[j]
175+ 3 index % ... ai
176+ //ultracode.rsprod exec % p = coeffs[j] * ai
177+ coeffs 2 index 1 sub get % ... coeffs[j-1]
178+ add 283 mod put % coeffs[j] = (p + coeffs[j-1]) mod 283
179+ } for
180+ coeffs 0 2 copy get % coeffs 0 coeffs[0]
181+ 3 index % ... ai
182+ //ultracode.rsprod exec put % coeffs[0] = coeffs[0] * ai
183+ pop % ai
184+ } for
185+ /coeffs coeffs 0 coeffs length 1 sub getinterval def
186+ coeffs length 1 sub -2 0 { coeffs exch 2 copy get 283 exch sub put } for
187+ coeffs
188+ } bind def
189+
190+ %
191+ % FIFO cache for ECC polynomials
192+ %
193+ % 37 unique keys: qcc=factor*ceil(mcc/25)+5 yields sparse values 3-101
194+ %
195+ /ultracode.coeffscachemax 37 def % Override with global_ctx.ultracode.coeffscachemax
196+ /ultracode.coeffscachelimit 1311 def % Override with global_ctx.ultracode.coeffscachelimit
197+ /uk.co.terryburton.bwipp.global_ctx dup where {
198+ exch get
199+ dup /ultracode.coeffscachemax 2 copy known {get /ultracode.coeffscachemax exch def} {pop pop} ifelse
200+ /ultracode.coeffscachelimit 2 copy known {get /ultracode.coeffscachelimit exch def} {pop pop} ifelse
201+ } {pop} ifelse
202+
203+ /ultracode.coeffscache ultracode.coeffscachemax ultracode.coeffscachelimit //fifocache exec def
204+
205+ /ultracode.coeffscachefetch {
206+ /k exch def
207+ k
208+ { //ultracode.gencoeffs exec }
209+ { length }
210+ //ultracode.coeffscache /fetch get exec
211+ } bind def
212+
155213/ultracode {
156214
157215 64 dict begin
@@ -290,34 +348,10 @@ begin
290348 ] def
291349
292350 %
293- % Function to calculate the product in the field
294- %
295- /rsprod {
296- dup 0 ne { 1 index 0 ne {
297- rslog exch get exch rslog exch get add 282 mod rsalog exch get
298- } { pop pop 0 } ifelse } { pop pop 0 } ifelse
299- } def
300-
301- %
302- % Generate the coefficients
351+ % Fetch the coefficients
303352 %
304353 /n mcc def /k qcc def
305- /coeffs [ 1 k {0} repeat ] def
306- 1 1 k { % i
307- coeffs 1 index 2 copy 1 sub get put % coeffs[i] = coeffs[i-1]
308- rsalog 1 index get % ai = rsalog[i]
309- exch 1 sub -1 1 { % j
310- coeffs exch 2 copy get % ... coeffs j coeffs[j]
311- 3 index % ... ai
312- rsprod % p = coeffs[j] * ai
313- coeffs 2 index 1 sub get % ... coeffs[j-1]
314- add 283 mod put % coeffs[j] = (p + coeffs[j-1]) mod 283
315- } for
316- coeffs 0 2 copy get 3 index rsprod put % coeffs[0] = coeffs[0] * ai
317- pop % ai
318- } for
319- /coeffs coeffs 0 coeffs length 1 sub getinterval def
320- coeffs length 1 sub -2 0 {coeffs exch 2 copy get 283 exch sub put} for
354+ /coeffs k //ultracode.coeffscachefetch exec def
321355
322356 %
323357 % Derive the error codewords
0 commit comments