From 8bb4d2ce8613cc8ab8a505437976876cfdb80373 Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Sun, 11 Feb 2018 08:00:32 +0000 Subject: [PATCH] Improved binary multiplication for large numbers --- backend/composite.c | 1 - backend/imail.c | 65 ++++++-------------------------------- backend/large.c | 20 ++++++++++++ backend/large.h | 1 + backend/mailmark.c | 77 ++++++--------------------------------------- backend/pdf417.c | 2 -- 6 files changed, 41 insertions(+), 125 deletions(-) diff --git a/backend/composite.c b/backend/composite.c index 09b52e62..93dd9e63 100644 --- a/backend/composite.c +++ b/backend/composite.c @@ -58,7 +58,6 @@ #include #endif #include "common.h" -#include "large.h" #include "composite.h" #include "pdf417.h" #include "gs1.h" diff --git a/backend/imail.c b/backend/imail.c index c3bd90c1..fe60215a 100644 --- a/backend/imail.c +++ b/backend/imail.c @@ -316,17 +316,8 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) { for (read = 0; read < strlen(zip); read++) { - for (i = 0; i < 112; i++) { - x_reg[i] = accum[i]; - } - - for (i = 0; i < 9; i++) { - binary_add(accum, x_reg); - } - - for (i = 0; i < 112; i++) { - x_reg[i] = 0; - } + binary_multiply(accum, "10"); + binary_load(x_reg, "0", 1); for (i = 0; i < 4; i++) { if (ctoi(zip[read]) & (0x01 << i)) x_reg[i] = 1; @@ -361,17 +352,8 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) { for (read = 0; read < strlen(zip_adder); read++) { - for (i = 0; i < 112; i++) { - y_reg[i] = accum[i]; - } - - for (i = 0; i < 9; i++) { - binary_add(accum, y_reg); - } - - for (i = 0; i < 112; i++) { - y_reg[i] = 0; - } + binary_multiply(accum, "10"); + binary_load(y_reg, "0", 1); for (i = 0; i < 4; i++) { if (ctoi(zip_adder[read]) & (0x01 << i)) y_reg[i] = 1; @@ -385,17 +367,8 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) { /* tracking code */ /* multiply by 10 */ - for (i = 0; i < 112; i++) { - y_reg[i] = accum[i]; - } - - for (i = 0; i < 9; i++) { - binary_add(accum, y_reg); - } - - for (i = 0; i < 112; i++) { - y_reg[i] = 0; - } + binary_multiply(accum, "10"); + binary_load(y_reg, "0", 1); /* add first digit of tracker */ for (i = 0; i < 4; i++) { @@ -405,17 +378,8 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) { binary_add(accum, y_reg); /* multiply by 5 */ - for (i = 0; i < 112; i++) { - y_reg[i] = accum[i]; - } - - for (i = 0; i < 4; i++) { - binary_add(accum, y_reg); - } - - for (i = 0; i < 112; i++) { - y_reg[i] = 0; - } + binary_multiply(accum, "5"); + binary_load(y_reg, "0", 1); /* add second digit */ for (i = 0; i < 4; i++) { @@ -428,17 +392,8 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) { for (read = 2; read < strlen(tracker); read++) { - for (i = 0; i < 112; i++) { - y_reg[i] = accum[i]; - } - - for (i = 0; i < 9; i++) { - binary_add(accum, y_reg); - } - - for (i = 0; i < 112; i++) { - y_reg[i] = 0; - } + binary_multiply(accum, "10"); + binary_load(y_reg, "0", 1); for (i = 0; i < 4; i++) { if (ctoi(tracker[read]) & (0x01 << i)) y_reg[i] = 1; diff --git a/backend/large.c b/backend/large.c index b7211a40..0f574e34 100644 --- a/backend/large.c +++ b/backend/large.c @@ -115,6 +115,26 @@ void binary_subtract(short int accumulator[], short int input_buffer[]) { binary_add(accumulator, sub_buffer); } +void binary_multiply(short int reg[], char data[]) { + /* Multiply the contents of reg[] by a number */ + short int temp[112] = {0}; + short int accum[112] = {0}; + int i; + + binary_load(temp, data, strlen(data)); + + for (i = 0; i < 102; i++) { + if (temp[i] == 1) { + binary_add(accum, reg); + } + shiftup(reg); + } + + for (i = 0; i < 112; i++) { + reg[i] = accum[i]; + } +} + void shiftdown(short int buffer[]) { int i; diff --git a/backend/large.h b/backend/large.h index 4a8e69be..01f89aa3 100644 --- a/backend/large.h +++ b/backend/large.h @@ -42,6 +42,7 @@ extern void binary_subtract(short int accumulator[], short int input_buffer[]); extern void shiftdown(short int buffer[]); extern void shiftup(short int buffer[]); extern short int islarger(short int accum[], short int reg[]); +extern void binary_multiply(short int reg[], char data[]); #ifdef __cplusplus } diff --git a/backend/mailmark.c b/backend/mailmark.c index e15414fc..269240a9 100644 --- a/backend/mailmark.c +++ b/backend/mailmark.c @@ -293,14 +293,7 @@ int mailmark(struct zint_symbol *symbol, unsigned char source[], int length) { for (i = 0; i < 9; i++) { switch (pattern[i]) { case 'F': - for (j = 0; j < 112; j++) { - temp[j] = b[j]; - } - - // b *= 26 - for (j = 1; j < 26; j++) { - binary_add(b, temp); - } + binary_multiply(b, "26"); binary_load(temp, "0", 1); for (j = 0; j < 5; j++) { @@ -310,14 +303,7 @@ int mailmark(struct zint_symbol *symbol, unsigned char source[], int length) { binary_add(b, temp); break; case 'L': - for (j = 0; j < 112; j++) { - temp[j] = b[j]; - } - - // b *= 20 - for (j = 1; j < 20; j++) { - binary_add(b, temp); - } + binary_multiply(b, "20"); binary_load(temp, "0", 1); for (j = 0; j < 5; j++) { @@ -327,14 +313,7 @@ int mailmark(struct zint_symbol *symbol, unsigned char source[], int length) { binary_add(b, temp); break; case 'N': - for (j = 0; j < 112; j++) { - temp[j] = b[j]; - } - - for (j = 1; j < 10; j++) { - // b *= 10 - binary_add(b, temp); - } + binary_multiply(b, "10"); binary_load(temp, "0", 1); for (j = 0; j < 4; j++) { @@ -390,14 +369,7 @@ int mailmark(struct zint_symbol *symbol, unsigned char source[], int length) { binary_add(cdv, destination_postcode); // Multiply by 100,000,000 - for (i = 0; i < 8; i++) { - binary_load(temp, "0", 1); - binary_add(temp, cdv); - - for (j = 1; j < 10; j++) { - binary_add(cdv, temp); - } - } + binary_multiply(cdv, "100000000"); // Add Item ID binary_load(temp, "0", 1); @@ -406,26 +378,12 @@ int mailmark(struct zint_symbol *symbol, unsigned char source[], int length) { } binary_add(cdv, temp); - if (length == 22) { + if (length == 22) { // Barcode C - Multiply by 100 - for (i = 0; i < 2; i++) { - binary_load(temp, "0", 1); - binary_add(temp, cdv); - - for (j = 1; j < 10; j++) { - binary_add(cdv, temp); - } - } + binary_multiply(cdv, "100"); } else { // Barcode L - Multiply by 1,000,000 - for (i = 0; i < 6; i++) { - binary_load(temp, "0", 1); - binary_add(temp, cdv); - - for (j = 1; j < 10; j++) { - binary_add(cdv, temp); - } - } + binary_multiply(cdv, "1000000"); } // Add Supply Chain ID @@ -436,12 +394,7 @@ int mailmark(struct zint_symbol *symbol, unsigned char source[], int length) { binary_add(cdv, temp); // Multiply by 15 - binary_load(temp, "0", 1); - binary_add(temp, cdv); - - for (j = 1; j < 15; j++) { - binary_add(cdv, temp); - } + binary_multiply(cdv, "15"); // Add Class binary_load(temp, "0", 1); @@ -451,12 +404,7 @@ int mailmark(struct zint_symbol *symbol, unsigned char source[], int length) { binary_add(cdv, temp); // Multiply by 5 - binary_load(temp, "0", 1); - binary_add(temp, cdv); - - for (j = 1; j < 5; j++) { - binary_add(cdv, temp); - } + binary_multiply(cdv, "5"); // Add Format binary_load(temp, "0", 1); @@ -466,12 +414,7 @@ int mailmark(struct zint_symbol *symbol, unsigned char source[], int length) { binary_add(cdv, temp); // Multiply by 4 - binary_load(temp, "0", 1); - binary_add(temp, cdv); - - for (j = 1; j < 4; j++) { - binary_add(cdv, temp); - } + binary_multiply(cdv, "4"); // Add Version ID binary_load(temp, "0", 1); diff --git a/backend/pdf417.c b/backend/pdf417.c index 62eb4cd6..7170503d 100644 --- a/backend/pdf417.c +++ b/backend/pdf417.c @@ -53,8 +53,6 @@ #endif #include "pdf417.h" #include "common.h" -#include "large.h" - /* Three figure numbers in comments give the location of command equivalents in the original Visual Basic source code file pdf417.frm