aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mvp/mvpkm/mvp_math.h
blob: 7017bc8f356b8d614094a8cbd50643fa8d0ad0d3 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
 *
 * Copyright (C) 2010-2012 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; see the file COPYING.  If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#line 5

/**
 * @file
 *
 * @brief Math library.
 */

#ifndef _MVP_MATH_H_
#define _MVP_MATH_H_

#define INCLUDE_ALLOW_VMX
#define INCLUDE_ALLOW_PV
#define INCLUDE_ALLOW_MODULE
#define INCLUDE_ALLOW_MONITOR
#define INCLUDE_ALLOW_GPL
#define INCLUDE_ALLOW_HOSTUSER
#include "include_check.h"

#include "mvp_compiler_gcc.h"

/**
 * @brief Compute floor log2 of a given 32-bit unsigned integer.
 *
 * @param n 32-bit unsigned integer, n > 0.
 *
 * @return floor(log2(n)).
 */
#define LOG2(n)                         \
(                                       \
          __builtin_constant_p(n) ? (   \
            (n) & (1UL << 31) ? 31 :    \
            (n) & (1UL << 30) ? 30 :    \
            (n) & (1UL << 29) ? 29 :    \
            (n) & (1UL << 28) ? 28 :    \
            (n) & (1UL << 27) ? 27 :    \
            (n) & (1UL << 26) ? 26 :    \
            (n) & (1UL << 25) ? 25 :    \
            (n) & (1UL << 24) ? 24 :    \
            (n) & (1UL << 23) ? 23 :    \
            (n) & (1UL << 22) ? 22 :    \
            (n) & (1UL << 21) ? 21 :    \
            (n) & (1UL << 20) ? 20 :    \
            (n) & (1UL << 19) ? 19 :    \
            (n) & (1UL << 18) ? 18 :    \
            (n) & (1UL << 17) ? 17 :    \
            (n) & (1UL << 16) ? 16 :    \
            (n) & (1UL << 15) ? 15 :    \
            (n) & (1UL << 14) ? 14 :    \
            (n) & (1UL << 13) ? 13 :    \
            (n) & (1UL << 12) ? 12 :    \
            (n) & (1UL << 11) ? 11 :    \
            (n) & (1UL << 10) ? 10 :    \
            (n) & (1UL <<  9) ?  9 :    \
            (n) & (1UL <<  8) ?  8 :    \
            (n) & (1UL <<  7) ?  7 :    \
            (n) & (1UL <<  6) ?  6 :    \
            (n) & (1UL <<  5) ?  5 :    \
            (n) & (1UL <<  4) ?  4 :    \
            (n) & (1UL <<  3) ?  3 :    \
            (n) & (1UL <<  2) ?  2 :    \
            (n) & (1UL <<  1) ?  1 :    \
            (n) & (1UL <<  0) ?  0 :    \
	    0xffffffff			\
	  ) : (uint32)(CLZ(1) - CLZ(n)) \
)

/**
 * @brief Multiplicative hash function for 32-bit key and p-bit range. See p229
 *        Introduction to Algorithms, Cormen, Leiserson and Rivest, 1996.
 *
 * @param key 32-bit key.
 * @param p range order, <= 32.
 *
 * @return hash value in range [0..2^p)
 */
static inline uint32
Math_MultiplicativeHash(uint32 key, uint32 p)
{
   return (key * 2654435769UL) >> (32 - p);
}

/**
 * @brief Compute ceiling log2 of a given 32-bit unsigned integer.
 *
 * @param n 32-bit unsigned integer, n > 0.
 *
 * @return ceiling(log2(n)).
 */
static inline uint32 CLOG2(uint32 n)
{
   return LOG2(n) + ((n & -n) != n);
}


/**
 * @brief djb2 String hashing function by Dan Bernstein, see
 *      http://www.cse.yorku.ca/~oz/hash.html
 * @param str String to hash
 * @return 32-bit hash value
 */
static inline
uint32 Math_Djb2Hash(uint8 *str)
{
     uint32 hash = 5381;
     int32 c;

     while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
     }

     return hash;
}

#endif // ifndef _MVP_MATH_H_