Skip to content

Commit

Permalink
Refine non-intuitive bitwise operations
Browse files Browse the repository at this point in the history
- Replace non-intuitive left shifts with multiplication
- Replace non-intuitive right shifts with division with unsigned divisor
   - include/config.h: macro `TAB_STOP` & util/poststat.c: macro `HASHSIZE`:
      Lift the restriction that each of these macros must be a power of 2.
- Replace non-intuitive bitwise `and`s with modulo with unsigned divisor
- maple/xover.c: `xo_thread()`: Lift the restriction that macro `RS_FORWARD` must be `2`.

A modern compiler will transform those operations into bitwise operations,
   and therefore it is a waste of time to do this by the programmers.
  • Loading branch information
IepIweidieng committed Feb 13, 2020
1 parent 22d3fe4 commit ede75ca
Show file tree
Hide file tree
Showing 43 changed files with 173 additions and 175 deletions.
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@

#define MOVIE_LINES (11) /* 動畫最多有 11 列 */

#define MENU_XPOS ((d_cols >> 1) + 23) /* 選單開始的 (y, x) 座標 */
#define MENU_XPOS (d_cols / 2U + 23) /* 選單開始的 (y, x) 座標 */
#define MENU_YPOS 13
#define MENU_LOAD 1
#define MENU_DRAW 2
Expand Down
3 changes: 1 addition & 2 deletions include/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@

#define T_LINES 50 /* maximum total lines */
#define T_COLS 120 /* maximum total columns,要比 ANSILINELEN 小 */
#define TAB_STOP 4 /* 按 TAB 換成幾格空白 (要是 2 的次方) */
#define TAB_WIDTH (TAB_STOP - 1)
#define TAB_STOP 4U /* 按 TAB 換成幾格空白 (建議是 2 的次方,可免去除法) */

#define SCR_WIDTH 80
/* #define VE_WIDTH (ANSILINELEN - 1) */
Expand Down
2 changes: 1 addition & 1 deletion innbbsd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ channelreader(
if (len < ReadSize + 3)
{
len += (used + ReadSize);
len += (len >> 3);
len += (len / 8U);

in->data = data = (char *) realloc(data, len);
len -= used;
Expand Down
4 changes: 2 additions & 2 deletions innbbsd/convcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ conv_init(void)
if (BtoG != NULL)
return;

BGsize = BtoG_count << 1; /* 每個漢字 2-byte */
GBsize = GtoB_count << 1;
BGsize = 2 * BtoG_count; /* 每個漢字 2-byte */
GBsize = 2 * GtoB_count;
BtoG = (unsigned char *) malloc(BGsize + GBsize);
GtoB = BtoG + BGsize;

Expand Down
4 changes: 2 additions & 2 deletions innbbsd/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void
str_ncpy(his.xname, xname, sizeof(his.xname));

/* 依 msgid 將 history 打散至 32 個檔案 */
sprintf(fpath, "innd/history/%02d", his.hash & 31);
sprintf(fpath, "innd/history/%02d", his.hash % 32U);
rec_add(fpath, &his, sizeof(HIS));
}

Expand All @@ -110,7 +110,7 @@ void

/* 依 msgid 找出在哪一份 history 檔案中 */
hash = str_hash(msgid, 1);
sprintf(fpath, "innd/history/%02d", hash & 31);
sprintf(fpath, "innd/history/%02d", hash % 32U);

/* 去該份 history 檔案中找看看有沒有 */
if ((fd = open(fpath, O_RDONLY)) >= 0)
Expand Down
8 changes: 4 additions & 4 deletions lib/archiv32.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ void archiv32(time_t chrono, /* 32 bits */
*str = '\0';
for (;;)
{
*(--str) = radix32[chrono & 31];
*(--str) = radix32[chrono % 32U];
if (str == fname)
return;
chrono >>= 5;
chrono /= 32U;
}
}

Expand All @@ -27,9 +27,9 @@ void archiv32m(time_t chrono, /* 32 bits */
*str = '\0';
for (;;)
{
*(--str) = radix32[chrono & 31];
*(--str) = radix32[chrono % 32U];
if (str == fname)
return;
chrono >>= 5;
chrono /= 32U;
}
}
2 changes: 1 addition & 1 deletion lib/chrono32.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GCC_PURE time_t chrono32(const char *str /* M0123456 */
ch -= '0';
if (ch >= 10)
ch -= 'A' - '0' - 10;
chrono = (chrono << 5) + ch;
chrono = (32 * chrono) + ch;
}
return chrono;
}
2 changes: 1 addition & 1 deletion lib/date_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ char *Ctime(const time_t * clock)

