Use /2 and 2* instead of >>1 and <<1 which are tricky with signed types

Today's compilers generate shift instructions to perform division and
multiplications by powers of 2 [1]. `(x >> 1)` looks straightforward enough, but
if x is signed the code will fail when x < 0. The compiler knows better: use
`x / 2`.

That's why we have code like this:

    (long)((long_u)Rows >> 1))

instead of the cleaner version that generates the same or better machine code:

    Rows / 2

[1] http://goo.gl/J4WpG7
This commit is contained in:
Felipe Oliveira Carvalho
2014-04-19 13:12:04 -03:00
committed by Thiago de Arruda
parent 9a5b3eee5f
commit db23cb05d1
6 changed files with 11 additions and 13 deletions

View File

@@ -7162,7 +7162,7 @@ find_internal_func (
* Find the function name in the table. Binary search. * Find the function name in the table. Binary search.
*/ */
while (first <= last) { while (first <= last) {
x = first + ((unsigned)(last - first) >> 1); x = first + (last - first) / 2;
cmp = STRCMP(name, functions[x].f_name); cmp = STRCMP(name, functions[x].f_name);
if (cmp < 0) if (cmp < 0)
last = x - 1; last = x - 1;

View File

@@ -141,7 +141,7 @@ hashitem_T* hash_lookup(hashtab_T *ht, char_u *key, hash_T hash)
// count a "miss" for hashtab lookup // count a "miss" for hashtab lookup
hash_count_perturb++; hash_count_perturb++;
#endif // ifdef HT_DEBUG #endif // ifdef HT_DEBUG
idx = (unsigned)((idx << 2U) + idx + perturb + 1U); idx = 5 * idx + perturb + 1;
hi = &ht->ht_array[idx & ht->ht_mask]; hi = &ht->ht_array[idx & ht->ht_mask];
if (hi->hi_key == NULL) { if (hi->hi_key == NULL) {
@@ -378,7 +378,7 @@ static int hash_may_resize(hashtab_T *ht, int minitems)
newitem = &newarray[newi]; newitem = &newarray[newi];
if (newitem->hi_key != NULL) { if (newitem->hi_key != NULL) {
for (perturb = olditem->hi_hash;; perturb >>= PERTURB_SHIFT) { for (perturb = olditem->hi_hash;; perturb >>= PERTURB_SHIFT) {
newi = (unsigned)((newi << 2U) + newi + perturb + 1U); newi = 5 * newi + perturb + 1;
newitem = &newarray[newi & newmask]; newitem = &newarray[newi & newmask];
if (newitem->hi_key == NULL) { if (newitem->hi_key == NULL) {
break; break;

View File

@@ -192,7 +192,7 @@ memfile_T *mf_open(char_u *fname, int flags)
unsigned page_size = mfp->mf_page_size; unsigned page_size = mfp->mf_page_size;
while (shift > 0 && (page_size & 1) == 0) { while (shift > 0 && (page_size & 1) == 0) {
page_size = page_size >> 1; page_size /= 2;
--shift; --shift;
} }
mfp->mf_used_count_max = (p_mm << shift) / page_size; mfp->mf_used_count_max = (p_mm << shift) / page_size;

View File

@@ -2042,8 +2042,8 @@ void set_init_1(void)
/* Initialize the 'cdpath' option's default value. */ /* Initialize the 'cdpath' option's default value. */
cdpath = vim_getenv((char_u *)"CDPATH", &mustfree); cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
if (cdpath != NULL) { if (cdpath != NULL) {
buf = alloc((unsigned)((STRLEN(cdpath) << 1) + 2)); buf = xmalloc(2 * STRLEN(cdpath) + 2);
if (buf != NULL) { {
buf[0] = ','; /* start with ",", current dir first */ buf[0] = ','; /* start with ",", current dir first */
j = 1; j = 1;
for (i = 0; cdpath[i] != NUL; ++i) { for (i = 0; cdpath[i] != NUL; ++i) {
@@ -2376,7 +2376,7 @@ void set_init_2(void)
* 'scroll' defaults to half the window height. Note that this default is * 'scroll' defaults to half the window height. Note that this default is
* wrong when the window height changes. * wrong when the window height changes.
*/ */
set_number_default("scroll", (long)((long_u)Rows >> 1)); set_number_default("scroll", Rows / 2);
idx = findoption((char_u *)"scroll"); idx = findoption((char_u *)"scroll");
if (idx >= 0 && !(options[idx].flags & P_WAS_SET)) if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
set_option_default(idx, OPT_LOCAL, p_cp); set_option_default(idx, OPT_LOCAL, p_cp);

View File

@@ -312,7 +312,7 @@ qf_init_ext (
/* /*
* Get some space to modify the format string into. * Get some space to modify the format string into.
*/ */
i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2); i = 3 * FMT_PATTERNS + 4 * (int)STRLEN(efm);
for (round = FMT_PATTERNS; round > 0; ) for (round = FMT_PATTERNS; round > 0; )
i += (int)STRLEN(fmt_pat[--round].pattern); i += (int)STRLEN(fmt_pat[--round].pattern);
#ifdef COLON_IN_FILENAME #ifdef COLON_IN_FILENAME

View File

@@ -1516,8 +1516,7 @@ win_equal_rec (
if (totwincount == 0) if (totwincount == 0)
new_size = room; new_size = room;
else else
new_size = (wincount * room + ((unsigned)totwincount >> 1)) new_size = (wincount * room + (totwincount / 2)) / totwincount;
/ totwincount;
if (hnc) { /* add next_curwin size */ if (hnc) { /* add next_curwin size */
next_curwin_size -= p_wiw - (m - n); next_curwin_size -= p_wiw - (m - n);
new_size += next_curwin_size; new_size += next_curwin_size;
@@ -1638,8 +1637,7 @@ win_equal_rec (
if (totwincount == 0) if (totwincount == 0)
new_size = room; new_size = room;
else else
new_size = (wincount * room + ((unsigned)totwincount >> 1)) new_size = (wincount * room + (totwincount / 2)) / totwincount;
/ totwincount;
if (hnc) { /* add next_curwin size */ if (hnc) { /* add next_curwin size */
next_curwin_size -= p_wh - (m - n); next_curwin_size -= p_wh - (m - n);
new_size += next_curwin_size; new_size += next_curwin_size;
@@ -4644,7 +4642,7 @@ void win_new_width(win_T *wp, int width)
void win_comp_scroll(win_T *wp) void win_comp_scroll(win_T *wp)
{ {
wp->w_p_scr = ((unsigned)wp->w_height >> 1); wp->w_p_scr = wp->w_height / 2;
if (wp->w_p_scr == 0) if (wp->w_p_scr == 0)
wp->w_p_scr = 1; wp->w_p_scr = 1;
} }