From a7ce7322f8b6ee9d33e08cf79b4d93b349159af3 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Wed, 23 Oct 2019 12:30:39 +0200
Subject: [PATCH 4/4] world/kernel: Use the rounddown2() macro in various
 places.

---
 bin/ls/print.c                         |  4 ++--
 lib/libc/db/hash/hash_page.c           |  2 +-
 lib/libc/net/name6.c                   |  4 ++--
 lib/libc/stdio/fseek.c                 |  4 ++--
 lib/libc/stdlib/merge.c                |  5 ++---
 lib/libc/sysvipc/shm.c                 |  2 +-
 lib/libkvm/kvm_proc.c                  |  2 +-
 lib/libusb/libusb20_desc.c             |  7 +++----
 libexec/rtld-elf/malloc.c              |  2 +-
 sbin/gpt/map.c                         |  5 ++---
 sbin/ifconfig/af_inet.c                |  3 +--
 sbin/restore/dirs.c                    |  2 +-
 sys/bus/u4b/usb_busdma.c               |  4 ++--
 sys/dev/netif/ti/if_ti.c               |  2 +-
 sys/dev/pccard/exca/exca.c             |  3 +--
 sys/dev/raid/hptmv/gui_lib.c           |  2 +-
 sys/dev/video/cxm/cxm.h                |  2 +-
 sys/emulation/ndis/ntoskrnl_var.h      |  2 +-
 sys/gnu/vfs/ext2fs/ext2_lookup.c       |  2 +-
 sys/kern/imgact_elf.c                  |  2 +-
 sys/kern/init_main.c                   |  2 +-
 sys/kern/subr_rman.c                   |  4 ++--
 sys/kern/sysv_shm.c                    |  3 ++-
 sys/kern/uipc_mbuf.c                   |  2 +-
 sys/kern/vfs_cluster.c                 |  4 ++--
 sys/net/pf/if_pfsync.c                 |  2 +-
 sys/platform/pc64/vmm/vmx.h            |  2 +-
 sys/platform/pc64/x86_64/amd64_mem.c   |  2 +-
 sys/platform/vkernel64/platform/init.c |  4 ++--
 sys/platform/vkernel64/platform/pmap.c | 14 +++++++++-----
 sys/sys/mbuf.h                         |  4 ++--
 sys/vfs/hammer2/hammer2_chain.c        |  9 +++------
 sys/vfs/hammer2/hammer2_disk.h         |  2 +-
 sys/vfs/isofs/cd9660/cd9660_bmap.c     |  2 +-
 sys/vfs/isofs/cd9660/cd9660_vnops.c    |  2 +-
 sys/vfs/nfs/nfs_vfsops.c               |  6 +++---
 sys/vfs/ntfs/ntfs_subr.c               |  2 +-
 sys/vfs/smbfs/smbfs_io.c               |  4 ++--
 sys/vfs/ufs/ffs_softdep.c              |  2 +-
 sys/vfs/ufs/ufs_dirhash.c              |  2 +-
 sys/vfs/ufs/ufs_lookup.c               |  6 +++---
 sys/vm/vm_contig.c                     |  3 +--
 sys/vm/vm_page.c                       |  4 ++--
 usr.bin/colrm/colrm.c                  |  4 ++--
 usr.bin/tail/misc.c                    |  5 ++---
 45 files changed, 76 insertions(+), 81 deletions(-)

diff --git a/bin/ls/print.c b/bin/ls/print.c
index 606893745a..630a54314b 100644
--- a/bin/ls/print.c
+++ b/bin/ls/print.c
@@ -265,7 +265,7 @@ printcol(const DISPLAY *dp)
 	if (f_type)
 		colwidth += 1;
 
