From 865a107cdf59f4dc720e3770657d767f242ac5a4 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Wed, 23 Oct 2019 13:15:11 +0200
Subject: [PATCH 3/4] world/kernel: Use the rounddown() macro in various
 places.

---
 bin/mined/mined.h                               |  5 ++---
 bin/pax/buf_subs.c                              |  7 +++----
 games/adventure/hdr.h                           |  2 +-
 games/adventure/subr.c                          |  2 +-
 games/larn/store.c                              |  8 +++++---
 gnu/usr.bin/rcs/lib/maketime.c                  |  4 ++--
 lib/libc/rpc/clnt_dg.c                          |  6 +++---
 lib/libc/rpc/svc_dg.c                           |  2 +-
 lib/libevtr/evtr.c                              |  3 ++-
 lib/libtcplay/io.c                              |  2 +-
 lib/libtelnet/pk.c                              |  3 ++-
 sbin/dhclient/dhclient.c                        |  2 +-
 sbin/dump/main.c                                |  2 +-
 sbin/fdisk/fdisk.c                              | 14 +++++++-------
 sbin/ffsinfo/ffsinfo.c                          |  2 +-
 sbin/fsck/inode.c                               |  2 +-
 sbin/growfs/growfs.c                            |  2 +-
 sys/ddb/db_output.c                             |  3 +--
 sys/dev/acpica/acpi_hp/acpi_hp.c                |  2 +-
 sys/dev/disk/nata/ata-raid.c                    |  4 ++--
 sys/dev/disk/nata/atapi-cd.c                    |  2 +-
 sys/dev/misc/syscons/scmouse.c                  |  8 +++-----
 sys/dev/netif/ath/ath_hal/ar5211/ar5211_reset.c |  2 +-
 sys/dev/netif/ath/ath_hal/ar5212/ar5111.c       |  3 +--
 sys/dev/raid/asr/i2odep.h                       |  2 +-
 sys/dev/raid/mrsas/mrsas.c                      |  2 +-
 sys/dev/sound/pci/hda/hdaa.c                    |  2 +-
 sys/dev/sound/pcm/feeder_eq.c                   |  5 ++---
 sys/dev/video/fb/fb.c                           |  2 +-
 sys/dev/video/fb/vga.c                          |  2 +-
 sys/kern/kern_proc.c                            |  3 +--
 sys/libkern/icrc32.c                            |  6 ++----
 sys/netinet/tcp_input.c                         |  2 +-
 sys/netinet/tcp_subr.c                          |  2 +-
 sys/vfs/devfs/devfs_vnops.c                     |  2 +-
 test/stress/fsx/fsx.c                           |  6 ++----
 usr.bin/at/parsetime.c                          |  4 ++--
 usr.sbin/mrouted/igmpv2.h                       |  3 +--
 usr.sbin/quot/quot.c                            |  4 ++--
 usr.sbin/rpcbind/rpcb_svc_com.c                 |  2 +-
 40 files changed, 66 insertions(+), 75 deletions(-)

diff --git a/bin/mined/mined.h b/bin/mined/mined.h
index 688941ba66..53dc6de320 100644
--- a/bin/mined/mined.h
+++ b/bin/mined/mined.h
@@ -43,7 +43,7 @@
 #define ASSUME_CONS25
 #define ASSUME_XTERM
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -78,8 +78,7 @@ extern const char *pos_string;		/* Absolute cursor positioning */
 #define MAX_CHARS	1024		/* Maximum chars on one line */
 
 /* LINE_START must be rounded up to the lowest SHIFT_SIZE */
-#define LINE_START	(((-MAX_CHARS - 1) / SHIFT_SIZE) * SHIFT_SIZE \
-  				   - SHIFT_SIZE)
+#define LINE_START	(rounddown(-MAX_CHARS - 1, SHIFT_SIZE) - SHIFT_SIZE)
 #define LINE_END	(MAX_CHARS + 1)	/* Highest x-coordinate for line */
 
 #define LINE_LEN	(XMAX + 1)	/* Number of characters on line */
