From owner-freebsd-virtualization@FreeBSD.ORG Fri Mar 1 00:49:41 2013 Return-Path: Delivered-To: freebsd-virtualization@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id C2BB4363 for ; Fri, 1 Mar 2013 00:49:41 +0000 (UTC) (envelope-from torek@torek.net) Received: from elf.torek.net (50-73-42-1-utah.hfc.comcastbusiness.net [50.73.42.1]) by mx1.freebsd.org (Postfix) with ESMTP id A0CC8C2D for ; Fri, 1 Mar 2013 00:49:40 +0000 (UTC) Received: from elf.torek.net (localhost [127.0.0.1]) by elf.torek.net (8.14.5/8.14.5) with ESMTP id r210Sdd8039430 for ; Thu, 28 Feb 2013 17:28:39 -0700 (MST) (envelope-from torek@torek.net) Message-Id: <201303010028.r210Sdd8039430@elf.torek.net> From: Chris Torek To: freebsd-virtualization@freebsd.org Subject: trivial improvement for usr.sbin/bhyve/pci_virtio_block.c Date: Thu, 28 Feb 2013 17:28:39 -0700 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.2.7 (elf.torek.net [127.0.0.1]); Thu, 28 Feb 2013 17:28:39 -0700 (MST) X-BeenThere: freebsd-virtualization@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Discussion of various virtualization techniques FreeBSD supports." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Mar 2013 00:49:41 -0000 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); }