Skip to content

Commit

Permalink
Avoid unsigned division and refine unsigned modulo
Browse files Browse the repository at this point in the history
- Replace unsigned division with normal right shift
- Replace right shift with unsigned division unless there is a corresponding right shift

This partially reverts ede75ca.
  • Loading branch information
IepIweidieng committed Oct 4, 2020
1 parent 34460e8 commit 39902b5
Show file tree
Hide file tree
Showing 28 changed files with 127 additions and 127 deletions.
4 changes: 2 additions & 2 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,9 @@
#define MOVIE_LINES (11) /* 動畫最多有 11 列 */

#define MENU_NOMOVIE_POS(y, x) (y < (MOVIE_LINES + 2)) /* Suppress movie display when menu starts at (x, y) */
#define MENU_XPOS (d_cols / 2U + 23) /* 選單開始的 (y, x) 座標 */
#define MENU_XPOS ((d_cols >> 1) + 23) /* 選單開始的 (y, x) 座標 */
#define MENU_YPOS 13
#define MENU_XPOS_REF (D_COLS_REF / 2U + 23) /* For `get[y|x]_ref()` & `move_ref()` */
#define MENU_XPOS_REF ((D_COLS_REF >> 1) + 23) /* For `get[y|x]_ref()` & `move_ref()` */
#define MENU_YPOS_REF 13
#define MENU_LOAD 1
#define MENU_DRAW 2
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 / 8U);
len += (len >> 3);

in->data = data = (char *) realloc(data, len);
len -= used;
Expand Down
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 / 8U) * es;
d = (n >> 3) * 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
6 changes: 3 additions & 3 deletions maple/acct.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void x_file(int mode, /* M_XFILES / M_UFILES */
if (mode == M_UFILES)
domenu(list, MENU_YPOS_REF, MENU_XPOS_REF, 0, 0, 1);
else
domenu(list, (B_LINES_REF-19)/2U, 0, 20, T_COLS_REF/2U, 2);
domenu(list, ((B_LINES_REF-19) >> 1), 0, 20, T_COLS_REF >> 1, 2);

for (MENU *mptr = list; mptr->item.func; mptr++)
free((char *)mptr->desc);
Expand Down Expand Up @@ -347,7 +347,7 @@ void bitmsg(const char *msg, const char *str, int level)
else
pbits ^= j;
}
move(5 + (i % 16U), (i < 16 ? 0 : (b_cols+1) / 2U));
move(5 + (i % 16U), (i < 16 ? 0 : (b_cols+1) >> 1));
if (perms[i])
prints("%c %s %s", radix32[i], msg, perms[i]);
else
Expand Down Expand Up @@ -378,7 +378,7 @@ void bitmsg(const char *msg, const char *str, int level)
}

pbits ^= j;
move(5 + (i % 16U), (i < 16 ? 0 : (b_cols+1) / 2U) + 2);
move(5 + (i % 16U), (i < 16 ? 0 : (b_cols+1) >> 1) + 2);
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 @@ -859,7 +859,7 @@ tn_login(void)
level &= ~PERM_CHAT;

/*
if ((cuser.numemail / 16U) > (cuser.numlogins + cuser.numposts))
if ((cuser.numemail >> 4) > (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 @@ -38,7 +38,7 @@ brh_alloc(
if (size > brh_size)
{
/* size = (size & -BRH_PAGE) + BRH_PAGE; */
size += n / 16U; /* 多預約一些記憶體 */
size += n >> 4; /* 多預約一些記憶體 */
base = (int *) realloc((char *) base, size);

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

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

}
else
Expand Down
10 changes: 5 additions & 5 deletions maple/edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ quote_check(void)
checkqt--;
#endif