diff --git a/bin/pax/buf_subs.c b/bin/pax/buf_subs.c
index bc8082c49f..c0f522fab7 100644
--- a/bin/pax/buf_subs.c
+++ b/bin/pax/buf_subs.c
@@ -32,10 +32,9 @@
  *
  * @(#)buf_subs.c	8.2 (Berkeley) 4/18/94
  * $FreeBSD: src/bin/pax/buf_subs.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $
- * $DragonFly: src/bin/pax/buf_subs.c,v 1.6 2006/09/27 21:58:08 pavalos Exp $
  */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <unistd.h>
@@ -234,7 +233,7 @@ appnd_start(off_t skcnt)
 	 * travel skcnt + padding ROUNDED UP to blksize.
 	 */
 	skcnt += bufend - bufpt;
-	if ((cnt = (skcnt/blksz) * blksz) < skcnt)
+	if ((cnt = rounddown(skcnt, blksz)) < skcnt)
 		cnt += blksz;
 	if (ar_rev((off_t)cnt) < 0)
 		goto out;
@@ -414,7 +413,7 @@ rd_skip(off_t skcnt)
 	 * based on rdblksz. we skip over "cnt" complete records
 	 */
 	res = skcnt%rdblksz;
-	cnt = (skcnt/rdblksz) * rdblksz;
+	cnt = rounddown(skcnt, rdblksz);
 
 	/*
 	 * if the skip fails, we will have to resync. ar_fow will tell us
diff --git a/games/adventure/hdr.h b/games/adventure/hdr.h
index 740141f3bf..8bfa57676a 100644
--- a/games/adventure/hdr.h
+++ b/games/adventure/hdr.h
@@ -53,7 +53,7 @@
  */
 
 /* hdr.h: included by c advent files */
-#include <sys/types.h>
+#include <sys/param.h>
 #include <signal.h>
 
 int datfd;			/* message file descriptor */
diff --git a/games/adventure/subr.c b/games/adventure/subr.c
index 65db839ecb..09be463c57 100644
--- a/games/adventure/subr.c
+++ b/games/adventure/subr.c
@@ -985,7 +985,7 @@ trfill(void)
 		spk = 105;
 	if (spk != 107)
 		return (2011);
-	prop[bottle] = ((cond[loc] % 4) / 2) * 2;
+	prop[bottle] = rounddown(cond[loc] % 4, 2);
 	k = liq();
 	if (toting(bottle))
 		place[k] = -1;
diff --git a/games/larn/store.c b/games/larn/store.c
index 6867ef0ef5..1e7bcee406 100644
--- a/games/larn/store.c
+++ b/games/larn/store.c
@@ -28,10 +28,11 @@
  *
  * @(#)store.c	5.4 (Berkeley) 5/13/91
  * $FreeBSD: src/games/larn/store.c,v 1.5 1999/11/30 03:49:00 billf Exp $
- * $DragonFly: src/games/larn/store.c,v 1.4 2006/08/26 17:05:05 pavalos Exp $
  */
 
 /* store.c		Larn is copyrighted 1986 by Noah Morgan. */
+#include <sys/param.h>
+
 #include "header.h"
 
 static void dnd_2hed(void);
@@ -547,7 +548,7 @@ ointerest(void)
 		if (c[BANKACCOUNT] > 500000)
 			c[BANKACCOUNT] = 500000;	/* interest limit */
 	}
-	lasttime = (gtime / 100) * 100;
+	lasttime = rounddown(gtime, 100);
 }
 
 static short gemorder[26] = { 0 };	/* the reference to screen location for each */
@@ -569,7 +570,8 @@ obanksub(void)
 		case OSAPPHIRE:
 
 			if (iven[i] == OLARNEYE) {
-				gemvalue[i] = 250000 - ((gtime * 7) / 100) * 100;
+				gemvalue[i] = 250000 -
+				    rounddown(gtime * 7, 100);
 				if (gemvalue[i] < 50000)
 					gemvalue[i] = 50000;
 			} else
diff --git a/gnu/usr.bin/rcs/lib/maketime.c b/gnu/usr.bin/rcs/lib/maketime.c
index 8146b9f09d..d94c604c92 100644
--- a/gnu/usr.bin/rcs/lib/maketime.c
+++ b/gnu/usr.bin/rcs/lib/maketime.c
@@ -27,7 +27,6 @@ Report problems and direct all questions to:
 
 /*
  * $FreeBSD: src/gnu/usr.bin/rcs/lib/maketime.c,v 1.7 1999/08/27 23:36:43 peter Exp $
- * $DragonFly: src/gnu/usr.bin/rcs/lib/maketime.c,v 1.2 2003/06/17 04:25:47 dillon Exp $
  */
 
 #if has_conf_h
@@ -39,6 +38,7 @@ Report problems and direct all questions to:
 #		define const
 #		define P(x) ()
 #	endif
+#	include <sys/param.h>
 #	include <stdlib.h>
 #	include <time.h>
 #endif
@@ -277,7 +277,7 @@ maketime(pt, default_time)
 
 	if (TM_DEFINED(pt->ymodulus))
 	    tm.tm_year +=
