Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Feb 2013 17:28:39 -0700
From:      Chris Torek <torek@torek.net>
To:        freebsd-virtualization@freebsd.org
Subject:   trivial improvement for usr.sbin/bhyve/pci_virtio_block.c
Message-ID:  <201303010028.r210Sdd8039430@elf.torek.net>

next in thread | raw e-mail | index | archive | help
I was looking through the bhyve code and noticed an obvious
easy (if trivial) code improvement.

Tested "standalone" rather than inside bhyve (with both gcc and
clang, on FreeBSD 9.0).

Not sure where/how diffs should go, so I figured I would send this
here as a test. :-)

Chris

Index: pci_virtio_block.c
===================================================================
--- pci_virtio_block.c	(revision 247510)
+++ pci_virtio_block.c	(working copy)
@@ -164,14 +164,19 @@
 static int
 hq_num_avail(struct vring_hqueue *hq)
 {
-	int ndesc;
+	uint16_t ndesc;
 
-	if (*hq->hq_avail_idx >= hq->hq_cur_aidx)
-		ndesc = *hq->hq_avail_idx - hq->hq_cur_aidx;
-	else
-		ndesc = UINT16_MAX - hq->hq_cur_aidx + *hq->hq_avail_idx + 1;
+	/*
+	 * We're just computing (a-b) in GF(2^16).
+	 *
+	 * The only glitch here is that in standard C,
+	 * uint16_t promotes to (signed) int when int has
+	 * more than 16 bits (pretty much always now), so
+	 * we have to force it back to unsigned.
+	 */
+	ndesc = (unsigned)*hq->hq_avail_idx - (unsigned)hq->hq_cur_aidx;
 
-	assert(ndesc >= 0 && ndesc <= hq->hq_size);
+	assert(ndesc <= hq->hq_size);
 
 	return (ndesc);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201303010028.r210Sdd8039430>