-	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
+	colwidth = rounddown2(colwidth + tabwidth, tabwidth);
 	if (termwidth < 2 * colwidth) {
 		printscol(dp);
 		return;
@@ -296,7 +296,7 @@ printcol(const DISPLAY *dp)
 				base += numrows;
 			if (base >= num)
 				break;
-			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
+			while ((cnt = (rounddown2(chcnt + tabwidth, tabwidth)))
 			    <= endcol) {
 				if (f_sortacross && col + 1 >= numcols)
 					break;
diff --git a/lib/libc/db/hash/hash_page.c b/lib/libc/db/hash/hash_page.c
index 6914dfaa63..3a8b693d46 100644
--- a/lib/libc/db/hash/hash_page.c
+++ b/lib/libc/db/hash/hash_page.c
@@ -674,7 +674,7 @@ overflow_page(HTAB *hashp)
 			bit = hashp->LAST_FREED &
 			    ((hashp->BSIZE << BYTE_SHIFT) - 1);
 			j = bit / BITS_PER_MAP;
-			bit = bit & ~(BITS_PER_MAP - 1);
+			bit = rounddown2(bit, BITS_PER_MAP);
 		} else {
 			bit = 0;
 			j = 0;
diff --git a/lib/libc/net/name6.c b/lib/libc/net/name6.c
index 6ade2973e4..dba468fc92 100644
--- a/lib/libc/net/name6.c
+++ b/lib/libc/net/name6.c
@@ -326,7 +326,7 @@ getipnodebyaddr(const void *src, size_t len, int af, int *errp)
 			*errp = NO_RECOVERY;
 			return NULL;
 		}
-		if ((long)src & ~(sizeof(struct in_addr) - 1)) {
+		if (rounddown2((long)src, sizeof(struct in_addr))) {
 			memcpy(&addrbuf, src, len);
 			src = &addrbuf;
 		}
@@ -339,7 +339,7 @@ getipnodebyaddr(const void *src, size_t len, int af, int *errp)
 			*errp = NO_RECOVERY;
 			return NULL;
 		}
-		if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) {	/*XXX*/
+		if (rounddown2((long)src, sizeof(struct in6_addr) / 2)) {	/*XXX*/
 			memcpy(&addrbuf, src, len);
 			src = &addrbuf;
 		}
diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c
index ad43eedbdb..2fadf85e5e 100644
--- a/lib/libc/stdio/fseek.c
+++ b/lib/libc/stdio/fseek.c
@@ -34,7 +34,7 @@
  */
 
 #include "namespace.h"
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -255,7 +255,7 @@ _fseeko(FILE *fp, off_t offset, int whence, int ltest)
 	 * copying bytes laboriously.  Using a block boundary also
 	 * ensures that we only read one block, rather than two.
 	 */
-	curoff = target & ~(fp->_blksize - 1);
+	curoff = rounddown2(target, fp->_blksize);
 	if (_sseek(fp, curoff, SEEK_SET) == POS_ERR)
 		goto dumb;
 	fp->pub._r = 0;
diff --git a/lib/libc/stdlib/merge.c b/lib/libc/stdlib/merge.c
index c76e9094d7..efd4dcd32e 100644
--- a/lib/libc/stdlib/merge.c
+++ b/lib/libc/stdlib/merge.c
@@ -31,7 +31,6 @@
  *
  * @(#)merge.c	8.2 (Berkeley) 2/14/94
  * $FreeBSD: src/lib/libc/stdlib/merge.c,v 1.8 2007/01/09 00:28:10 imp Exp $
- * $DragonFly: src/lib/libc/stdlib/merge.c,v 1.6 2005/11/20 12:37:48 swildner Exp $
  */
 
 /*
@@ -48,7 +47,7 @@
  * (The default is pairwise merging.)
  */
 
-#include <sys/types.h>
+#include <sys/param.h>
 
 #include <errno.h>
 #include <stdlib.h>
@@ -87,7 +86,7 @@ static void insertionsort(u_char *, size_t, size_t,
 /* Assumption: PSIZE is a power of 2. */
 #define EVAL(p) (u_char **)						\
 	((u_char *)0 +							\
-	    (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
+	    (rounddown2((u_char *)p + PSIZE - 1 - (u_char *)0, PSIZE)))
 
 /*
  * Arguments are as for qsort.
diff --git a/lib/libc/sysvipc/shm.c b/lib/libc/sysvipc/shm.c
index 2adf107b69..00c8f6f0ee 100644
--- a/lib/libc/sysvipc/shm.c
+++ b/lib/libc/sysvipc/shm.c
@@ -245,7 +245,7 @@ sysvipc_shmat(int shmid, const void *shmaddr, int shmflg)
 	flags = MAP_SHARED;
 	if (shmaddr) {
 		if (shmflg & SHM_RND) {
-			addr = (void *)((uintptr_t)shmaddr & ~(SHMLBA-1));
+			addr = (void *)(rounddown2((uintptr_t)shmaddr, SHMLBA));
 		} else if (((uintptr_t)shmaddr & (SHMLBA-1)) == 0) {
 			addr = __DECONST(void *, shmaddr);
 		} else {
diff --git a/lib/libkvm/kvm_proc.c b/lib/libkvm/kvm_proc.c
index be1529ed46..96864245bb 100644
--- a/lib/libkvm/kvm_proc.c
+++ b/lib/libkvm/kvm_proc.c
@@ -648,7 +648,7 @@ kvm_argv(kvm_t *kd, pid_t pid, u_long addr, int narg, int maxcnt)
 	while (argv < kd->argv + narg && *argv != NULL) {
 
 		/* get the address that the current argv string is on */
-		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
+		addr = rounddown2((u_long)*argv, PAGE_SIZE);
 
 		/* is it the same page as the last one? */
 		if (addr != oaddr) {
diff --git a/lib/libusb/libusb20_desc.c b/lib/libusb/libusb20_desc.c
index 583ede47c8..a25b9e34df 100644
--- a/lib/libusb/libusb20_desc.c
+++ b/lib/libusb/libusb20_desc.c
@@ -31,6 +31,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <sys/param.h>
 #include <sys/queue.h>
 #endif
 
@@ -431,8 +432,7 @@ libusb20_me_encode(void *ptr, uint16_t len, const void *pd)
 			break;
 
 		case LIBUSB20_ME_STRUCT:
-			pd_offset = -((-pd_offset) &
-			    ~(LIBUSB20_ME_STRUCT_ALIGN - 1));	/* align */
+			pd_offset = -rounddown2(-pd_offset, LIBUSB20_ME_STRUCT_ALIGN);	/* align */
 			while (pd_count--) {
 				void *src_ptr;
 				uint16_t src_len;
@@ -669,8 +669,7 @@ libusb20_me_decode(const void *ptr, uint16_t len, void *pd)
 			break;
 
 		case LIBUSB20_ME_STRUCT:
-			pd_offset = -((-pd_offset) &
-			    ~(LIBUSB20_ME_STRUCT_ALIGN - 1));	/* align */
+			pd_offset = -rounddown2(-pd_offset, LIBUSB20_ME_STRUCT_ALIGN);	/* align */
 			while (pd_count--) {
 				uint16_t temp;
 				uint16_t dummy;
diff --git a/libexec/rtld-elf/malloc.c b/libexec/rtld-elf/malloc.c
index 5170ef110f..1ea455025f 100644
--- a/libexec/rtld-elf/malloc.c
+++ b/libexec/rtld-elf/malloc.c
@@ -450,7 +450,7 @@ morepages(int n)
 			    addr);
 	}
 
-	offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1));
+	offset = (long)pagepool_start - rounddown2((long)pagepool_start, pagesz);
 
 	if ((pagepool_start = mmap(0, n * pagesz,
 			PROT_READ|PROT_WRITE,
diff --git a/sbin/gpt/map.c b/sbin/gpt/map.c
index 53c4fe206a..11d4e8d198 100644
--- a/sbin/gpt/map.c
+++ b/sbin/gpt/map.c
@@ -24,10 +24,9 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * $FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $
- * $DragonFly: src/sbin/gpt/map.c,v 1.2 2007/06/17 08:34:59 dillon Exp $
  */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -35,7 +34,7 @@
 #include "map.h"
 
 #define ROUNDTO 2048	// must be a power of two
-#define ROUNDDOWN(x) ((x-1) & ~(ROUNDTO-1))
+#define ROUNDDOWN(x) rounddown2(x - 1, ROUNDTO)
 #define ROUNDUP(x) (ROUNDDOWN(x) + ROUNDTO)
 
 int lbawidth;
diff --git a/sbin/ifconfig/af_inet.c b/sbin/ifconfig/af_inet.c
index c804596d20..1a4fd0e2d8 100644
--- a/sbin/ifconfig/af_inet.c
+++ b/sbin/ifconfig/af_inet.c
@@ -118,8 +118,7 @@ in_getaddr(const char *s, int which)
 				errx(1, "%s: bad value", s);
 			}
 			min->sin_len = sizeof(*min);
-			min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
-						     0xffffffff);
+			min->sin_addr.s_addr = htonl(rounddown2(0xffffffff, 1LL << (32 - masklen)));
 		}
 	}
 
diff --git a/sbin/restore/dirs.c b/sbin/restore/dirs.c
index 969eb8c43c..9bf8b26444 100644
--- a/sbin/restore/dirs.c
+++ b/sbin/restore/dirs.c
@@ -455,7 +455,7 @@ rst_seekdir(RST_DIR *dirpl, long loc, long base)
 	loc -= base;
 	if (loc < 0)
 		fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
-	lseek(dirpl->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
+	lseek(dirpl->dd_fd, base + rounddown2(loc, DIRBLKSIZ), SEEK_SET);
 	dirpl->dd_loc = loc & (DIRBLKSIZ - 1);
 	if (dirpl->dd_loc != 0)
 		dirpl->dd_size = read(dirpl->dd_fd, dirpl->dd_buf, DIRBLKSIZ);
diff --git a/sys/bus/u4b/usb_busdma.c b/sys/bus/u4b/usb_busdma.c
index 8e75ed5b6f..fd10e964aa 100644
--- a/sys/bus/u4b/usb_busdma.c
+++ b/sys/bus/u4b/usb_busdma.c
@@ -433,7 +433,7 @@ usb_pc_common_mem_cb(void *arg, bus_dma_segment_t *segs,
 
 	off = 0;
 	pg = pc->page_start;
-	pg->physaddr = segs->ds_addr & ~(USB_PAGE_SIZE - 1);
+	pg->physaddr = rounddown2(segs->ds_addr, USB_PAGE_SIZE);
 	rem = segs->ds_addr & (USB_PAGE_SIZE - 1);
 	pc->page_offset_buf = rem;
 	pc->page_offset_end += rem;
@@ -459,7 +459,7 @@ usb_pc_common_mem_cb(void *arg, bus_dma_segment_t *segs,
 				break;
 		}
 		pg++;
-		pg->physaddr = (segs->ds_addr + off) & ~(USB_PAGE_SIZE - 1);
+		pg->physaddr = rounddown2(segs->ds_addr + off, USB_PAGE_SIZE);
 	}
 
 done:
diff --git a/sys/dev/netif/ti/if_ti.c b/sys/dev/netif/ti/if_ti.c
index 7c42245105..abb265058f 100644
--- a/sys/dev/netif/ti/if_ti.c
+++ b/sys/dev/netif/ti/if_ti.c
@@ -380,7 +380,7 @@ ti_mem(struct ti_softc *sc, uint32_t addr, uint32_t len, caddr_t buf)
 			segsize = cnt;
 		else
 			segsize = TI_WINLEN - (segptr % TI_WINLEN);
-		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
+		CSR_WRITE_4(sc, TI_WINBASE, rounddown2(segptr, TI_WINLEN));
 		if (buf == NULL)
 			bzero((char *)ti_winbase + (segptr &
 			    (TI_WINLEN - 1)), segsize);
diff --git a/sys/dev/pccard/exca/exca.c b/sys/dev/pccard/exca/exca.c
index 81c5e4227a..586986e53c 100644
--- a/sys/dev/pccard/exca/exca.c
+++ b/sys/dev/pccard/exca/exca.c
@@ -52,7 +52,6 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * $FreeBSD: src/sys/dev/exca/exca.c,v 1.19 2005/01/11 00:32:43 imp Exp $
- * $DragonFly: src/sys/dev/pccard/exca/exca.c,v 1.4 2007/07/05 12:08:54 sephe Exp $
  */
 
 #include <sys/param.h>
@@ -378,7 +377,7 @@ exca_mem_set_offset(struct exca_softc *sc, struct resource *res,
 		    "set_memory_offset: specified resource not active\n");
 		return (ENOENT);
 	}
-	sc->mem[win].cardaddr = cardaddr & ~(EXCA_MEM_PAGESIZE - 1);
+	sc->mem[win].cardaddr = rounddown2(cardaddr, EXCA_MEM_PAGESIZE);
 	delta = cardaddr % EXCA_MEM_PAGESIZE;
 	if (deltap)
 		*deltap = delta;
diff --git a/sys/dev/raid/hptmv/gui_lib.c b/sys/dev/raid/hptmv/gui_lib.c
index 3e5baf5565..4419d13235 100644
--- a/sys/dev/raid/hptmv/gui_lib.c
+++ b/sys/dev/raid/hptmv/gui_lib.c
@@ -783,7 +783,7 @@ static DEVICEID hpt_create_array_v2(_VBUS_ARG PCREATE_ARRAY_PARAMS_V2 pParam)
 			for(i = 0; i < pArray->u.array.bArnMember; i++)
 				if(pArray->u.array.pMember[i]->VDeviceCapacity < capacity)
 					capacity = pArray->u.array.pMember[i]->VDeviceCapacity;
-			pArray->VDeviceCapacity = (capacity & ~(pArray->u.array.bStripeWitch - 1))
+			pArray->VDeviceCapacity = rounddown2(capacity, pArray->u.array.bStripeWitch)
 				* (pArray->u.array.bArnMember - 1);
 			break;
 
diff --git a/sys/dev/video/cxm/cxm.h b/sys/dev/video/cxm/cxm.h
index 19ed784a20..c4a5790040 100644
--- a/sys/dev/video/cxm/cxm.h
+++ b/sys/dev/video/cxm/cxm.h
@@ -180,7 +180,7 @@ enum cxm_mailbox_name { cxm_unknown_mailbox,
  * that the size of each piece must be a multiple of
  * 256 and less than 64k.
  */
-#define CXM_SG_SEGMENT  (0xff00 & ~(PAGE_SIZE - 1))
+#define CXM_SG_SEGMENT  rounddown2(0xff00, PAGE_SIZE)
 
 struct cxm_sg_entry {
 	uint32_t	src;
diff --git a/sys/emulation/ndis/ntoskrnl_var.h b/sys/emulation/ndis/ntoskrnl_var.h
index 4786d1778d..f725363d29 100644
--- a/sys/emulation/ndis/ntoskrnl_var.h
+++ b/sys/emulation/ndis/ntoskrnl_var.h
@@ -132,7 +132,7 @@ typedef struct mdl mdl, ndis_buffer;
 	(len) + (PAGE_SIZE - 1)) >> PAGE_SHIFT))
 
 #define PAGE_ALIGN(ptr)						\
-	((void *)((uintptr_t)(ptr) & ~(PAGE_SIZE - 1)))
+	((void *)(rounddown2((uintptr_t)ptr, PAGE_SIZE)))
 
 #define BYTE_OFFSET(ptr)					\
 	((uint32_t)((uintptr_t)(ptr) & (PAGE_SIZE - 1)))
diff --git a/sys/gnu/vfs/ext2fs/ext2_lookup.c b/sys/gnu/vfs/ext2fs/ext2_lookup.c
index df9829b769..591e405aa8 100644
--- a/sys/gnu/vfs/ext2fs/ext2_lookup.c
+++ b/sys/gnu/vfs/ext2fs/ext2_lookup.c
@@ -544,7 +544,7 @@ ext2_lookup(struct vop_old_lookup_args *ap)
 	 * in the cache as to where the entry was found.
 	 */
 	if (nameiop == NAMEI_LOOKUP)
-		dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
+		dp->i_diroff = rounddown2(dp->i_offset, DIRBLKSIZ);
 
 	/*
 	 * If deleting, and at end of pathname, return
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index 3a92b2aa7b..8d2e0cd51c 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -70,7 +70,7 @@
 #include <sys/ckpt.h>
 
 #define OLD_EI_BRAND	8
-#define truncps(va,ps)	((va) & ~(ps - 1))
+#define truncps(va,ps)	rounddown2(va, ps)
 #define aligned(a,t)	(truncps((u_long)(a), sizeof(t)) == (u_long)(a))
 
 static int __elfN(check_header)(const Elf_Ehdr *hdr);
diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index 98ebac21df..9a30b80886 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -645,7 +645,7 @@ start_init(void *dummy, struct trapframe *frame)
 		/*
 		 * Move out the arg pointers.
 		 */
-		uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1));
+		uap = (char **)(rounddown2((intptr_t)ucp, sizeof(intptr_t)));
 
 		/* terminator */
 		suword64((uint64_t *)(caddr_t)--uap, (uint64_t)0);
