summaryrefslogtreecommitdiffstats
path: root/pbnint.h
blob: b80c96c5b6754a566f84622b2cc7a09168535bbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA; version 2.1,
 *   incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * pbn library internal header file
 */

#ifndef PBNINT_H
#define PBNINT_H

#include "pbn.h"

/* Macro to do a 2A/A -> A division/remainder operation */
#if defined(__GNUC__) && PBN_LIMB_BITS == 32 && \
    (defined(__i386__) || defined(__x86_64__))
#define DIVIDE(hn,ln,d,q,r)			\
    asm("divl %4"                               \
        : "=d" (r), "=a" (q)                    \
        : "d" (hn), "a" (ln), "rm" (d))
#else
/*
 * Note: the explicit casts allow some compilers to tell this is a 2A:A -> A
 * division operation.
 */
#define DIVIDE(hn,ln,d,q,r)                     			\
    do {                                                                \
        pbn_2limb_t _n;                                          	\
	pbn_limb_t  _d = (d);						\
        _n= ((pbn_2limb_t)(hn) << PBN_LIMB_BITS) + (pbn_limb_t)(ln);    \
        (q) = (pbn_limb_t)(_n/_d);					\
	(r) = (pbn_limb_t)(_n%_d);					\
    } while(0)
#endif

#ifdef __GNUC__
#define likely(x)	__builtin_expect(!!(x), 1)
#define unlikely(x)	__builtin_expect(!!(x), 0)
#else
#define likely(x)	(!!(x))
#define unlikely(x)	(!!(x))
#endif

#endif /* PBNINT_H */