sprintf(datemsg, "%d年%2d月%2d日%3d:%02d:%02d 星期%.2s",
t->tm_year - 11, t->tm_mon + 1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec, &week[t->tm_wday << 1]);
t->tm_hour, t->tm_min, t->tm_sec, &week[2 * t->tm_wday]);
return (datemsg);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/header.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void hdr_fpath(char *fpath, const char *folder, const HDR * hdr)
folder = hdr->xname;
cc = *folder;
if (cc != '@')
cc = radix32[chrono & 31];
cc = radix32[chrono % 32U];

if (*str == '.')
{
Expand Down Expand Up @@ -98,7 +98,7 @@ int hdr_stamp(const char *folder, int token, HDR * hdr, char *fpath)

for (;;)
{
*family = radix32[chrono & 31];
*family = radix32[chrono % 32U];
archiv32(chrono, fname);

if (flink)
Expand Down
6 changes: 3 additions & 3 deletions lib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ GCC_PURE int str_hash2(const char *str, int seed)

while ((cc = *str++))
{
seed = (seed << 7) - seed + cc; /* 127 * seed + cc */
seed = 127 * seed + cc;
}
return (seed & 0x7fffffff);
}
Expand All @@ -643,7 +643,7 @@ GCC_PURE int str_hash(const char *str, int seed)

while ((cc = *str++))
{
seed = (seed << 5) - seed + cc; /* 31 * seed + cc */
seed = 31 * seed + cc;
}
return (seed & 0x7fffffff);
}
Expand Down Expand Up @@ -1326,7 +1326,7 @@ GCC_PURE int hash32(const char *str)
xo = 1048583; /* a big prime number */
while ((cc = *str++))
{
xo = (xo << 5) - xo + cc; /* 31 * xo + cc */
xo = 31 * xo + cc;
}
return (xo & 0x7fffffff);
}
2 changes: 1 addition & 1 deletion lib/xsort.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void xsort(void *a, size_t n, size_t es, int (*cmp) (const void *lhs, const void
pn = (char *)a + (n - 1) * es;
if (n > 40)
{
d = (n >> 3) * es;
d = (n / 8U) * es;
pl = med3(pl, pl + d, pl + d + d, cmp);
pm = med3(pm - d, pm, pm + d, cmp);
pn = med3(pn - 2 * d, pn - d, pn, cmp);
Expand Down
12 changes: 6 additions & 6 deletions maple/acct.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,19 @@ void x_file(int mode, /* M_XFILES / M_UFILES */
else
{
if (n < 21) /* statue.000703: 註解: 一個畫面只能 show 20 個資料 */
move_ansi(n + ((b_lines-21) >> 1), 2);
move_ansi(n + (b_lines-21)/2U, 2);
else
move_ansi(n + ((b_lines-21) >> 1) - 20, 2 + ((b_cols+1) >> 1));
move_ansi(n + (b_lines-21)/2U - 20, 2 + (b_cols+1)/2U);

prints("(\x1b[1;36m%2d\x1b[m) %s", n, desc);


if (mode == M_XFILES)
{
if (n < 21)
move_ansi(n + ((b_lines-21) >> 1), 24 + (d_cols >> 2)); /* Thor.980806: 註解: 印出檔名 */
move_ansi(n + (b_lines-21)/2U, 24 + d_cols/4U); /* Thor.980806: 註解: 印出檔名 */
else
move_ansi(n + ((b_lines-21) >> 1) - 20, 24 + (d_cols >> 2) + ((b_cols+1) >> 1));
move_ansi(n + (b_lines-21)/2U - 20, 24 + d_cols/4U + (b_cols+1)/2U);
outs(flist[n - 1] + 4); /* statue.000703: 註解: +4 去掉目錄 */
clrtoeol();
}
Expand Down Expand Up @@ -363,7 +363,7 @@ void bitmsg(const char *msg, const char *str, int level)
else
pbits ^= j;
}
move(5 + (i & 15), (i < 16 ? 0 : 40));
move(5 + (i % 16U), (i < 16 ? 0 : 40));
if (perms[i])
prints("%c %s %s", radix32[i], msg, perms[i]);
else
Expand Down Expand Up @@ -394,7 +394,7 @@ void bitmsg(const char *msg, const char *str, int level)
}

pbits ^= j;
move(5 + (i & 15), (i < 16 ? 2 : 42));
move(5 + (i % 16U), (i < 16 ? 2 : 42));
outs(msg);
}
}
Expand Down
2 changes: 1 addition & 1 deletion maple/bbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ tn_login(void)
level &= ~PERM_CHAT;

/*
if ((cuser.numemail >> 4) > (cuser.numlogins + cuser.numposts))
if ((cuser.numemail / 16U) > (cuser.numlogins + cuser.numposts))
level |= PERM_DENYMAIL;*/
}