diff --git a/sys/kern/subr_rman.c b/sys/kern/subr_rman.c
index 267c47a70f..6246760091 100644
--- a/sys/kern/subr_rman.c
+++ b/sys/kern/subr_rman.c
@@ -225,8 +225,8 @@ rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
 			continue;
 		}
 		rstart = ulmax(s->r_start, start);
-		rstart = (rstart + ((1ul << RF_ALIGNMENT(flags))) - 1) &
-		    ~((1ul << RF_ALIGNMENT(flags)) - 1);
+		rstart = rounddown2(rstart + (1ul << RF_ALIGNMENT(flags)) - 1,
+		    1ul << RF_ALIGNMENT(flags));
 		rend = ulmin(s->r_end, ulmax(start + count - 1, end));
 		DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
 		       rstart, rend, (rend - rstart + 1), count));
diff --git a/sys/kern/sysv_shm.c b/sys/kern/sysv_shm.c
index 52a361431e..087f14afda 100644
--- a/sys/kern/sysv_shm.c
+++ b/sys/kern/sysv_shm.c
@@ -331,7 +331,8 @@ sys_shmat(struct shmat_args *uap)
 	if (uap->shmaddr) {
 		flags |= MAP_FIXED;
 		if (uap->shmflg & SHM_RND) {
-			attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
+			attach_va =
+			    rounddown2((vm_offset_t)uap->shmaddr, SHMLBA);
 		} else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
 			attach_va = (vm_offset_t)uap->shmaddr;
 		} else {
diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c
index b91bee487e..d0330c8f43 100644
--- a/sys/kern/uipc_mbuf.c
+++ b/sys/kern/uipc_mbuf.c
@@ -1957,7 +1957,7 @@ m_align(struct mbuf *m, int len)
 		adjust = MHLEN - len;
 	else
 		adjust = MLEN - len;
-	m->m_data += adjust &~ (sizeof(long)-1);
+	m->m_data += rounddown2(adjust, sizeof(long));
 }
 
 /*
diff --git a/sys/kern/vfs_cluster.c b/sys/kern/vfs_cluster.c
index 18abdf6172..ae71523245 100644
--- a/sys/kern/vfs_cluster.c
+++ b/sys/kern/vfs_cluster.c
@@ -159,14 +159,14 @@ cluster_getcache(cluster_cache_t *dummy, struct vnode *vp, off_t loffset)
 	for (i = 0; i < 4; ++i) {
 		if (cc[i].vp != vp)
 			continue;
-		if (((cc[i].v_cstart ^ loffset) & ~(CLUSTER_ZONE - 1)) == 0) {
+		if (rounddown2(cc[i].v_cstart ^ loffset, CLUSTER_ZONE) == 0) {
 			xact = i;
 			break;
 		}
 	}
 	if (xact >= 0 && atomic_swap_int(&cc[xact].locked, 1) == 0) {
 		if (cc[xact].vp == vp &&
-		    ((cc[i].v_cstart ^ loffset) & ~(CLUSTER_ZONE - 1)) == 0) {
+		    rounddown2(cc[i].v_cstart ^ loffset, CLUSTER_ZONE) == 0) {
 			return(&cc[xact]);
 		}
 		atomic_swap_int(&cc[xact].locked, 0);
diff --git a/sys/net/pf/if_pfsync.c b/sys/net/pf/if_pfsync.c
index 41c2ff50fe..ff45b1b4a9 100644
--- a/sys/net/pf/if_pfsync.c
+++ b/sys/net/pf/if_pfsync.c
@@ -1255,7 +1255,7 @@ pfsync_get_mbuf(struct pfsync_softc *sc, u_int8_t action, void **sp)
 			IFNET_STAT_INC(&sc->sc_if, oerrors, 1);
 			return (NULL);
 		}
-		m->m_data += (MCLBYTES - len) &~ (sizeof(long) - 1);
+		m->m_data += rounddown2(MCLBYTES - len, sizeof(long));
 	} else
 		MH_ALIGN(m, len);
 
diff --git a/sys/platform/pc64/vmm/vmx.h b/sys/platform/pc64/vmm/vmx.h
index aac2d4c14f..fc3200bed4 100644
--- a/sys/platform/pc64/vmm/vmx.h
+++ b/sys/platform/pc64/vmm/vmx.h
@@ -155,7 +155,7 @@ void vmx_vmexit(void);
 #define 	VMX_REGION_SIZE(reg_val)	((reg_val >> 32) & 0x01fff) /* 32:44 */
 #define 	VMX_WIDTH_ADDR(reg_val)		(reg_val >> 48 & 0x1) /* 48 */
 #define		VMXON_REGION_ALIGN_SIZE		4096ULL
