world/kernel: Use the rounddown2() macro in various places.
authorSascha Wildner <saw@online.de>
Wed, 23 Oct 2019 18:09:23 +0000 (20:09 +0200)
committerSascha Wildner <saw@online.de>
Wed, 23 Oct 2019 18:09:23 +0000 (20:09 +0200)
Tested-by: zrj
45 files changed:
bin/ls/print.c
lib/libc/db/hash/hash_page.c
lib/libc/net/name6.c
lib/libc/stdio/fseek.c
lib/libc/stdlib/merge.c
lib/libc/sysvipc/shm.c
lib/libkvm/kvm_proc.c
lib/libusb/libusb20_desc.c
libexec/rtld-elf/malloc.c
sbin/gpt/map.c
sbin/ifconfig/af_inet.c
sbin/restore/dirs.c
sys/bus/u4b/usb_busdma.c
sys/dev/netif/ti/if_ti.c
sys/dev/pccard/exca/exca.c
sys/dev/raid/hptmv/gui_lib.c
sys/dev/video/cxm/cxm.h
sys/emulation/ndis/ntoskrnl_var.h
sys/gnu/vfs/ext2fs/ext2_lookup.c
sys/kern/imgact_elf.c
sys/kern/init_main.c
sys/kern/subr_rman.c
sys/kern/sysv_shm.c
sys/kern/uipc_mbuf.c
sys/kern/vfs_cluster.c
sys/net/pf/if_pfsync.c
sys/platform/pc64/vmm/vmx.h
sys/platform/pc64/x86_64/amd64_mem.c
sys/platform/vkernel64/platform/init.c
sys/platform/vkernel64/platform/pmap.c
sys/sys/mbuf.h
sys/vfs/hammer2/hammer2_chain.c
sys/vfs/hammer2/hammer2_disk.h
sys/vfs/isofs/cd9660/cd9660_bmap.c
sys/vfs/isofs/cd9660/cd9660_vnops.c
sys/vfs/nfs/nfs_vfsops.c
sys/vfs/ntfs/ntfs_subr.c
sys/vfs/smbfs/smbfs_io.c
sys/vfs/ufs/ffs_softdep.c
sys/vfs/ufs/ufs_dirhash.c
sys/vfs/ufs/ufs_lookup.c
sys/vm/vm_contig.c
sys/vm/vm_page.c
usr.bin/colrm/colrm.c
usr.bin/tail/misc.c

index 6068937..630a543 100644 (file)
@@ -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;
index 6914dfa..3a8b693 100644 (file)
@@ -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;
index 6ade297..dba468f 100644 (file)
@@ -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;
                }
index ad43eed..2fadf85 100644 (file)
@@ -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 @@ abspos:
         * 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;
index c76e909..efd4dcd 100644 (file)
@@ -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.
index 2adf107..00c8f6f 100644 (file)
@@ -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 {
index be1529e..9686424 100644 (file)
@@ -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) {
index 583ede4..a25b9e3 100644 (file)
@@ -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;
index 5170ef1..1ea4550 100644 (file)
@@ -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,
index 53c4fe2..11d4e8d 100644 (file)
  * 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;
index c804596..1a4fd0e 100644 (file)
@@ -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)));
                }
        }
 
index 969eb8c..9bf8b26 100644 (file)
@@ -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);
index 8e75ed5..fd10e96 100644 (file)
@@ -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:
index 7c42245..abb2650 100644 (file)
@@ -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);
index 81c5e42..586986e 100644 (file)
@@ -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;
index 3e5baf5..4419d13 100644 (file)
@@ -783,7 +783,7 @@ simple:
                        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;
 
index 19ed784..c4a5790 100644 (file)
@@ -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;
index 4786d17..f725363 100644 (file)
@@ -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)))
index df9829b..591e405 100644 (file)
@@ -544,7 +544,7 @@ found:
         * 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
index 3a92b2a..8d2e0cd 100644 (file)
@@ -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);
index 98ebac2..9a30b80 100644 (file)
@@ -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);
index 267c47a..6246760 100644 (file)
@@ -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));
index 52a3614..087f14a 100644 (file)
@@ -331,7 +331,8 @@ again:
        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 {
index b91bee4..d0330c8 100644 (file)
@@ -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));
 }
 
 /*
index 18abdf6..ae71523 100644 (file)
@@ -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);
index 41c2ff5..ff45b1b 100644 (file)
@@ -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);
 
index aac2d4c..fc3200b 100644 (file)
@@ -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)
 
 
 
index 2fca20c..232e25d 100644 (file)
@@ -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;
                }
index de24304..a4c9517 100644 (file)
@@ -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;
index c7a2bce..591d1ee 100644 (file)
@@ -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;
index c55792a..f8006aa 100644 (file)
@@ -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)
 
 /*
index e4a1107..0129c57 100644 (file)
@@ -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 @@ next_key_spinlocked:
        /*
         * 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;
                }
 
index 6c83947..f57b941 100644 (file)
  *     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).
index 01d6a31..98ffd10 100644 (file)
@@ -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) {
index 70a3342..de44a52 100644 (file)
@@ -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);
index 3cc5904..6d0f949 100644 (file)
@@ -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;
                }
index 662c640..eadd97a 100644 (file)
@@ -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) {
index fcee3de..7daaf9e 100644 (file)
@@ -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);
                }
                
index 83a40ed..fd4651a 100644 (file)
@@ -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;
index 32b6613..ddcbb77 100644 (file)
@@ -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;
index 3d493b1..78067a9 100644 (file)
@@ -444,7 +444,7 @@ found:
         * 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)) {
index ec7d6c9..2ab8f6d 100644 (file)
@@ -307,8 +307,7 @@ again:
                        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 &&
index a88ed90..0105379 100644 (file)
@@ -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(
index bc9ea15..870d19b 100644 (file)
@@ -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)
index dd1bcd1..202e9bc 100644 (file)
  *
  * @(#)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;