akaros/kern/arch/x86/div64.h
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
   3 * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
   4 *
   5 * Optimization for constant divisors on 32-bit machines:
   6 * Copyright (C) 2006-2015 Nicolas Pitre
   7 *
   8 * The semantics of do_div() are:
   9 *
  10 * uint32_t do_div(uint64_t *n, uint32_t base)
  11 * {
  12 *      uint32_t remainder = *n % base;
  13 *      *n = *n / base;
  14 *      return remainder;
  15 * }
  16 *
  17 * NOTE: macro parameter n is evaluated multiple times,
  18 *       beware of side effects!
  19 */
  20
  21#pragma once
  22
  23#include <sys/types.h>
  24
  25#if BITS_PER_LONG == 64
  26
  27# define do_div(n,base) ({                                      \
  28        uint32_t __base = (base);                               \
  29        uint32_t __rem;                                         \
  30        __rem = ((uint64_t)(n)) % __base;                       \
  31        (n) = ((uint64_t)(n)) / __base;                         \
  32        __rem;                                                  \
  33 })
  34
  35#else /* BITS_PER_LONG == ?? */
  36
  37# error "BITS_PER_LONG isn't #defined 64"
  38
  39#endif /* BITS_PER_LONG */
  40