-#define		VMXON_REGION_ALIGN(p)		(((unsigned long long)(p) + VMXON_REGION_ALIGN_SIZE) & ~(VMXON_REGION_ALIGN_SIZE - 1))
+#define		VMXON_REGION_ALIGN(p)		rounddown2((unsigned long long)(p) + VMXON_REGION_ALIGN_SIZE, VMXON_REGION_ALIGN_SIZE)
 
 
 
diff --git a/sys/platform/pc64/x86_64/amd64_mem.c b/sys/platform/pc64/x86_64/amd64_mem.c
index 2fca20cfb9..232e25dfa9 100644
--- a/sys/platform/pc64/x86_64/amd64_mem.c
+++ b/sys/platform/pc64/x86_64/amd64_mem.c
@@ -378,7 +378,7 @@ amd64_mrstoreone(void *arg)
 		/* mask/active register */
 		if (mrd->mr_flags & MDF_ACTIVE) {
 			msrv = MTRR_PHYSMASK_VALID |
-			    (~(mrd->mr_len - 1) & mtrr_physmask);
+			    rounddown2(mtrr_physmask, mrd->mr_len);
 		} else {
 			msrv = 0;
 		}
diff --git a/sys/platform/vkernel64/platform/init.c b/sys/platform/vkernel64/platform/init.c
index de2430497e..a4c95176cc 100644
--- a/sys/platform/vkernel64/platform/init.c
+++ b/sys/platform/vkernel64/platform/init.c
@@ -1396,8 +1396,8 @@ netif_init_tap(int tap_unit, in_addr_t *addr, in_addr_t *mask, int s)
 
 		masklen = strtoul(masklen_str, NULL, 10);
 		if (masklen < 32 && masklen > 0) {
-			netmask = htonl(~((1LL << (32 - masklen)) - 1)
-					& 0xffffffff);
+			netmask =
+			    htonl(rounddown2(0xffffffff, 1LL << (32 - masklen)));
 		} else {
 			warnx("Invalid netmask len: %lu", masklen);
 			return -1;
diff --git a/sys/platform/vkernel64/platform/pmap.c b/sys/platform/vkernel64/platform/pmap.c
index c7a2bceadb..591d1ee834 100644
--- a/sys/platform/vkernel64/platform/pmap.c
+++ b/sys/platform/vkernel64/platform/pmap.c
@@ -1631,7 +1631,9 @@ pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
 		kernel_vm_end = KvaStart;
 		nkpt = 0;
 		while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & VPTE_V) != 0) {
-			kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
+			kernel_vm_end =
+			    rounddown2(kernel_vm_end + PAGE_SIZE * NPTEPG,
+				PAGE_SIZE * NPTEPG);
 			nkpt++;
 			if (kernel_vm_end - 1 >= vm_map_max(&kernel_map)) {
 				kernel_vm_end = vm_map_max(&kernel_map);
@@ -1665,8 +1667,9 @@ pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
 			continue; /* try again */
 		}
 		if ((*pde & VPTE_V) != 0) {
-			kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
-					~(PAGE_SIZE * NPTEPG - 1);
+			kernel_vm_end =
+			    rounddown2(kernel_vm_end + PAGE_SIZE * NPTEPG,
+				PAGE_SIZE * NPTEPG);
 			if (kernel_vm_end - 1 >= vm_map_max(&kernel_map)) {
 				kernel_vm_end = vm_map_max(&kernel_map);
 				break;
@@ -1694,8 +1697,9 @@ pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
 		atomic_add_long(&kernel_pmap.pm_stats.wired_count, 1);
 		nkpt++;
 
-		kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
-				~(PAGE_SIZE * NPTEPG - 1);
+		kernel_vm_end =
+		    rounddown2(kernel_vm_end + PAGE_SIZE * NPTEPG,
+			PAGE_SIZE * NPTEPG);
 		if (kernel_vm_end - 1 >= vm_map_max(&kernel_map)) {
 			kernel_vm_end = vm_map_max(&kernel_map);
 			break;
diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h
index c55792a3e6..f8006aa45c 100644
--- a/sys/sys/mbuf.h
+++ b/sys/sys/mbuf.h
@@ -408,7 +408,7 @@ struct mbstat {
  * an object of the specified size at the end of the mbuf, longword aligned.
  */
 #define	M_ALIGN(m, len) do {						\
-	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);		\
+	(m)->m_data += rounddown2(MLEN - (len), sizeof(long));		\
 } while (0)
 
 /*
@@ -416,7 +416,7 @@ struct mbstat {
  * or initialized by M_COPY_PKTHDR.
  */
 #define	MH_ALIGN(m, len) do {						\
-	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);		\
+	(m)->m_data += rounddown2(MHLEN - (len), sizeof(long));		\
 } while (0)
 
 /*
diff --git a/sys/vfs/hammer2/hammer2_chain.c b/sys/vfs/hammer2/hammer2_chain.c
index e4a1107541..0129c576c1 100644
--- a/sys/vfs/hammer2/hammer2_chain.c
+++ b/sys/vfs/hammer2/hammer2_chain.c
@@ -4208,8 +4208,7 @@ hammer2_chain_create_indirect(hammer2_chain_t *parent,
 		 * Skip keys that are not within the key/radix of the new
 		 * indirect block.  They stay in the parent.
 		 */
-		if ((~(((hammer2_key_t)1 << keybits) - 1) &
-		    (key ^ bref->key)) != 0) {
+		if (rounddown2(key ^ bref->key, (hammer2_key_t)1 << keybits) != 0) {
 			goto next_key_spinlocked;
 		}
 
@@ -4338,8 +4337,7 @@ hammer2_chain_create_indirect(hammer2_chain_t *parent,
 	/*
 	 * Figure out what to return.
 	 */
-	if (~(((hammer2_key_t)1 << keybits) - 1) &
-		   (create_key ^ key)) {
+	if (rounddown2(create_key ^ key, (hammer2_key_t)1 << keybits)) {
 		/*
 		 * Key being created is outside the key range,
 		 * return the original parent.
@@ -4870,8 +4868,7 @@ hammer2_chain_indkey_dir(hammer2_chain_t *parent, hammer2_key_t *keyp,
 			nkeybits = bref->keybits;
 		}
 		while (nkeybits < 64 &&
-		       (~(((hammer2_key_t)1 << nkeybits) - 1) &
-		        (key ^ bref->key)) != 0) {
+		       rounddown2(key ^ bref->key, (hammer2_key_t)1 << nkeybits) != 0) {
 			++nkeybits;
 		}
 
diff --git a/sys/vfs/hammer2/hammer2_disk.h b/sys/vfs/hammer2/hammer2_disk.h
index 6c83947f61..f57b94101f 100644
--- a/sys/vfs/hammer2/hammer2_disk.h
+++ b/sys/vfs/hammer2/hammer2_disk.h
@@ -390,7 +390,7 @@
  *     now we retain compatibility and make FMZONEBASE every 1GB
  */
 #define H2FMZONEBASE(key)	((key) & ~HAMMER2_FREEMAP_LEVEL1_MASK)
-#define H2FMBASE(key, radix)	((key) & ~(((hammer2_off_t)1 << (radix)) - 1))
+#define H2FMBASE(key, radix)	rounddown2(key, (hammer2_off_t)1 << (radix))
 
 /*
  * 16KB bitmap granularity (x2 bits per entry).
diff --git a/sys/vfs/isofs/cd9660/cd9660_bmap.c b/sys/vfs/isofs/cd9660/cd9660_bmap.c
index 01d6a31471..98ffd1047c 100644
--- a/sys/vfs/isofs/cd9660/cd9660_bmap.c
+++ b/sys/vfs/isofs/cd9660/cd9660_bmap.c
@@ -89,7 +89,7 @@ cd9660_bmap(struct vop_bmap_args *ap)
 		else if (nbytes > MAXBSIZE)
 			*ap->a_runp = MAXBSIZE;
 		else
-			*ap->a_runp = (int)nbytes & ~(bsize - 1);
+			*ap->a_runp = rounddown2((int)nbytes, bsize);
 	}
 
 	if (ap->a_runb) {
diff --git a/sys/vfs/isofs/cd9660/cd9660_vnops.c b/sys/vfs/isofs/cd9660/cd9660_vnops.c
index 70a33427c4..de44a52094 100644
--- a/sys/vfs/isofs/cd9660/cd9660_vnops.c
+++ b/sys/vfs/isofs/cd9660/cd9660_vnops.c
@@ -626,7 +626,7 @@ cd9660_readlink(struct vop_readlink_args *ap)
 	 * Get parents directory record block that this inode included.
 	 */
 	error = bread(imp->im_devvp,
-		      (off_t)ip->i_number & ~((1 << imp->im_bshift) - 1),
+		      rounddown2((off_t)ip->i_number, 1 << imp->im_bshift),
 		      imp->logical_block_size, &bp);
 	if (error) {
 		brelse(bp);
diff --git a/sys/vfs/nfs/nfs_vfsops.c b/sys/vfs/nfs/nfs_vfsops.c
index 3cc59046a1..6d0f94994e 100644
--- a/sys/vfs/nfs/nfs_vfsops.c
+++ b/sys/vfs/nfs/nfs_vfsops.c
@@ -503,7 +503,7 @@ nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct thread *td)
 			nmp->nm_wsize = roundup2(pref, NFS_FABLKSIZE);
 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
 		if (max < nmp->nm_wsize && max > 0) {
-			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
+			nmp->nm_wsize = rounddown2(max, NFS_FABLKSIZE);
 			if (nmp->nm_wsize == 0)
 				nmp->nm_wsize = max;
 		}
@@ -512,7 +512,7 @@ nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct thread *td)
 			nmp->nm_rsize = roundup2(pref, NFS_FABLKSIZE);
 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
 		if (max < nmp->nm_rsize && max > 0) {
-			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
+			nmp->nm_rsize = rounddown2(max, NFS_FABLKSIZE);
 			if (nmp->nm_rsize == 0)
 				nmp->nm_rsize = max;
 		}
@@ -520,7 +520,7 @@ nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct thread *td)
 		if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ)
 			nmp->nm_readdirsize = roundup2(pref, NFS_DIRBLKSIZ);
 		if (max < nmp->nm_readdirsize && max > 0) {
-			nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
+			nmp->nm_readdirsize = rounddown2(max, NFS_DIRBLKSIZ);
 			if (nmp->nm_readdirsize == 0)
 				nmp->nm_readdirsize = max;
 		}
diff --git a/sys/vfs/ntfs/ntfs_subr.c b/sys/vfs/ntfs/ntfs_subr.c
index 662c64073a..eadd97a4d9 100644
--- a/sys/vfs/ntfs/ntfs_subr.c
+++ b/sys/vfs/ntfs/ntfs_subr.c
@@ -1688,7 +1688,7 @@ ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
 		uup = kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
 			      M_WAITOK);
 
-		cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
+		cn = rounddown2(ntfs_btocn(roff), NTFS_COMPUNIT_CL);
 		off = roff - ntfs_cntob(cn);
 
 		while (left) {
diff --git a/sys/vfs/smbfs/smbfs_io.c b/sys/vfs/smbfs/smbfs_io.c
index fcee3de169..7daaf9e68f 100644
--- a/sys/vfs/smbfs/smbfs_io.c
+++ b/sys/vfs/smbfs/smbfs_io.c
@@ -489,8 +489,8 @@ smbfs_getpages(struct vop_getpages_args *ap)
 			m->valid = VM_PAGE_BITS_ALL;
 			m->dirty = 0;
 		} else {
-			int nvalid = ((size + DEV_BSIZE - 1) - toff) &
-				      ~(DEV_BSIZE - 1);
+			int nvalid = rounddown2((size + DEV_BSIZE - 1) - toff,
+			    DEV_BSIZE);
 			vm_page_set_validclean(m, 0, nvalid);
 		}
 		
diff --git a/sys/vfs/ufs/ffs_softdep.c b/sys/vfs/ufs/ffs_softdep.c
index 83a40ed648..fd4651af84 100644
--- a/sys/vfs/ufs/ffs_softdep.c
+++ b/sys/vfs/ufs/ffs_softdep.c
@@ -4875,7 +4875,7 @@ clear_inodedeps(struct thread *td)
 	/*
 	 * Find the last inode in the block with dependencies.
 	 */
-	firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
+	firstino = rounddown2(inodedep->id_ino, INOPB(fs));
 	for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
 		if (inodedep_lookup(fs, lastino, 0, &inodedep) != 0)
 			break;
diff --git a/sys/vfs/ufs/ufs_dirhash.c b/sys/vfs/ufs/ufs_dirhash.c
index 32b6613ae7..ddcbb77e9b 100644
--- a/sys/vfs/ufs/ufs_dirhash.c
+++ b/sys/vfs/ufs/ufs_dirhash.c
@@ -901,7 +901,7 @@ ufsdirhash_getprev(struct direct *dirp, doff_t offset)
 	doff_t blkoff, prevoff;
 	int entrypos, i;
 
-	blkoff = offset & ~(DIRBLKSIZ - 1);	/* offset of start of block */
+	blkoff = rounddown2(offset, DIRBLKSIZ);	/* offset of start of block */
 	entrypos = offset & (DIRBLKSIZ - 1);	/* entry relative to block */
 	blkbuf = (char *)dirp - entrypos;
 	prevoff = blkoff;
diff --git a/sys/vfs/ufs/ufs_lookup.c b/sys/vfs/ufs/ufs_lookup.c
index 3d493b10c1..78067a9e92 100644
--- a/sys/vfs/ufs/ufs_lookup.c
+++ b/sys/vfs/ufs/ufs_lookup.c
@@ -444,7 +444,7 @@ ufs_lookup(struct vop_old_lookup_args *ap)
 	 * in the cache as to where the entry was found.
 	 */
 	if (nameiop == NAMEI_LOOKUP)
-		dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
+		dp->i_diroff = rounddown2(dp->i_offset, DIRBLKSIZ);
 
 	/*
 	 * If deleting, and at end of pathname, return
@@ -874,7 +874,7 @@ ufs_direnter(struct vnode *dvp, struct vnode *tvp, struct direct *dirp,
 	if (dp->i_dirhash != NULL)
 		ufsdirhash_checkblock(dp, dirbuf -
 		    (dp->i_offset & (DIRBLKSIZ - 1)),
-		    dp->i_offset & ~(DIRBLKSIZ - 1));
+		    rounddown2(dp->i_offset, DIRBLKSIZ));
 #endif
 
 	if (DOINGSOFTDEP(dvp)) {
@@ -972,7 +972,7 @@ ufs_dirremove(struct vnode *dvp, struct inode *ip, int flags, int isrmdir)
 	if (dp->i_dirhash != NULL)
 		ufsdirhash_checkblock(dp, (char *)ep -
 		    ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)),
-		    dp->i_offset & ~(DIRBLKSIZ - 1));
+		    rounddown2(dp->i_offset, DIRBLKSIZ));
 #endif
 out:
 	if (DOINGSOFTDEP(dvp)) {
diff --git a/sys/vm/vm_contig.c b/sys/vm/vm_contig.c
index ec7d6c9d96..2ab8f6d061 100644
--- a/sys/vm/vm_contig.c
+++ b/sys/vm/vm_contig.c
@@ -307,8 +307,7 @@ vm_contig_pg_alloc(unsigned long size, vm_paddr_t low, vm_paddr_t high,
 			if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
 			    (phys >= low) && (phys < high) &&
 			    ((phys & (alignment - 1)) == 0) &&
-			    (((phys ^ (phys + size - 1)) & /* bitwise and */
-			     ~(boundary - 1)) == 0) &&
+			    ((rounddown2(phys ^ (phys + size - 1), boundary)) == 0) &&
 			    m->wire_count == 0 && m->hold_count == 0 &&
 			    (m->busy_count &
 			     (PBUSY_LOCKED | PBUSY_MASK)) == 0 &&