if ((quot_line / 4U) <= post_line)
if ((quot_line >> 2) <= 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_REF/2U - 7, D_COLS_REF/2U + 20);
re = popupmenu_ans2(menu1, "存檔選項", (B_LINES_REF >> 1) - 7, (D_COLS_REF >> 1) + 20);
else if (curredit & EDIT_OUTGO)
re = popupmenu_ans2(menu2, "存檔選項", B_LINES_REF/2U - 7, D_COLS_REF/2U + 20);
re = popupmenu_ans2(menu2, "存檔選項", (B_LINES_REF >> 1) - 7, (D_COLS_REF >> 1) + 20);
else
re = popupmenu_ans2(menu3, "存檔選項", B_LINES_REF/2U - 7, D_COLS_REF/2U + 20);
re = popupmenu_ans2(menu3, "存檔選項", (B_LINES_REF >> 1) - 7, (D_COLS_REF >> 1) + 20);

// }

Expand Down Expand Up @@ -2190,7 +2190,7 @@ vedit(
NULL
};

switch (cc = popupmenu_ans2(menu, "控制碼選擇", B_LINES_REF/2U - 4, D_COLS_REF/2U + 20))
switch (cc = popupmenu_ans2(menu, "控制碼選擇", (B_LINES_REF >> 1) - 4, (D_COLS_REF >> 1) + 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 @@ -714,7 +714,7 @@ gbuf_malloc(
{
if (GemBufferSiz < num)
{
num += (num / 2U);
num += (num >> 1);
GemBufferSiz = num;
GemBuffer = gbuf = (HDR *) realloc(gbuf, sizeof(HDR) * num);
}
Expand Down
6 changes: 3 additions & 3 deletions maple/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pad_draw(void)
len = strlen(str);
strcat(str, & " \x1b[30;46m"[len % 2U]);

for (i = len / 2U; i < 41; i++)
for (i = len >> 1; i < 41; i++)
strcat(str, "▄");
sprintf(str2, "\x1b[34;47m %.14s \x1b[37;46mΥ\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 @@ -274,7 +274,7 @@ vs_mid(
}

spc = b_cols - len; /* spc: 中間還剩下多長的空間 */
pad = spc / 2U; /* pad: Spaces needed to center `mid` */
pad = spc >> 1; /* pad: Spaces needed to center `mid` */

prints("%*s%s%*s\x1b[m\n", pad, "", mid, spc - pad, "");
}
Expand Down Expand Up @@ -318,7 +318,7 @@ vs_head(
spc = b_cols - 14 - len - strlen(currboard); /* spc: 中間還剩下多長的空間 */
len_ttl = BMIN(len_ttl, spc); /* Truncate `title` if too long */
spc -= len_ttl; /* 擺完 title 以後,中間還有 spc 格空間 */
pad = BMAX((b_cols - len)/2U - (len_ttl + 5), 0U); /* pad: Spaces needed to center `mid` */
pad = BMAX(((b_cols - len) >> 1) - (len_ttl + 5), 0); /* pad: Spaces needed to center `mid` */
pad = BMAX(pad, 0);

#ifdef COLOR_HEADER
Expand Down
8 changes: 4 additions & 4 deletions maple/more.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ more(

lino++;

if ((lino % 32U == 0) && ((i = lino / 32U) < MAXBLOCK))
if ((lino & 31 == 0) && ((i = lino >> 5) < MAXBLOCK))
block[i] = foff - fimage;


Expand Down Expand Up @@ -762,12 +762,12 @@ more(
while (more_line(buf))
{
totallino++;
if ((totallino % 32U == 0) && ((i = totallino / 32U) < MAXBLOCK))
if ((totallino & 31 == 0) && ((i = totallino >> 5) < MAXBLOCK))
block[i] = foff - fimage;
}

/* 先位移到上一個 block 的尾端 */
i = BMIN((totallino - b_lines) / 32U, MAXBLOCK - 1);
i = BMIN((totallino - b_lines) >> 5, MAXBLOCK - 1);
foff = fimage + block[i];
i = i * 32;

Expand Down Expand Up @@ -801,7 +801,7 @@ more(
*/

/* 先位移到上一個 block 的尾端 */
i = BMIN((lino - b_lines) / 32U, MAXBLOCK - 1);
i = BMIN((lino - b_lines) >> 5, MAXBLOCK - 1);
foff = fimage + block[i];
i = i * 32;

Expand Down
24 changes: 12 additions & 12 deletions maple/popupmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,34 +635,34 @@ void pmsg_body(const char *msg)
{
pcopy(patten, "ˍ", plen);
sprintf(buf, " \x1b[0;40;37mˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍ%s\x1b[m ", plen?patten:"");
vs_line(buf, b_lines/2U - 4, d_cols/2U + 18-plen);
vs_line(buf, (b_lines >> 1) - 4, (d_cols >> 1) + 18-plen);
pcopy(patten, " ", plen);
sprintf(buf, " \x1b[0;37;44m▏ 請按任意鍵繼續 ..... %s\x1b[0;47;34m▉\x1b[m ", plen?patten:"");
vs_line(buf, b_lines/2U - 3, d_cols/2U + 18-plen);
vs_line(buf, (b_lines >> 1) - 3, (d_cols >> 1) + 18-plen);
sprintf(buf, " \x1b[0;37m▏ %s\x1b[0;47;30m▉\x1b[m\x1b[40;30;1m▉\x1b[m ", plen?patten:"");
vs_line(buf, b_lines/2U - 2, d_cols/2U + 18-plen);
vs_line(buf, (b_lines >> 1) - 2, (d_cols >> 1) + 18-plen);
sprintf(buf, " \x1b[0;37m▏%-30s%s\x1b[0;47;30m▉\x1b[m\x1b[40;30;1m▉\x1b[m ", msg, ((len > 30 && len%2 == 1) ? " " : ""));
vs_line(buf, b_lines/2U - 1, d_cols/2U + 18-plen);
vs_line(buf, (b_lines >> 1) - 1, (d_cols >> 1) + 18-plen);
sprintf(buf, " \x1b[0;37m▏ %s\x1b[0;47;30m▉\x1b[m\x1b[40;30;1m▉\x1b[m ", plen?patten:"");
vs_line(buf, b_lines/2U + 0, d_cols/2U + 18-plen);
vs_line(buf, (b_lines >> 1) + 0, (d_cols >> 1) + 18-plen);
pcopy(patten, "▇", plen);
sprintf(buf, " \x1b[0;47;30m▇\x1b[30;1m▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇%s\x1b[40;30;1m▉\x1b[m ", plen?patten:"");
vs_line(buf, b_lines/2U + 1, d_cols/2U + 18-plen);
vs_line(buf, (b_lines >> 1) + 1, (d_cols >> 1) + 18-plen);
}
else
{
sprintf(buf, " \x1b[0;40;37mˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍ\x1b[m ");
vs_line(buf, b_lines/2U - 4, d_cols/2U + 18);
vs_line(buf, (b_lines >> 1) - 4, (d_cols >> 1) + 18);
sprintf(buf, " \x1b[0;37;44m▏ 請按任意鍵繼續 ..... \x1b[0;47;34m▉\x1b[m ");
vs_line(buf, b_lines/2U - 3, d_cols/2U + 18);
vs_line(buf, (b_lines >> 1) - 3, (d_cols >> 1) + 18);
sprintf(buf, " \x1b[0;37m▏ \x1b[0;47;30m▉\x1b[m\x1b[40;30;1m▉\x1b[m ");
vs_line(buf, b_lines/2U - 2, d_cols/2U + 18);
vs_line(buf, (b_lines >> 1) - 2, (d_cols >> 1) + 18);
sprintf(buf, " \x1b[0;37m▏%-30s \x1b[0;47;30m▉\x1b[m\x1b[40;30;1m▉\x1b[m ", "[請按任意鍵繼續]");
vs_line(buf, b_lines/2U - 1, d_cols/2U + 18);
vs_line(buf, (b_lines >> 1) - 1, (d_cols >> 1) + 18);
sprintf(buf, " \x1b[0;37m▏ \x1b[0;47;30m▉\x1b[m\x1b[40;30;1m▉\x1b[m ");
vs_line(buf, b_lines/2U + 0, d_cols/2U + 18);
vs_line(buf, (b_lines >> 1) + 0, (d_cols >> 1) + 18);
sprintf(buf, " \x1b[0;47;30m▇\x1b[30;1m▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇\x1b[40;30;1m▉\x1b[m ");
vs_line(buf, b_lines/2U + 1, d_cols/2U + 18);
vs_line(buf, (b_lines >> 1) + 1, (d_cols >> 1) + 18);
}
}

Expand Down
4 changes: 2 additions & 2 deletions maple/post.c
Original file line number Diff line number Diff line change
Expand Up @@ -3930,7 +3930,7 @@ post_manage(

grayout(0, b_lines, GRAYOUT_DARK);

switch (re = popupmenu_ans2(menu, "板主管理", B_LINES_REF/2U - 8, D_COLS_REF/2U + 20))
switch (re = popupmenu_ans2(menu, "板主管理", (B_LINES_REF >> 1) - 8, (D_COLS_REF >> 1) + 20))
{
case 't':
return post_brdtitle(xo);
Expand Down Expand Up @@ -4682,7 +4682,7 @@ XoXpost( /* Thor: call from post_cb */
break;
}

mid = (left + right) / 2U;
mid = (left + right) >> 1;
cptr = &chain[mid];
cmp = strcmp(title, cptr->subject);

Expand Down
32 changes: 16 additions & 16 deletions maple/talk.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ can_see(
{
for (count = up->pal_max; count > 0;)
{
datum = cache[mid = count / 2U];
datum = cache[mid = count >> 1];
if ((-cuser.userno) == datum)
return 2;
if ((-cuser.userno) > datum)
Expand All @@ -186,7 +186,7 @@ can_see(
{
for (count = up->pal_max; count > 0;)
{
datum = cache[mid = count / 2U];
datum = cache[mid = count >> 1];
if (cuser.userno == datum)
return 1;
if (cuser.userno > datum)
Expand Down Expand Up @@ -215,7 +215,7 @@ is_bad(
{
for (count = pal_count; count > 0;)
{
datum = cache[mid = count / 2U];
datum = cache[mid = count >> 1];
if ((-userno) == datum)
return true;
if ((-userno) > datum)
Expand Down Expand Up @@ -244,7 +244,7 @@ can_banmsg(
{
for (count = up->banmsg_max; count > 0;)
{
datum = cache[mid = count / 2U];
datum = cache[mid = count >> 1];
if (cuser.userno == datum)
return true;
if (cuser.userno > datum)
Expand Down Expand Up @@ -355,7 +355,7 @@ is_pal(
{
for (count = pal_count; count > 0;)
{
datum = cache[mid = count / 2U];
datum = cache[mid = count >> 1];
if (userno == datum)
return true;
if (userno > datum)
Expand Down Expand Up @@ -384,7 +384,7 @@ is_banmsg(
{
for (count = cutmp->banmsg_max; count > 0;)
{
datum = cache[mid = count / 2U];
datum = cache[mid = count >> 1];
if (userno == datum)
return true;
if (userno > datum)
Expand Down Expand Up @@ -1354,7 +1354,7 @@ bm_belong(

while (count > 0)
{
datum = up[mid = count / 2U];
datum = up[mid = count >> 1];
if (userno == datum)
{
result = BRD_R_BIT | BRD_W_BIT;
Expand All @@ -1374,7 +1374,7 @@ bm_belong(
up = (int *) board_img;
while (wcount > 0)
{
datum = up[mid = wcount / 2U];
datum = up[mid = wcount >> 1];
if (-userno == datum)
{
result = BRD_R_BIT;
Expand Down Expand Up @@ -2552,15 +2552,15 @@ talk_speak(
memset(&mywin, 0, sizeof(mywin));
memset(&itswin, 0, sizeof(itswin));

ch = b_lines / 2U;
ch = b_lines >> 1;
mywin.eline = ch - 1;
itswin.curln = itswin.sline = ch + 1;
itswin.eline = b_lines - 1;

clear();
move(ch, 0);
prints("\x1b[1;46;37m 談天說地 \x1b[45m%*s%s】 ◆ %s%*s\x1b[m",
i/2U, "", buf, page_requestor, (i+1)/2U, "");
i>>1, "", buf, page_requestor, (i+1)>>1, "");
#if 1
outf(FOOTER_TALK);
#endif
Expand Down Expand Up @@ -3149,9 +3149,9 @@ ulist_body(
prints("%6d%c%s%-13s%-*.*s%s%-*.*s%c%c %-12.12s %5.5s",
cnt, (up->ufo & UFO_WEB)?'*':' ',
color, up->userid,
d_cols/2U + 22, d_cols/2U + 21, (HAS_PERM(PERM_SYSOP) && (cuser.ufo2 & UFO2_REALNAME))? up->realname : up->username,
(d_cols >> 1) + 22, (d_cols >> 1) + 21, (HAS_PERM(PERM_SYSOP) && (cuser.ufo2 & UFO2_REALNAME))? up->realname : up->username,
colortmp > 0 ? "\x1b[m" : "",
(d_cols+1)/2U + 16, (d_cols+1)/2U + 15,
((d_cols+1) >> 1) + 16, ((d_cols+1) >> 1) + 15,
(cuser.ufo2 & UFO2_SHIP) ? ship : ((up->ufo & UFO_HIDDEN)&&!HAS_PERM(PERM_SYSOP)) ?
HIDDEN_SRC : up->from, diff, diffmsg,
bmode(up, 0), buf);
Expand Down Expand Up @@ -3367,14 +3367,14 @@ ulist_neck(
prints(" 排列方式:[\x1b[1m%s\x1b[m] 上站人數:%d %s我的朋友:%d %s與我為友:%d %s壞人:%d \x1b[0;36m板友:%d\x1b[m",
msg_pickup_way[pickup_way], total_num, COLOR_PAL, friend_num+pfriend_num, COLOR_OPAL, friend_num+ofriend_num, COLOR_BAD, bfriend_num, board_pals);
prints(NECK_ULIST,
d_cols/2U + 22, (HAS_PERM(PERM_SYSOP) && (cuser.ufo2 & UFO2_REALNAME)) ? "真實姓名" : "暱 稱",
(d_cols+1)/2U + 13, (cuser.ufo2 & UFO2_SHIP) ? "好友描述" :"故鄉", "動態");
(d_cols >> 1) + 22, (HAS_PERM(PERM_SYSOP) && (cuser.ufo2 & UFO2_REALNAME)) ? "真實姓名" : "暱 稱",
((d_cols+1) >> 1) + 13, (cuser.ufo2 & UFO2_SHIP) ? "好友描述" :"故鄉", "動態");
#else
prints(" 排列方式:[\x1b[1m%s\x1b[m] 上站人數:%d %s我的朋友:%d %s與我為友:%d %s壞人:%d\x1b[m",
msg_pickup_way[pickup_way], total_num, COLOR_PAL, friend_num+pfriend_num, COLOR_OPAL, friend_num+ofriend_num, COLOR_BAD, bfriend_num);
prints(NECK_ULIST,
d_cols/2U + 22, (HAS_PERM(PERM_SYSOP) && (cuser.ufo & UFO_REALNAME)) ? "真實姓名" : "暱 稱",
(d_cols+1)/2U + 13, (cuser.ufo2 & UFO2_SHIP) ? "好友描述" :"故鄉", "動態");
(d_cols >> 1) + 22, (HAS_PERM(PERM_SYSOP) && (cuser.ufo & UFO_REALNAME)) ? "真實姓名" : "暱 稱",
((d_cols+1) >> 1) + 13, (cuser.ufo2 & UFO2_SHIP) ? "好友描述" :"故鄉", "動態");
#endif

return ulist_body(xo);
Expand Down
Loading

0 comments on commit 39902b5

Please sign in to comment.