-		(tm0->tm_year + TM_YEAR_ORIGIN)/pt->ymodulus * pt->ymodulus;
+		rounddown(tm0->tm_year + TM_YEAR_ORIGIN, pt->ymodulus);
 	else if (!TM_DEFINED(tm.tm_year)) {
 	    /* Set default year, month, day from current time.  */
 	    tm.tm_year = tm0->tm_year + TM_YEAR_ORIGIN;
diff --git a/lib/libc/rpc/clnt_dg.c b/lib/libc/rpc/clnt_dg.c
index 2a4f0aee70..d9c1286a34 100644
--- a/lib/libc/rpc/clnt_dg.c
+++ b/lib/libc/rpc/clnt_dg.c
@@ -39,7 +39,7 @@
 
 #include "namespace.h"
 #include "reentrant.h"
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/event.h>
 #include <sys/time.h>
 #include <sys/socket.h>
@@ -215,8 +215,8 @@ clnt_dg_create(int fd,			/* open file descriptor */
 	/*
 	 * Should be multiple of 4 for XDR.
 	 */
-	sendsz = ((sendsz + 3) / 4) * 4;
-	recvsz = ((recvsz + 3) / 4) * 4;
+	sendsz = rounddown(sendsz + 3, 4);
+	recvsz = rounddown(recvsz + 3, 4);
 	cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
 	if (cu == NULL)
 		goto err1;
diff --git a/lib/libc/rpc/svc_dg.c b/lib/libc/rpc/svc_dg.c
index 763b5182e3..efd4e592e4 100644
--- a/lib/libc/rpc/svc_dg.c
+++ b/lib/libc/rpc/svc_dg.c
@@ -125,7 +125,7 @@ svc_dg_create(int fd, u_int sendsize, u_int recvsize)
 	su = mem_alloc(sizeof (*su));
 	if (su == NULL)
 		goto freedata;
-	su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
+	su->su_iosz = rounddown(MAX(sendsize, recvsize) + 3, 4);
 	if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
 		goto freedata;
 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
diff --git a/lib/libevtr/evtr.c b/lib/libevtr/evtr.c
index a4ea7a4f91..5e6daf4efe 100644
--- a/lib/libevtr/evtr.c
+++ b/lib/libevtr/evtr.c
@@ -38,6 +38,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/stat.h>
 #include <sys/tree.h>
@@ -1872,7 +1873,7 @@ evtr_last_event(evtr_t evtr, evtr_event_t ev)
 		 * location that we are sure contains a header so we can
 		 * pick up the stream.
 		 */
-		last_boundary = (st.st_size / REC_BOUNDARY) * REC_BOUNDARY;
+		last_boundary = rounddown(st.st_size, REC_BOUNDARY);
 		/* XXX: ->bytes should be in query */
 		assert(evtr->bytes == 0);
 		evtr_skip(evtr, last_boundary);
diff --git a/lib/libtcplay/io.c b/lib/libtcplay/io.c
index ebac5ac45e..bc8a4d4048 100644
--- a/lib/libtcplay/io.c
+++ b/lib/libtcplay/io.c
@@ -315,7 +315,7 @@ write_to_disk(const char *dev, off_t offset, size_t blksz, void *mem,
 	printf("offset: %"PRIu64", internal offset: %"PRIu64"\n",
 	    (uint64_t)offset, (uint64_t)internal_off);
 #endif
-	offset = (offset/blksz) * blksz;
+	offset = rounddown(offset, blksz);
 
 	if ((internal_off + bytes) > blksz) {
 		tc_log(1, "This should never happen: internal_off + bytes > "
diff --git a/lib/libtelnet/pk.c b/lib/libtelnet/pk.c
index c106e46d10..8ef55f1906 100644
--- a/lib/libtelnet/pk.c
+++ b/lib/libtelnet/pk.c
@@ -41,6 +41,7 @@
 	char secret[HEXKEYBYTES + 1];
  */
 
+#include <sys/param.h>
 #include <sys/time.h>
 #include <err.h>
 #include <fcntl.h>
@@ -277,7 +278,7 @@ pk_encode(char *in, char *out, DesData *key)
 
 	memset(&i,0,sizeof(i));
 	memset(buf,0,sizeof(buf));
-	deslen = ((strlen(in) + 7)/8)*8;
+	deslen = rounddown(strlen(in) + 7, 8);
 	DES_key_sched(key, &k);
 	DES_cbc_encrypt(in,buf,deslen, &k,&i,DES_ENCRYPT);
 	for (l=0,op=0;l<deslen;l++) {
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c
index 3e2a90ecb6..06a5408a76 100644
--- a/sbin/dhclient/dhclient.c
+++ b/sbin/dhclient/dhclient.c
@@ -2099,7 +2099,7 @@ check_classless_option(unsigned char *data, int len)
 		if ((addr & mask) != addr) {
 			addr &= mask;
 			data[i - 1] = (unsigned char)(
-				(addr >> (((32 - width)/8)*8)) & 0xFF);
+				(addr >> rounddown(32 - width, 8)) & 0xFF);
 		}
 		i += 4;
 	}
diff --git a/sbin/dump/main.c b/sbin/dump/main.c
index 9d128d163f..5134c04bb2 100644
--- a/sbin/dump/main.c
+++ b/sbin/dump/main.c
@@ -224,7 +224,7 @@ main(int argc, char **argv)
 	}
 
 	if (blocksperfile)
-		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
+		blocksperfile = rounddown(blocksperfile, ntrec); /* round down */
 	else if (!unlimited) {
 		/*
 		 * Determine how to default tape size and density
diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c
index 57a52dbaa6..f313102dde 100644
--- a/sbin/fdisk/fdisk.c
+++ b/sbin/fdisk/fdisk.c
@@ -372,8 +372,8 @@ main(int argc, char *argv[])
 			printf("Warning: Ending logical block > 2TB, using max value\n");
 			partp->dp_size = 0xFFFFFFFFU;
 		} else {
-			partp->dp_size = (disksecs / dos_cylsecs) *
-					dos_cylsecs - dos_sectors;
+			partp->dp_size =
+			    rounddown(disksecs, dos_cylsecs) - dos_sectors;
 		}
 		dos(partp);
 		if (v_flag)
@@ -571,7 +571,7 @@ struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
 		printf("Warning: Ending logical block > 2TB, using max value\n");
 		partp->dp_size = 0xFFFFFFFFU;
 	} else {
-		partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
+		partp->dp_size = rounddown(disksecs, dos_cylsecs) - start;
 	}
 
 	dos(partp);
@@ -1254,7 +1254,7 @@ process_partition(CMD *command)
 	 */
 	if (partp->dp_start % dos_sectors != 0)
 	{
-	    prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
+	    prev_head_boundary = rounddown(partp->dp_start, dos_sectors);
 	    if (max_end < (uint32_t)dos_sectors ||
 		prev_head_boundary > max_end - dos_sectors)
 	    {
@@ -1280,7 +1280,7 @@ process_partition(CMD *command)
 	 * boundary.
 	 */
 	prev_cyl_boundary =
-	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
+	    rounddown(partp->dp_start + partp->dp_size, dos_cylsecs);
 	if (prev_cyl_boundary > partp->dp_start)
 	    adj_size = prev_cyl_boundary - partp->dp_start;
 	else
@@ -1492,7 +1492,7 @@ sanitize_partition(struct dos_partition *partp)
      * Adjust start upwards, if necessary, to fall on an head boundary.
      */
     if (start % dos_sectors != 0) {
-	prev_head_boundary = start / dos_sectors * dos_sectors;
+	prev_head_boundary = rounddown(start, dos_sectors);
 	if (max_end < (uint32_t)dos_sectors ||
 	    prev_head_boundary >= max_end - dos_sectors) {
 	    /*
@@ -1509,7 +1509,7 @@ sanitize_partition(struct dos_partition *partp)
      * Adjust size downwards, if necessary, to fall on a cylinder
      * boundary.
      */
-    prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
+    prev_cyl_boundary = rounddown(start + size, dos_cylsecs);
     if (prev_cyl_boundary > start)
 	size = prev_cyl_boundary - start;
     else {
diff --git a/sbin/ffsinfo/ffsinfo.c b/sbin/ffsinfo/ffsinfo.c
index 39b1e04751..20fb1a8247 100644
--- a/sbin/ffsinfo/ffsinfo.c
+++ b/sbin/ffsinfo/ffsinfo.c
@@ -578,7 +578,7 @@ ginode(ino_t inumber, int fsi)
 		iblk = ino_to_fsba(&sblock, inumber);
 		rdfs(fsbtodb(&sblock, iblk), (size_t)sblock.fs_bsize,
 		    &ablk, fsi);
-		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
+		startinum = rounddown(inumber, INOPB(&sblock));
 	}
 
 	DBG_LEAVE;
diff --git a/sbin/fsck/inode.c b/sbin/fsck/inode.c
index 92bd26a186..f19d0d7920 100644
--- a/sbin/fsck/inode.c
+++ b/sbin/fsck/inode.c
@@ -277,7 +277,7 @@ ginode(ufs1_ino_t inumber)
 		if (pbp != 0)
 			pbp->b_flags &= ~B_INUSE;
 		pbp = getdatablk(iblk, sblock.fs_bsize);
-		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
+		startinum = rounddown(inumber, INOPB(&sblock));
 	}
 	return (&pbp->b_un.b_dinode[inumber % INOPB(&sblock)]);
 }
diff --git a/sbin/growfs/growfs.c b/sbin/growfs/growfs.c
index e31341d3c9..adbd66e40a 100644
--- a/sbin/growfs/growfs.c
+++ b/sbin/growfs/growfs.c
@@ -1851,7 +1851,7 @@ ginode(ino_t inumber, int fsi, int cg)
 		iblk = ino_to_fsba(&sblock, inumber);
 		in_src=fsbtodb(&sblock, iblk);
 		rdfs(in_src, (size_t)sblock.fs_bsize, &ablk, fsi);
-		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
+		startinum = rounddown(inumber, INOPB(&sblock));
 	}
 
 	DBG_LEAVE;
diff --git a/sys/ddb/db_output.c b/sys/ddb/db_output.c
index d1c36a65f2..c356f9c66e 100644
--- a/sys/ddb/db_output.c
+++ b/sys/ddb/db_output.c
@@ -62,8 +62,7 @@
 static int	db_output_position = 0;		/* output column */
 static int	db_last_non_space = 0;		/* last non-space character */
 db_expr_t	db_tab_stop_width = 8;		/* how wide are tab stops? */
-#define	NEXT_TAB(i) \
-	((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width)
+#define	NEXT_TAB(i)	rounddown((i) + db_tab_stop_width, db_tab_stop_width)
 db_expr_t	db_max_width = 79;		/* output line width */
 
 static void db_putchar (int c, void *arg);
diff --git a/sys/dev/acpica/acpi_hp/acpi_hp.c b/sys/dev/acpica/acpi_hp/acpi_hp.c
index 8a44a4424a..d992a3fff1 100644
--- a/sys/dev/acpica/acpi_hp/acpi_hp.c
+++ b/sys/dev/acpica/acpi_hp/acpi_hp.c
@@ -1046,7 +1046,7 @@ acpi_hp_hex_decode(char* buffer)
 	UINT8	*uin;
 	UINT8	uout;
 
-	if (((int)length/2)*2 == length || length < 10) return;
+	if (rounddown((int)length, 2) == length || length < 10) return;
 
 	for (i = 0; i<length; ++i) {
 		if (!((i+1)%3)) {
diff --git a/sys/dev/disk/nata/ata-raid.c b/sys/dev/disk/nata/ata-raid.c
index 2490a19818..607f7ed637 100644
--- a/sys/dev/disk/nata/ata-raid.c
+++ b/sys/dev/disk/nata/ata-raid.c
@@ -154,8 +154,8 @@ ata_raid_attach(struct ar_softc *rdp, int writeback)
     /* sanitize arrays total_size % (width * interleave) == 0 */
     if (rdp->type == AR_T_RAID0 || rdp->type == AR_T_RAID01 ||
 	rdp->type == AR_T_RAID5) {
-	rdp->total_sectors = (rdp->total_sectors/(rdp->interleave*rdp->width))*
-			     (rdp->interleave * rdp->width);
+	rdp->total_sectors = rounddown(rdp->total_sectors,
+	    rdp->interleave * rdp->width);
 	ksprintf(buffer, " (stripe %d KB)",
 		(rdp->interleave * DEV_BSIZE) / 1024);
     }
diff --git a/sys/dev/disk/nata/atapi-cd.c b/sys/dev/disk/nata/atapi-cd.c
index 2d18563f94..c70b7f2249 100644
--- a/sys/dev/disk/nata/atapi-cd.c
+++ b/sys/dev/disk/nata/atapi-cd.c
@@ -948,7 +948,7 @@ acd_set_ioparm(device_t dev)
     else
 	cdp->iomax = min(MAXPHYS, 65534);
 
-    cdp->cdev->si_iosize_max = (cdp->iomax / cdp->block_size) * cdp->block_size;
+    cdp->cdev->si_iosize_max = rounddown(cdp->iomax, cdp->block_size);
     cdp->cdev->si_bsize_phys = cdp->block_size;
     bzero(&info, sizeof(info));
     info.d_media_blksize = cdp->block_size;
diff --git a/sys/dev/misc/syscons/scmouse.c b/sys/dev/misc/syscons/scmouse.c
index 238fe547bd..6e95433017 100644
--- a/sys/dev/misc/syscons/scmouse.c
+++ b/sys/dev/misc/syscons/scmouse.c
@@ -382,8 +382,7 @@ mouse_cut_start(scr_stat *scp)
 	    /* if the pointer is on trailing blank chars, mark towards eol */
 	    i = skip_spc_left(scp, scp->mouse_pos) + 1;
 	    crit_enter();
-	    scp->mouse_cut_start =
-	        (scp->mouse_pos / scp->xsize) * scp->xsize + i;
+	    scp->mouse_cut_start = rounddown(scp->mouse_pos, scp->xsize) + i;
 	    scp->mouse_cut_end =
 	        (scp->mouse_pos / scp->xsize + 1) * scp->xsize - 1;
 	    crit_exit();
@@ -442,7 +441,7 @@ mouse_cut_word(scr_stat *scp)
 	scp->mouse_cut_end = -1;
 	crit_exit();
 
-	sol = (scp->mouse_pos / scp->xsize) * scp->xsize;
+	sol = rounddown(scp->mouse_pos, scp->xsize);
 	eol = sol + scp->xsize;
 	c = sc_vtb_getc(&scp->vtb, scp->mouse_pos);
 	if (IS_SPACE_CHAR(c)) {
@@ -510,8 +509,7 @@ mouse_cut_line(scr_stat *scp)
 	}
 
 	/* mark the entire line */
-	scp->mouse_cut_start =
-	    (scp->mouse_pos / scp->xsize) * scp->xsize;
+	scp->mouse_cut_start = rounddown(scp->mouse_pos, scp->xsize);
 	scp->mouse_cut_end = scp->mouse_cut_start + scp->xsize - 1;
 	mark_for_update(scp, scp->mouse_cut_start);
 	mark_for_update(scp, scp->mouse_cut_end);
diff --git a/sys/dev/netif/ath/ath_hal/ar5211/ar5211_reset.c b/sys/dev/netif/ath/ath_hal/ar5211/ar5211_reset.c
index ce4f9c0bd1..8a4f34f594 100644
--- a/sys/dev/netif/ath/ath_hal/ar5211/ar5211_reset.c
+++ b/sys/dev/netif/ath/ath_hal/ar5211/ar5211_reset.c
@@ -1442,7 +1442,7 @@ ar5211SetPowerTable(struct ath_hal *ah, PCDACS_EEPROM *pSrcStruct,
 		}
 
 	/* Find the first power level with a pcdac */
-	pwr = (uint16_t)(PWR_STEP * ((minScaledPwr - PWR_MIN + PWR_STEP / 2) / PWR_STEP)  + PWR_MIN);
+	pwr = (uint16_t)(rounddown(minScaledPwr - PWR_MIN + PWR_STEP / 2, PWR_STEP)  + PWR_MIN);
 
 	/* Write all the first pcdac entries based off the pcdacMin */
 	pcdacTableIndex = 0;
diff --git a/sys/dev/netif/ath/ath_hal/ar5212/ar5111.c b/sys/dev/netif/ath/ath_hal/ar5212/ar5111.c
index 598d84bee4..611831fc13 100644
--- a/sys/dev/netif/ath/ath_hal/ar5212/ar5111.c
+++ b/sys/dev/netif/ath/ath_hal/ar5212/ar5111.c
@@ -489,8 +489,7 @@ ar5111SetPowerTable(struct ath_hal *ah,
 		}
 
 	/* Find the first power level with a pcdac */
-	pwr = (uint16_t)(PWR_STEP *
-		((minScaledPwr - PWR_MIN + PWR_STEP / 2) / PWR_STEP) + PWR_MIN);
+	pwr = (uint16_t)(rounddown(minScaledPwr - PWR_MIN + PWR_STEP / 2, PWR_STEP) + PWR_MIN);
 
 	/* Write all the first pcdac entries based off the pcdacMin */
 	pcdacTableIndex = 0;
diff --git a/sys/dev/raid/asr/i2odep.h b/sys/dev/raid/asr/i2odep.h
index 299e35e15a..96e38b117d 100644
--- a/sys/dev/raid/asr/i2odep.h
+++ b/sys/dev/raid/asr/i2odep.h
@@ -181,7 +181,7 @@ typedef unsigned long	       *pUINT32;
 /* Seconds 12 bits (optimized with the assumption of 12 & 12) */
 # define _F_getTID1(w,x,y)    ((*(U16 __FAR__ *)(((U8 __FAR__ *)(&((w)->x)))\
 			       + (I2O_TID_SZ/8)))\
-				>> (I2O_TID_SZ-((I2O_TID_SZ/8)*8)))
+				>> (I2O_TID_SZ-rounddown(I2O_TID_SZ, 8)))
 # define _F_setTID1(w,x,y,z)  ((*((U16 __FAR__ *)(((U8 __FAR__ *)(&((w)->x)))\
 			       + (I2O_TID_SZ/8)))) &= (0xFFFF >> I2O_TID_SZ));\
 			      ((*((U16 __FAR__ *)(((U8 __FAR__ *)(&((w)->x)))\
diff --git a/sys/dev/raid/mrsas/mrsas.c b/sys/dev/raid/mrsas/mrsas.c
index 3bd4dac170..7db5696247 100644
--- a/sys/dev/raid/mrsas/mrsas.c
+++ b/sys/dev/raid/mrsas/mrsas.c
@@ -1813,7 +1813,7 @@ int mrsas_init_adapter(struct mrsas_softc *sc)
     max_cmd = sc->max_fw_cmds;
 
     /* Determine allocation size of command frames */
-    sc->reply_q_depth = ((max_cmd *2 +1 +15)/16*16);
+    sc->reply_q_depth = rounddown(max_cmd * 2 + 1 + 15, 16);
     sc->request_alloc_sz = sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * max_cmd;
     sc->reply_alloc_sz = sizeof(MPI2_REPLY_DESCRIPTORS_UNION) * (sc->reply_q_depth);
     sc->io_frames_alloc_sz = MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE + (MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * (max_cmd + 1));
diff --git a/sys/dev/sound/pci/hda/hdaa.c b/sys/dev/sound/pci/hda/hdaa.c
index 48bb46e90e..fdf4e98c98 100644
--- a/sys/dev/sound/pci/hda/hdaa.c
+++ b/sys/dev/sound/pci/hda/hdaa.c
@@ -1815,7 +1815,7 @@ hdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
 	/* First look for equal or multiple frequency. */
 	for (i = 0; ch->pcmrates[i] != 0; i++) {
 		spd = ch->pcmrates[i];
-		if (speed != 0 && spd / speed * speed == spd) {
+		if (speed != 0 && rounddown(spd, speed) == spd) {
 			ch->spd = spd;
 			return (spd);
 		}
diff --git a/sys/dev/sound/pcm/feeder_eq.c b/sys/dev/sound/pcm/feeder_eq.c
index e893d2bc6f..6655c85b6d 100644
--- a/sys/dev/sound/pcm/feeder_eq.c
+++ b/sys/dev/sound/pcm/feeder_eq.c
@@ -70,7 +70,7 @@ SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder_eq.c 267992 2014-06-28
 
 #define FEEDEQ_IF2PREAMP(i, f)						\
 	((abs(i) << FEEDEQ_GAIN_SHIFT) |				\
-	(((abs(f) / FEEDEQ_GAIN_STEP) * FEEDEQ_GAIN_STEP) &		\
+	(rounddown(abs(f), FEEDEQ_GAIN_STEP) &				\
 	FEEDEQ_GAIN_FMASK))
 
 #define FEEDEQ_PREAMP_MIN						\
@@ -692,8 +692,7 @@ feeder_eq_initsys(device_t dev)
 	(void)ksnprintf(buf, sizeof(buf), "Bass/Treble Equalizer Preamp "
 	    "(-/+ %d.0dB , %d.%ddB step)",
 	    FEEDEQ_GAIN_MAX, FEEDEQ_GAIN_STEP / FEEDEQ_GAIN_DIV,
-	    FEEDEQ_GAIN_STEP - ((FEEDEQ_GAIN_STEP / FEEDEQ_GAIN_DIV) *
-	    FEEDEQ_GAIN_DIV));
+	    FEEDEQ_GAIN_STEP - rounddown(FEEDEQ_GAIN_STEP, FEEDEQ_GAIN_DIV));
 
 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
diff --git a/sys/dev/video/fb/fb.c b/sys/dev/video/fb/fb.c
index 73b67afaba..91ca87427a 100644
--- a/sys/dev/video/fb/fb.c
+++ b/sys/dev/video/fb/fb.c
@@ -87,7 +87,7 @@ vid_realloc_array(void)
 		return ENOMEM;
 
 	crit_enter();
-	newsize = ((adapters + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
+	newsize = rounddown(adapters + ARRAY_DELTA, ARRAY_DELTA);
 	new_adp = kmalloc(sizeof(*new_adp)*newsize, M_DEVBUF, M_WAITOK | M_ZERO);
 	new_vidsw = kmalloc(sizeof(*new_vidsw)*newsize, M_DEVBUF,
 	    M_WAITOK | M_ZERO);
diff --git a/sys/dev/video/fb/vga.c b/sys/dev/video/fb/vga.c
index b5d8b31951..1355ba3f55 100644
--- a/sys/dev/video/fb/vga.c
+++ b/sys/dev/video/fb/vga.c
@@ -735,7 +735,7 @@ set_line_length(video_adapter_t *adp, int pixel)
 	break;
     case V_INFO_MM_PACKED:
 	count = (pixel + 7)/8;
-	bpl = ((pixel + 7)/8)*8;
+	bpl = rounddown(pixel + 7, 8);
 	break;
     case V_INFO_MM_TEXT:
 	count = (pixel + 7)/8;			/* columns */
diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c
index e41cfbc391..f5fc9da8c4 100644
--- a/sys/kern/kern_proc.c
+++ b/sys/kern/kern_proc.c
@@ -74,8 +74,7 @@
 #define PIDDOM_COUNT	10	/* 10 pids per domain - reduce array size */
 #define PIDDOM_DELAY	10	/* min 10 seconds after exit before reuse */
 #define PIDDOM_SCALE	10	/* (10,000*SCALE)/sec performance guarantee */
-#define PIDSEL_DOMAINS	(PID_MAX * PIDDOM_SCALE / PIDDOM_COUNT /	\
-			 ALLPROC_HSIZE * ALLPROC_HSIZE)
+#define PIDSEL_DOMAINS	rounddown(PID_MAX * PIDDOM_SCALE / PIDDOM_COUNT, ALLPROC_HSIZE)
 
 /* Used by libkvm */
 int allproc_hsize = ALLPROC_HSIZE;
diff --git a/sys/libkern/icrc32.c b/sys/libkern/icrc32.c
index 3dca6e8d9b..9cc892e461 100644
--- a/sys/libkern/icrc32.c
+++ b/sys/libkern/icrc32.c
@@ -3,11 +3,9 @@
  *  code or tables extracted from it, as desired without restriction.
  */
 
-#ifdef _KERNEL
 #include <sys/param.h>
+#ifdef _KERNEL
 #include <sys/systm.h>
-#else
-#include <sys/types.h>
 #endif
 
 #ifndef _KERNEL
@@ -693,7 +691,7 @@ crc32c_sb8_64_bit(uint32_t crc,
 	uint32_t running_length;
 	uint32_t end_bytes;
 
-	running_length = ((length - init_bytes) / 8) * 8;
+	running_length = rounddown(length - init_bytes, 8);
 	end_bytes = length - init_bytes - running_length;
 
 	for (li = 0; li < init_bytes; li++)
diff --git a/sys/netinet/tcp_input.c b/sys/netinet/tcp_input.c
index be34ebeef0..9c33878dbd 100644
--- a/sys/netinet/tcp_input.c
+++ b/sys/netinet/tcp_input.c
@@ -3041,7 +3041,7 @@ tcp_rmx_mss(struct tcpcb *tp, struct rtentry *rt, int offer)
 		mss &= ~(MCLBYTES-1);
 #else
 	if (mss > MCLBYTES)
-		mss = mss / MCLBYTES * MCLBYTES;
+		mss = rounddown(mss, MCLBYTES);
 #endif
 	/*
 	 * If there's a pipesize, change the socket buffer
diff --git a/sys/netinet/tcp_subr.c b/sys/netinet/tcp_subr.c
index 97b9f37479..ac1ca4688f 100644
--- a/sys/netinet/tcp_subr.c
+++ b/sys/netinet/tcp_subr.c
@@ -1816,7 +1816,7 @@ tcp_mtudisc(struct inpcb *inp, int mtu)
 		mss &= ~(MCLBYTES - 1);	
 #else
 	if (mss > MCLBYTES)
-		mss = (mss / MCLBYTES) * MCLBYTES;
+		mss = rounddown(mss, MCLBYTES);
 #endif
 
 	if (so->so_snd.ssb_hiwat < mss)
diff --git a/sys/vfs/devfs/devfs_vnops.c b/sys/vfs/devfs/devfs_vnops.c
index 04da14c70f..9772d22d58 100644
--- a/sys/vfs/devfs/devfs_vnops.c
+++ b/sys/vfs/devfs/devfs_vnops.c
@@ -1818,7 +1818,7 @@ devfs_spec_strategy(struct vop_strategy_args *ap)
 		chunksize = vp->v_rdev->si_bsize_phys;
 	else
 		chunksize = DEV_BSIZE;
-	chunksize = maxiosize / chunksize * chunksize;
+	chunksize = rounddown(maxiosize, chunksize);
 #if SPEC_CHAIN_DEBUG & 1
 	devfs_debug(DEVFS_DEBUG_DEBUG,
 		    "spec_strategy chained I/O chunksize=%d\n",
diff --git a/test/stress/fsx/fsx.c b/test/stress/fsx/fsx.c
index 716737ca81..e5d56dcddf 100644
--- a/test/stress/fsx/fsx.c
+++ b/test/stress/fsx/fsx.c
@@ -35,11 +35,9 @@
  *	Checks for mmap last-page zero fill.
  *
  * $FreeBSD: src/tools/regression/fsx/fsx.c,v 1.2 2003/04/23 23:42:23 jkh Exp $
- * $DragonFly: src/test/stress/fsx/fsx.c,v 1.2 2005/05/02 19:31:56 dillon Exp $
- *
  */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/mman.h>
@@ -233,7 +231,7 @@ logdump(void)
 	for ( ; count > 0; count--) {
 		int opnum;
 
-		opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
+		opnum = i+1 + rounddown(logcount, LOGSIZE);
 		prt("%d(%d mod 256): ", opnum, opnum%256);
 		lp = &oplog[i];
 		if ((closeopen = lp->operation < 0))
diff --git a/usr.bin/at/parsetime.c b/usr.bin/at/parsetime.c
index 56ca4a2b85..8e3f0ac2d9 100644
--- a/usr.bin/at/parsetime.c
+++ b/usr.bin/at/parsetime.c
@@ -37,7 +37,7 @@
 
 /* System Headers */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
@@ -413,7 +413,7 @@ assign_date(struct tm *tm, long mday, long mon, long year)
 		lt = localtime(&now);
 
 		/* Convert to tm_year assuming current century */
-		year += (lt->tm_year / 100) * 100;
+		year += rounddown(lt->tm_year, 100);
 
 		if (year == lt->tm_year - 1) year++;
 		else if (year < lt->tm_year)
diff --git a/usr.sbin/mrouted/igmpv2.h b/usr.sbin/mrouted/igmpv2.h
index 5f5ae27e68..26e91616de 100644
--- a/usr.sbin/mrouted/igmpv2.h
+++ b/usr.sbin/mrouted/igmpv2.h
@@ -24,8 +24,7 @@
 					IGMP_QUERY_INTERVAL + \
 					IGMP_QUERY_RESPONSE_INTERVAL / 2)
 						/* Round to the nearest TIMER_INTERVAL */
-#define	IGMP_STARTUP_QUERY_INTERVAL		(((IGMP_QUERY_INTERVAL / 4) \
-							/ TIMER_INTERVAL) * TIMER_INTERVAL)
+#define	IGMP_STARTUP_QUERY_INTERVAL		rounddown(IGMP_QUERY_INTERVAL / 4, TIMER_INTERVAL)
 #define	IGMP_STARTUP_QUERY_COUNT		IGMP_ROBUSTNESS_VARIABLE
 #define	IGMP_LAST_MEMBER_QUERY_INTERVAL		1
 #define	IGMP_LAST_MEMBER_QUERY_COUNT		IGMP_ROBUSTNESS_VARIABLE
diff --git a/usr.sbin/quot/quot.c b/usr.sbin/quot/quot.c
index 79a6ef1e14..a846c35dad 100644
--- a/usr.sbin/quot/quot.c
+++ b/usr.sbin/quot/quot.c
@@ -114,7 +114,7 @@ get_inode(int fd, struct fs *super, ino_t ino)
 		if (!ip
 		    && !(ip = (struct ufs1_dinode *)malloc(INOSZ(super))))
 			errx(1, "allocate inodes");
-		last = (ino / INOCNT(super)) * INOCNT(super);
+		last = rounddown(ino, INOCNT(super));
 		if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0
 		    || read(fd,ip,INOSZ(super)) != (ssize_t)INOSZ(super))
 			err(1, "read inodes");
@@ -374,7 +374,7 @@ dofsizes(int fd, struct fs *super, char *name)
 					errx(1, "allocate fsize structure");
 				fp->fsz_next = *fsp;
 				*fsp = fp;
-				fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
+				fp->fsz_first = rounddown(ksz, FSZCNT);
 				fp->fsz_last = fp->fsz_first + FSZCNT;
 				for (i = FSZCNT; --i >= 0;) {
 					fp->fsz_count[i] = 0;
diff --git a/usr.sbin/rpcbind/rpcb_svc_com.c b/usr.sbin/rpcbind/rpcb_svc_com.c
index d12c20bb90..edfde2779b 100644
--- a/usr.sbin/rpcbind/rpcb_svc_com.c
+++ b/usr.sbin/rpcbind/rpcb_svc_com.c
@@ -637,7 +637,7 @@ rpcbproc_callit_com(struct svc_req *rqstp, SVCXPRT *transp,
 	/*
 	 * Should be multiple of 4 for XDR.
 	 */
-	sendsz = ((sendsz + 3) / 4) * 4;
+	sendsz = rounddown(sendsz + 3, 4);
 	if (sendsz > RPC_BUF_MAX) {
 #ifdef	notyet
 		buf_alloc = alloca(sendsz);		/* not in IDR2? */
-- 
2.23.0