diff --git a/sys/vm/vm_page.c b/sys/vm/vm_page.c
index a88ed90dab..0105379fea 100644
--- a/sys/vm/vm_page.c
+++ b/sys/vm/vm_page.c
@@ -3502,7 +3502,7 @@ _vm_page_zero_valid(vm_page_t m, int base, int size)
 	 * first block.
 	 */
 
-	if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
+	if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
 	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0
 	) {
 		pmap_zero_page_area(
@@ -3520,7 +3520,7 @@ _vm_page_zero_valid(vm_page_t m, int base, int size)
 
 	endoff = base + size;
 
-	if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
+	if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
 	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0
 	) {
 		pmap_zero_page_area(
diff --git a/usr.bin/colrm/colrm.c b/usr.bin/colrm/colrm.c
index bc9ea15f15..870d19b297 100644
--- a/usr.bin/colrm/colrm.c
+++ b/usr.bin/colrm/colrm.c
@@ -32,7 +32,7 @@
  * @(#)colrm.c	8.2 (Berkeley) 5/4/95
  */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <err.h>
 #include <errno.h>
 #include <limits.h>
@@ -100,7 +100,7 @@ main(int argc, char *argv[])
 			column = 0;
 			break;
 		case '\t':
-			column = (column + TAB) & ~(TAB - 1);
+			column = rounddown2(column + TAB, TAB);
 			break;
 		default:
 			if ((width = wcwidth(ch)) > 0)
diff --git a/usr.bin/tail/misc.c b/usr.bin/tail/misc.c
index dd1bcd1d63..202e9bc0ce 100644
--- a/usr.bin/tail/misc.c
+++ b/usr.bin/tail/misc.c
@@ -31,10 +31,9 @@
  *
  * @(#)misc.c	8.1 (Berkeley) 6/6/93
  * $FreeBSD: src/usr.bin/tail/misc.c,v 1.4.8.2 2001/04/18 09:32:08 dwmalone Exp $
- * $DragonFly: src/usr.bin/tail/misc.c,v 1.3 2003/10/04 20:36:51 hmp Exp $
  */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 #include <errno.h>
@@ -95,7 +94,7 @@ maparound(struct mapinfo *mip, off_t offset)
 	if (mip->start != NULL && munmap(mip->start, mip->maplen) != 0)
 		return (1);
 
-	mip->mapoff = offset & ~((off_t)TAILMAPLEN - 1);
+	mip->mapoff = rounddown2(offset, (off_t)TAILMAPLEN);
 	mip->maplen = TAILMAPLEN;
 	if (mip->maplen > (uintmax_t)(mip->maxoff - mip->mapoff))
 		mip->maplen = mip->maxoff - mip->mapoff;
-- 
2.23.0

