2#ifndef CLICK_INTEGERS_HH
3#define CLICK_INTEGERS_HH
4#include <click/glue.hh>
6#if !HAVE___BUILTIN_FFS && HAVE_FFS && HAVE_STRINGS_H
15#if HAVE_INT64_TYPES && !defined(htonq)
16# if CLICK_BYTE_ORDER != CLICK_LITTLE_ENDIAN && CLICK_BYTE_ORDER != CLICK_BIG_ENDIAN
17inline uint64_t
htonq(uint64_t x) __attribute__((error(
"unknown byte order")));
21inline uint64_t
htonq(uint64_t x) {
22# if CLICK_BYTE_ORDER == CLICK_LITTLE_ENDIAN
23 uint32_t hi = x >> 32;
24 uint32_t lo = x & 0xffffffff;
25 return (((uint64_t)htonl(lo)) << 32) | htonl(hi);
26# elif CLICK_BYTE_ORDER == CLICK_BIG_ENDIAN
34#if HAVE_INT64_TYPES && !defined(ntohq)
35# if CLICK_BYTE_ORDER != CLICK_LITTLE_ENDIAN && CLICK_BYTE_ORDER != CLICK_BIG_ENDIAN
36inline uint64_t
htonq(uint64_t x) __attribute__((error(
"unknown byte order")));
40inline uint64_t
ntohq(uint64_t x) {
41# if CLICK_BYTE_ORDER == CLICK_LITTLE_ENDIAN
42 uint32_t hi = x >> 32;
43 uint32_t lo = x & 0xffffffff;
44 return (((uint64_t)ntohl(lo)) << 32) | ntohl(hi);
45# elif CLICK_BYTE_ORDER == CLICK_BIG_ENDIAN
106# if HAVE_LONG_LONG && SIZEOF_LONG_LONG == 8
116# if !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
181# if HAVE_LONG_LONG && SIZEOF_LONG_LONG == 8
191# if !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
205#if HAVE___BUILTIN_CLZ && !HAVE_NO_INTEGER_BUILTINS
211 return (x ? __builtin_clz(x) + 1 : 0);
214# define NEED_FFS_MSB_UNSIGNED 1
219#if HAVE___BUILTIN_CLZL && !HAVE_NO_INTEGER_BUILTINS
221inline int ffs_msb(
unsigned long x) {
222 return (x ? __builtin_clzl(x) + 1 : 0);
224#elif SIZEOF_INT == SIZEOF_LONG
227 return ffs_msb(
static_cast<unsigned>(x));
230# define NEED_FFS_MSB_UNSIGNED_LONG 1
235#if HAVE_LONG_LONG && HAVE___BUILTIN_CLZLL && !HAVE_NO_INTEGER_BUILTINS
237inline int ffs_msb(
unsigned long long x) {
238 return (x ? __builtin_clzll(x) + 1 : 0);
240#elif HAVE_LONG_LONG && SIZEOF_LONG == SIZEOF_LONG_LONG
242inline int ffs_msb(
unsigned long long x) {
243 return ffs_msb(
static_cast<unsigned long>(x));
246# define NEED_FFS_MSB_UNSIGNED_LONG_LONG 1
248int ffs_msb(
unsigned long long x);
251#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
254inline int ffs_msb(uint64_t x) {
255 return ffs_msb(
static_cast<unsigned long>(x));
257# elif HAVE_LONG_LONG && SIZEOF_LONG_LONG >= 8
259inline int ffs_msb(uint64_t x) {
260 return ffs_msb(
static_cast<unsigned long long>(x));
263# define NEED_FFS_MSB_UINT64_T 1
271#if HAVE___BUILTIN_FFS && !HAVE_NO_INTEGER_BUILTINS
277 return __builtin_ffs(x);
279#elif HAVE_FFS && !HAVE_NO_INTEGER_BUILTINS
281inline int ffs_lsb(
unsigned x) {
285# define NEED_FFS_LSB_UNSIGNED 1
290#if HAVE___BUILTIN_FFSL && !HAVE_NO_INTEGER_BUILTINS
292inline int ffs_lsb(
unsigned long x) {
293 return __builtin_ffsl(x);
295#elif SIZEOF_INT == SIZEOF_LONG
298 return ffs_lsb(
static_cast<unsigned>(x));
301# define NEED_FFS_LSB_UNSIGNED_LONG 1
306#if HAVE_LONG_LONG && HAVE___BUILTIN_FFSLL && !HAVE_NO_INTEGER_BUILTINS
308inline int ffs_lsb(
unsigned long long x) {
309 return __builtin_ffsll(x);
311#elif HAVE_LONG_LONG && SIZEOF_LONG == SIZEOF_LONG_LONG
313inline int ffs_lsb(
unsigned long long x) {
314 return ffs_lsb(
static_cast<unsigned long>(x));
317# define NEED_FFS_LSB_UNSIGNED_LONG_LONG 1
319int ffs_lsb(
unsigned long long x);
322#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
325inline int ffs_lsb(uint64_t x) {
326 return ffs_lsb(
static_cast<unsigned long>(x));
328# elif HAVE_LONG_LONG && SIZEOF_LONG_LONG >= 8
330inline int ffs_lsb(uint64_t x) {
331 return ffs_lsb(
static_cast<unsigned long long>(x));
334# define NEED_FFS_LSB_UINT64_T 1
347#if HAVE_INT64_TYPES && HAVE_INT64_DIVIDE
366# if CLICK_LINUXMODULE && BITS_PER_LONG < 64
376# if CLICK_LINUXMODULE && BITS_PER_LONG < 64
377 if (unlikely(a < 0)) {
378 uint64_t a_abs = -(a + 1);
380 return (int64_t) -a_abs - 1;
382 uint64_t &a_unsigned =
reinterpret_cast<uint64_t &
>(a);
383 do_div(a_unsigned, b);
398 typedef typename fasthalf::half_type half_type;
400 half_type al = fasthalf::low(a), ah = fasthalf::high(a),
401 bl = fasthalf::low(b), bh = fasthalf::high(b);
406 T r2 = T(al) * bh + fasthalf::high(r0) + r1;
408 r3 += fasthalf::half_value;
410 xhigh = r3 + fasthalf::high(r2);
411 xlow = (r2 << fasthalf::half_bits) + fasthalf::low(r0);
415struct has_fast_int_multiply :
public false_type {
419#if defined(__i386__) || defined(__x86_64__)
420inline void int_multiply(
unsigned a,
unsigned b,
unsigned &xlow,
unsigned &xhigh)
422 __asm__(
"mul %2" :
"=a" (xlow),
"=d" (xhigh) :
"r" (a),
"a" (b) :
"cc");
424template<>
struct has_fast_int_multiply<unsigned> :
public true_type {};
426# if SIZEOF_LONG == 4 || (defined(__x86_64__) && SIZEOF_LONG == 8)
427inline void int_multiply(
unsigned long a,
unsigned long b,
unsigned long &xlow,
unsigned long &xhigh)
429 __asm__(
"mul %2" :
"=a" (xlow),
"=d" (xhigh) :
"r" (a),
"a" (b) :
"cc");
431template<>
struct has_fast_int_multiply<unsigned long> :
public true_type {};
434# if defined(__x86_64__) && SIZEOF_LONG_LONG == 8
435inline void int_multiply(
unsigned long long a,
unsigned long long b,
unsigned long long &xlow,
unsigned long long &xhigh)
437 __asm__(
"mul %2" :
"=a" (xlow),
"=d" (xhigh) :
"r" (a),
"a" (b) :
"cc");
439template<>
struct has_fast_int_multiply<unsigned long long> :
public true_type {};
453 quot = -(-(a + 1) / b) - 1;
461# if CLICK_LINUXMODULE && BITS_PER_LONG < 64
462 uint32_t rem = do_div(a, b);
473# if CLICK_LINUXMODULE && BITS_PER_LONG < 64
474 if (unlikely(a < 0)) {
475 uint64_t a_abs = -(a + 1);
476 uint32_t rem = do_div(a_abs, b);
477 quot = (int64_t) -a_abs - 1;
478 return rem ? b - rem : 0;
480 uint64_t &a_unsigned =
reinterpret_cast<uint64_t &
>(a);
481 uint32_t rem = do_div(a_unsigned, b);
494 quot = -(-(a + 1) / b) - 1;
Type wrapper for the constant false.
Type wrapper for the constant true.
unsigned char host_to_net_order(unsigned char x)
Translate x to network byte order.
Definition integers.hh:58
uint64_t ntohq(uint64_t x)
Return x translated from network to host byte order.
Definition integers.hh:40
void int_multiply(T a, T b, T &xlow, T &xhigh)
Multiply a * b, placing the low-order bits of the result in xlow and the high-order bits in xhigh.
Definition integers.hh:395
int ffs_msb(unsigned x)
Return the index of the most significant bit set in x.
Definition integers.hh:210
uint32_t int_divide(uint32_t a, uint32_t b)
Return a / b.
Definition integers.hh:354
uint32_t int_remainder(uint32_t a, uint32_t b, uint32_t ")
Divide a / b, placing quotient in quot and returning remainder.
Definition integers.hh:445
int ffs_lsb(unsigned x)
Return the index of the least significant bit set in x.
Definition integers.hh:276
uint64_t htonq(uint64_t x)
Return x translated from host to network byte order.
Definition integers.hh:21
uint32_t int_sqrt(uint32_t x)
Return the integer approximation of x's square root.
Definition integers.cc:176
unsigned char net_to_host_order(unsigned char x)
Translate x to host byte order.
Definition integers.hh:133
Type transformation for big integers.
Definition type_traits.hh:485
Numeric traits template.
Definition type_traits.hh:244