Expand Down
2 changes: 1 addition & 1 deletion maple/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ brh_alloc(
if (size > brh_size)
{
/* size = (size & -BRH_PAGE) + BRH_PAGE; */
size += n >> 4; /* 多預約一些記憶體 */
size += n / 16U; /* 多預約一些記憶體 */
base = (int *) realloc((char *) base, size);

if (base == NULL)
Expand Down
10 changes: 5 additions & 5 deletions maple/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ observeshm_find(
{
for (count = oshm->total; count > 0;)
{
datum = cache[mid = count >> 1];
datum = cache[mid = count / 2U];
if (userno == datum)
return 1;
if (userno > datum)
Expand Down Expand Up @@ -769,7 +769,7 @@ out_rle(
else
getyx(&y, &SINKVAL(int));

move(y, d_cols >> 1/*item_length[count++]*/);
move(y, d_cols / 2U /*item_length[count++]*/);
while ((cc = (unsigned char) *str))
{
str++;
Expand All @@ -788,7 +788,7 @@ out_rle(
outs("\x1b[m");
clrtoeol();
}
move(++y, d_cols >> 1/*item_length[count++]*/);
move(++y, d_cols / 2U /*item_length[count++]*/);
}
else
outc(cc);
Expand Down Expand Up @@ -819,7 +819,7 @@ out_rle(
outs("\x1b[m");
clrtoeol();
}
move(++y, d_cols >> 1/*item_length[count++]*/);
move(++y, d_cols / 2U /*item_length[count++]*/);

}
else
Expand Down Expand Up @@ -861,7 +861,7 @@ film_out(

if (is_movie) /* random select */
{
tag += (time(0) & 7); /* 7 steps forward */
tag += (time(0) % 8U); /* 7 steps forward */
if (tag >= fmax)
tag = FILM_MOVIE;
} /* Thor.980804: 可能是故意的吧? 第一張 random select前八個其中一個 */
Expand Down
16 changes: 8 additions & 8 deletions maple/edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ ve_string(
do
{
ve_char(' ');
} while (ve_col & TAB_WIDTH);
} while (ve_col % TAB_STOP);
}
else if (ch == '\n')
{
Expand Down Expand Up @@ -692,7 +692,7 @@ ve_line(
{
*data++ = ' ';
len++;
} while ((len & TAB_WIDTH) && (len < VE_WIDTH));
} while ((len % TAB_STOP) && (len < VE_WIDTH));
}
else if (cc > 2 && cc < ' ' && cc != 27)
{
Expand Down Expand Up @@ -1190,7 +1190,7 @@ quote_check(void)
checkqt--;
#endif

if ((quot_line >> 2) <= post_line)
if ((quot_line / 4U) <= post_line)
return 0;

/* if (HAS_PERM(PERM_SYSOP))*/
Expand Down Expand Up @@ -1427,11 +1427,11 @@ ve_filer(
// else
// {
if (bbsmode != M_POST)
re = popupmenu_ans2(menu1, "存檔選項", (b_lines >> 1) - 7, (d_cols >> 1) + 20);
re = popupmenu_ans2(menu1, "存檔選項", b_lines/2U - 7, d_cols/2U + 20);
else if (curredit & EDIT_OUTGO)
re = popupmenu_ans2(menu2, "存檔選項", (b_lines >> 1) - 7, (d_cols >> 1) + 20);
re = popupmenu_ans2(menu2, "存檔選項", b_lines/2U - 7, d_cols/2U + 20);
else
re = popupmenu_ans2(menu3, "存檔選項", (b_lines >> 1) - 7, (d_cols >> 1) + 20);
re = popupmenu_ans2(menu3, "存檔選項", b_lines/2U - 7, d_cols/2U + 20);

// }

Expand Down Expand Up @@ -1863,7 +1863,7 @@ vedit(
do
{
ve_char(' ');
} while (ve_col & (TAB_STOP - 1));
} while (ve_col % TAB_STOP);
break;

case KEY_INS: /* Toggle insert/overwrite */
Expand Down Expand Up @@ -2190,7 +2190,7 @@ vedit(
NULL
};

switch (cc = popupmenu_ans2(menu, "控制碼選擇", (b_lines >> 1) - 4, (d_cols >> 1) + 20))
switch (cc = popupmenu_ans2(menu, "控制碼選擇", b_lines/2U - 4, d_cols/2U + 20))
{
case 'i':
ve_char(KEY_ESC);
Expand Down
2 changes: 1 addition & 1 deletion maple/gem.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ gbuf_malloc(
{
if (GemBufferSiz < num)
{
num += (num >> 1);
num += (num / 2U);
GemBufferSiz = num;
GemBuffer = gbuf = (HDR *) realloc(gbuf, sizeof(HDR) * num);
}
Expand Down
2 changes: 1 addition & 1 deletion maple/mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ mbox_attr(
if (type & MAIL_REPLIED)
return (type & MAIL_MARKED) ? 'R' : 'r';

return "+ Mm"[type & 3];
return "+ Mm"[type % 4U];
}


Expand Down
10 changes: 5 additions & 5 deletions maple/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ pad_draw(void)

sprintf(str, "\x1b[1;37;46m�W\x1b[34;47m %s \x1b[33m(%s)", cuser.userid, cuser.username);
len = strlen(str);
strcat(str, & " \x1b[30;46m"[len & 1]);
strcat(str, & " \x1b[30;46m"[len % 2U]);

for (i = len >> 1; i < 41; i++)
for (i = len / 2U; i < 41; i++)
strcat(str, "�e");
sprintf(str2, "\x1b[34;47m %.14s \x1b[37;46m�W\x1b[m\n%-70.70s\n%-70.70s\n%-70.70s\n",
Etime(&(pad.tpad)), buf[0], buf[1], buf[2]);
Expand Down Expand Up @@ -351,7 +351,7 @@ vs_mid(
}

spc = b_cols - len; /* spc: �����ٳѤU�h�����Ŷ� */
pad = spc >> 1; /* pad: Spaces needed to center `mid` */
pad = spc / 2U; /* pad: Spaces needed to center `mid` */

prints("%*s%s%*s\x1b[m\n", pad, "", mid, spc - pad, "");
}
Expand Down Expand Up @@ -395,7 +395,7 @@ vs_head(
spc = b_cols - 14 - len - strlen(currboard); /* spc: �����ٳѤU�h�����Ŷ� */
len_ttl = BMIN(len_ttl, spc); /* Truncate `title` if too long */
spc -= len_ttl; /* �\�� title �H��A�����٦� spc ��Ŷ� */
pad = BMAX(((b_cols - len) >> 1) - (len_ttl + 5), 0); /* pad: Spaces needed to center `mid` */
pad = BMAX((b_cols - len)/2U - (len_ttl + 5), 0U); /* pad: Spaces needed to center `mid` */

#ifdef COLOR_HEADER
prints("\x1b[1;%2d;37m�i%.*s�j%*s \x1b[33m%s\x1b[1;%2d;37m%*s \x1b[37m�ݪO�m%s�n\x1b[m\n",
Expand Down Expand Up @@ -469,7 +469,7 @@ movie(void)
ptime = localtime(&now);
sprintf(datemsg, "[%d/%d �P��%.2s ",
ptime->tm_mon + 1, ptime->tm_mday,
& "天一二三四五六"[ptime->tm_wday << 1]);
& "�Ѥ@�G�T�|����"[2 * ptime->tm_wday]);

uptime = now + 86400 - ptime->tm_hour * 3600 -
ptime->tm_min * 60 - ptime->tm_sec;
Expand Down
Loading

0 comments on commit ede75ca

Please sign in to comment.