Convert ffs() != 0 callers to ctz32()

There are a number of ffs(3) callers that do roughly:

  bit = ffs(val);
  if (bit) {
      do_something(bit - 1);
  }

This pattern can be converted to ctz32() like this:

  zeroes = ctz32(val);
  if (zeroes != 32) {
      do_something(zeroes);
  }

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 1427124571-28598-6-git-send-email-stefanha@redhat.com
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Stefan Hajnoczi 2015-03-23 15:29:27 +00:00 committed by Kevin Wolf
parent 786a4ea82e
commit bd2a88840e
6 changed files with 26 additions and 27 deletions

View File

@ -2004,8 +2004,7 @@ static void omap_mpuio_write(void *opaque, hwaddr addr,
case 0x04: /* OUTPUT_REG */ case 0x04: /* OUTPUT_REG */
diff = (s->outputs ^ value) & ~s->dir; diff = (s->outputs ^ value) & ~s->dir;
s->outputs = value; s->outputs = value;
while ((ln = ffs(diff))) { while ((ln = ctz32(diff)) != 32) {
ln --;
if (s->handler[ln]) if (s->handler[ln])
qemu_set_irq(s->handler[ln], (value >> ln) & 1); qemu_set_irq(s->handler[ln], (value >> ln) & 1);
diff &= ~(1 << ln); diff &= ~(1 << ln);
@ -2017,8 +2016,7 @@ static void omap_mpuio_write(void *opaque, hwaddr addr,
s->dir = value; s->dir = value;
value = s->outputs & ~s->dir; value = s->outputs & ~s->dir;
while ((ln = ffs(diff))) { while ((ln = ctz32(diff)) != 32) {
ln --;
if (s->handler[ln]) if (s->handler[ln])
qemu_set_irq(s->handler[ln], (value >> ln) & 1); qemu_set_irq(s->handler[ln], (value >> ln) & 1);
diff &= ~(1 << ln); diff &= ~(1 << ln);

View File

@ -814,12 +814,12 @@ static uint32_t find_free_port_id(VirtIOSerial *vser)
max_nr_ports = vser->serial.max_virtserial_ports; max_nr_ports = vser->serial.max_virtserial_ports;
for (i = 0; i < (max_nr_ports + 31) / 32; i++) { for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
uint32_t map, bit; uint32_t map, zeroes;
map = vser->ports_map[i]; map = vser->ports_map[i];
bit = ffs(~map); zeroes = ctz32(~map);
if (bit) { if (zeroes != 32) {
return (bit - 1) + i * 32; return zeroes + i * 32;
} }
} }
return VIRTIO_CONSOLE_BAD_ID; return VIRTIO_CONSOLE_BAD_ID;

View File

@ -125,8 +125,7 @@ static void omap_gpio_write(void *opaque, hwaddr addr,
case 0x04: /* DATA_OUTPUT */ case 0x04: /* DATA_OUTPUT */
diff = (s->outputs ^ value) & ~s->dir; diff = (s->outputs ^ value) & ~s->dir;
s->outputs = value; s->outputs = value;
while ((ln = ffs(diff))) { while ((ln = ctz32(diff)) != 32) {
ln --;
if (s->handler[ln]) if (s->handler[ln])
qemu_set_irq(s->handler[ln], (value >> ln) & 1); qemu_set_irq(s->handler[ln], (value >> ln) & 1);
diff &= ~(1 << ln); diff &= ~(1 << ln);
@ -138,8 +137,7 @@ static void omap_gpio_write(void *opaque, hwaddr addr,
s->dir = value; s->dir = value;
value = s->outputs & ~s->dir; value = s->outputs & ~s->dir;
while ((ln = ffs(diff))) { while ((ln = ctz32(diff)) != 32) {
ln --;
if (s->handler[ln]) if (s->handler[ln])
qemu_set_irq(s->handler[ln], (value >> ln) & 1); qemu_set_irq(s->handler[ln], (value >> ln) & 1);
diff &= ~(1 << ln); diff &= ~(1 << ln);
@ -253,8 +251,7 @@ static inline void omap2_gpio_module_out_update(struct omap2_gpio_s *s,
s->outputs ^= diff; s->outputs ^= diff;
diff &= ~s->dir; diff &= ~s->dir;
while ((ln = ffs(diff))) { while ((ln = ctz32(diff)) != 32) {
ln --;
qemu_set_irq(s->handler[ln], (s->outputs >> ln) & 1); qemu_set_irq(s->handler[ln], (s->outputs >> ln) & 1);
diff &= ~(1 << ln); diff &= ~(1 << ln);
} }
@ -442,8 +439,8 @@ static void omap2_gpio_module_write(void *opaque, hwaddr addr,
s->dir = value; s->dir = value;
value = s->outputs & ~s->dir; value = s->outputs & ~s->dir;
while ((ln = ffs(diff))) { while ((ln = ctz32(diff)) != 32) {
diff &= ~(1 <<-- ln); diff &= ~(1 << ln);
qemu_set_irq(s->handler[ln], (value >> ln) & 1); qemu_set_irq(s->handler[ln], (value >> ln) & 1);
} }

View File

@ -171,9 +171,13 @@ static uint32_t omap_i2c_read(void *opaque, hwaddr addr)
case 0x0c: /* I2C_IV */ case 0x0c: /* I2C_IV */
if (s->revision >= OMAP2_INTR_REV) if (s->revision >= OMAP2_INTR_REV)
break; break;
ret = ffs(s->stat & s->mask); ret = ctz32(s->stat & s->mask);
if (ret) if (ret != 32) {
s->stat ^= 1 << (ret - 1); s->stat ^= 1 << ret;
ret++;
} else {
ret = 0;
}
omap_i2c_interrupts_update(s); omap_i2c_interrupts_update(s);
return ret; return ret;

View File

@ -23,7 +23,7 @@
static void aw_a10_pic_update(AwA10PICState *s) static void aw_a10_pic_update(AwA10PICState *s)
{ {
uint8_t i; uint8_t i;
int irq = 0, fiq = 0, pending; int irq = 0, fiq = 0, zeroes;
s->vector = 0; s->vector = 0;
@ -32,9 +32,9 @@ static void aw_a10_pic_update(AwA10PICState *s)
fiq |= s->select[i] & s->irq_pending[i] & ~s->mask[i]; fiq |= s->select[i] & s->irq_pending[i] & ~s->mask[i];
if (!s->vector) { if (!s->vector) {
pending = ffs(s->irq_pending[i] & ~s->mask[i]); zeroes = ctz32(s->irq_pending[i] & ~s->mask[i]);
if (pending) { if (zeroes != 32) {
s->vector = (i * 32 + pending - 1) * 4; s->vector = (i * 32 + zeroes) * 4;
} }
} }
} }

View File

@ -1141,18 +1141,18 @@ static int kvm_irqchip_get_virq(KVMState *s)
{ {
uint32_t *word = s->used_gsi_bitmap; uint32_t *word = s->used_gsi_bitmap;
int max_words = ALIGN(s->gsi_count, 32) / 32; int max_words = ALIGN(s->gsi_count, 32) / 32;
int i, bit; int i, zeroes;
bool retry = true; bool retry = true;
again: again:
/* Return the lowest unused GSI in the bitmap */ /* Return the lowest unused GSI in the bitmap */
for (i = 0; i < max_words; i++) { for (i = 0; i < max_words; i++) {
bit = ffs(~word[i]); zeroes = ctz32(~word[i]);
if (!bit) { if (zeroes == 32) {
continue; continue;
} }
return bit - 1 + i * 32; return zeroes + i * 32;
} }
if (!s->direct_msi && retry) { if (!s->direct_msi && retry) {
retry = false; retry = false;