HANXIN minor code changes (cm_row -> cm_i), avoid divide in in_numeric()

This commit is contained in:
gitlost 2019-12-08 20:22:10 +00:00
parent 889e786d95
commit 64c078605e
2 changed files with 23 additions and 21 deletions

View File

@ -158,7 +158,7 @@ static unsigned int gm_eod_cost(unsigned int state[], const int k) {
/* Calculate cost of encoding current character */ /* Calculate cost of encoding current character */
static void gm_cur_cost(unsigned int state[], const unsigned int gbdata[], const size_t length, const int i, char* char_modes, unsigned int prev_costs[], unsigned int cur_costs[]) { static void gm_cur_cost(unsigned int state[], const unsigned int gbdata[], const size_t length, const int i, char* char_modes, unsigned int prev_costs[], unsigned int cur_costs[]) {
int cm_row = i * GM_NUM_MODES; int cm_i = i * GM_NUM_MODES;
int double_byte, space, numeric, lower, upper, control, double_digit, eol; int double_byte, space, numeric, lower, upper, control, double_digit, eol;
unsigned int* p_numeral_end = &state[0]; unsigned int* p_numeral_end = &state[0];
unsigned int* p_numeral_cost = &state[1]; unsigned int* p_numeral_cost = &state[1];
@ -175,7 +175,7 @@ static void gm_cur_cost(unsigned int state[], const unsigned int gbdata[], const
/* Hanzi mode can encode anything */ /* Hanzi mode can encode anything */
cur_costs[GM_H] = prev_costs[GM_H] + (double_digit || eol ? 39 : 78); /* (6.5 : 13) * GM_MULT */ cur_costs[GM_H] = prev_costs[GM_H] + (double_digit || eol ? 39 : 78); /* (6.5 : 13) * GM_MULT */
char_modes[cm_row + GM_H] = GM_CHINESE; char_modes[cm_i + GM_H] = GM_CHINESE;
/* Byte mode can encode anything */ /* Byte mode can encode anything */
if (*p_byte_count == 512 || (double_byte && *p_byte_count == 511)) { if (*p_byte_count == 512 || (double_byte && *p_byte_count == 511)) {
@ -187,33 +187,33 @@ static void gm_cur_cost(unsigned int state[], const unsigned int gbdata[], const
*p_byte_count = 0; *p_byte_count = 0;
} }
cur_costs[GM_B] += prev_costs[GM_B] + (double_byte ? 96 : 48); /* (16 : 8) * GM_MULT */ cur_costs[GM_B] += prev_costs[GM_B] + (double_byte ? 96 : 48); /* (16 : 8) * GM_MULT */
char_modes[cm_row + GM_B] = GM_BYTE; char_modes[cm_i + GM_B] = GM_BYTE;
*p_byte_count += double_byte ? 2 : 1; *p_byte_count += double_byte ? 2 : 1;
if (in_numeral(gbdata, length, i, p_numeral_end, p_numeral_cost)) { if (in_numeral(gbdata, length, i, p_numeral_end, p_numeral_cost)) {
cur_costs[GM_N] = prev_costs[GM_N] + *p_numeral_cost; cur_costs[GM_N] = prev_costs[GM_N] + *p_numeral_cost;
char_modes[cm_row + GM_N] = GM_NUMBER; char_modes[cm_i + GM_N] = GM_NUMBER;
} }
if (control) { if (control) {
cur_costs[GM_L] = prev_costs[GM_L] + 78; /* (7 + 6) * GM_MULT */ cur_costs[GM_L] = prev_costs[GM_L] + 78; /* (7 + 6) * GM_MULT */
char_modes[cm_row + GM_L] = GM_LOWER; char_modes[cm_i + GM_L] = GM_LOWER;
cur_costs[GM_U] = prev_costs[GM_U] + 78; /* (7 + 6) * GM_MULT */ cur_costs[GM_U] = prev_costs[GM_U] + 78; /* (7 + 6) * GM_MULT */
char_modes[cm_row + GM_U] = GM_UPPER; char_modes[cm_i + GM_U] = GM_UPPER;
cur_costs[GM_M] = prev_costs[GM_M] + 96; /* (10 + 6) * GM_MULT */ cur_costs[GM_M] = prev_costs[GM_M] + 96; /* (10 + 6) * GM_MULT */
char_modes[cm_row + GM_M] = GM_MIXED; char_modes[cm_i + GM_M] = GM_MIXED;
} else { } else {
if (lower || space) { if (lower || space) {
cur_costs[GM_L] = prev_costs[GM_L] + 30; /* 5 * GM_MULT */ cur_costs[GM_L] = prev_costs[GM_L] + 30; /* 5 * GM_MULT */
char_modes[cm_row + GM_L] = GM_LOWER; char_modes[cm_i + GM_L] = GM_LOWER;
} }
if (upper || space) { if (upper || space) {
cur_costs[GM_U] = prev_costs[GM_U] + 30; /* 5 * GM_MULT */ cur_costs[GM_U] = prev_costs[GM_U] + 30; /* 5 * GM_MULT */
char_modes[cm_row + GM_U] = GM_UPPER; char_modes[cm_i + GM_U] = GM_UPPER;
} }
if (numeric || lower || upper || space) { if (numeric || lower || upper || space) {
cur_costs[GM_M] = prev_costs[GM_M] + 36; /* 6 * GM_MULT */ cur_costs[GM_M] = prev_costs[GM_M] + 36; /* 6 * GM_MULT */
char_modes[cm_row + GM_M] = GM_MIXED; char_modes[cm_i + GM_M] = GM_MIXED;
} }
} }
} }

View File

@ -299,7 +299,7 @@ static int lookup_text2(unsigned int input) {
/* Whether in numeric or not. If in numeric, *p_end is set to position after numeric, and *p_cost is set to per-numeric cost */ /* Whether in numeric or not. If in numeric, *p_end is set to position after numeric, and *p_cost is set to per-numeric cost */
static int in_numeric(const unsigned int gbdata[], const size_t length, const int posn, unsigned int* p_end, unsigned int* p_cost) { static int in_numeric(const unsigned int gbdata[], const size_t length, const int posn, unsigned int* p_end, unsigned int* p_cost) {
int i; int i, digit_cnt;
if (posn < *p_end) { if (posn < *p_end) {
return 1; return 1;
@ -308,12 +308,14 @@ static int in_numeric(const unsigned int gbdata[], const size_t length, const in
/* Attempt to calculate the average 'cost' of using numeric mode in number of bits (times HX_MULT) */ /* Attempt to calculate the average 'cost' of using numeric mode in number of bits (times HX_MULT) */
for (i = posn; i < length && i < posn + 4 && gbdata[i] >= '0' && gbdata[i] <= '9'; i++); for (i = posn; i < length && i < posn + 4 && gbdata[i] >= '0' && gbdata[i] <= '9'; i++);
if (i == posn) { digit_cnt = i - posn;
if (digit_cnt == 0) {
*p_end = 0; *p_end = 0;
return 0; return 0;
} }
*p_end = i; *p_end = i;
*p_cost = 60 / (i - posn); /* 60 == 10 * HX_MULT */ *p_cost = digit_cnt == 1 ? 60 /* 10 * HX_MULT */ : digit_cnt == 2 ? 30 /* (10 / 2) * HX_MULT */ : 20 /* (10 / 3) * HX_MULT */;
return 1; return 1;
} }
@ -382,7 +384,7 @@ static unsigned int hx_eod_cost(unsigned int state[], const int k) {
/* Calculate cost of encoding character */ /* Calculate cost of encoding character */
static void hx_cur_cost(unsigned int state[], const unsigned int gbdata[], const size_t length, const int i, char* char_modes, unsigned int prev_costs[], unsigned int cur_costs[]) { static void hx_cur_cost(unsigned int state[], const unsigned int gbdata[], const size_t length, const int i, char* char_modes, unsigned int prev_costs[], unsigned int cur_costs[]) {
int cm_row = i * HX_NUM_MODES; int cm_i = i * HX_NUM_MODES;
int text1, text2; int text1, text2;
unsigned int* p_numeric_end = &state[0]; unsigned int* p_numeric_end = &state[0];
unsigned int* p_numeric_cost = &state[1]; unsigned int* p_numeric_cost = &state[1];
@ -392,7 +394,7 @@ static void hx_cur_cost(unsigned int state[], const unsigned int gbdata[], const
if (in_numeric(gbdata, length, i, p_numeric_end, p_numeric_cost)) { if (in_numeric(gbdata, length, i, p_numeric_end, p_numeric_cost)) {
cur_costs[HX_N] = prev_costs[HX_N] + *p_numeric_cost; cur_costs[HX_N] = prev_costs[HX_N] + *p_numeric_cost;
char_modes[cm_row + HX_N] = 'n'; char_modes[cm_i + HX_N] = 'n';
} }
text1 = lookup_text1(gbdata[i]) != -1; text1 = lookup_text1(gbdata[i]) != -1;
@ -405,30 +407,30 @@ static void hx_cur_cost(unsigned int state[], const unsigned int gbdata[], const
} else { } else {
cur_costs[HX_T] = prev_costs[HX_T] + 36; /* 6 * HX_MULT */ cur_costs[HX_T] = prev_costs[HX_T] + 36; /* 6 * HX_MULT */
} }
char_modes[cm_row + HX_T] = 't'; char_modes[cm_i + HX_T] = 't';
} else { } else {
*p_text_submode = 1; *p_text_submode = 1;
} }
/* Binary mode can encode anything */ /* Binary mode can encode anything */
cur_costs[HX_B] = prev_costs[HX_B] + (gbdata[i] > 0xFF ? 96 : 48); /* (16 : 8) * HX_MULT */ cur_costs[HX_B] = prev_costs[HX_B] + (gbdata[i] > 0xFF ? 96 : 48); /* (16 : 8) * HX_MULT */
char_modes[cm_row + HX_B] = 'b'; char_modes[cm_i + HX_B] = 'b';
if (isRegion1(gbdata[i])) { if (isRegion1(gbdata[i])) {
cur_costs[HX_1] = prev_costs[HX_1] + 72; /* 12 * HX_MULT */ cur_costs[HX_1] = prev_costs[HX_1] + 72; /* 12 * HX_MULT */
char_modes[cm_row + HX_1] = '1'; char_modes[cm_i + HX_1] = '1';
} }
if (isRegion2(gbdata[i])) { if (isRegion2(gbdata[i])) {
cur_costs[HX_2] = prev_costs[HX_2] + 72; /* 12 * HX_MULT */ cur_costs[HX_2] = prev_costs[HX_2] + 72; /* 12 * HX_MULT */
char_modes[cm_row + HX_2] = '2'; char_modes[cm_i + HX_2] = '2';
} }
if (isDoubleByte(gbdata[i])) { if (isDoubleByte(gbdata[i])) {
cur_costs[HX_D] = prev_costs[HX_D] + 90; /* 15 * HX_MULT */ cur_costs[HX_D] = prev_costs[HX_D] + 90; /* 15 * HX_MULT */
char_modes[cm_row + HX_D] = 'd'; char_modes[cm_i + HX_D] = 'd';
} }
if (in_fourbyte(gbdata, length, i, p_fourbyte_end, p_fourbyte_cost)) { if (in_fourbyte(gbdata, length, i, p_fourbyte_end, p_fourbyte_cost)) {
cur_costs[HX_F] = prev_costs[HX_F] + *p_fourbyte_cost; cur_costs[HX_F] = prev_costs[HX_F] + *p_fourbyte_cost;
char_modes[cm_row + HX_F] = 'f'; char_modes[cm_i + HX_F] = 'f';
} }
} }