Elm
2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
int.h
1
/*
2
* $Id$
3
* int module interface
4
*
5
* This file is part of OTAWA
6
* Copyright (c) 2007-10, IRIT UPS.
7
*
8
* OTAWA is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
12
*
13
* OTAWA is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
17
*
18
* You should have received a copy of the GNU General Public License
19
* along with OTAWA; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
*/
22
#ifndef ELM_INT_H_
23
#define ELM_INT_H_
24
25
#include <elm/arch.h>
26
#include <stdint.h>
27
28
namespace
elm
{
29
30
// bits operations
31
#ifdef __GNUC__
32
inline
int
countOnes
(
t::uint8
i) {
return
__builtin_popcount(i); }
33
inline
int
countOnes
(
t::uint16
i) {
return
__builtin_popcount(i); }
34
inline
int
countOnes
(
t::uint32
i) {
return
__builtin_popcountl(i); }
35
inline
int
countOnes
(
t::uint64
i) {
return
__builtin_popcountll(i); }
36
#else
37
int
ones
(
t::uint8
i);
38
inline
int
countOnes
(
t::uint16
i) {
return
ones
(
t::uint8
(i)) +
ones
(
t::uint8
(i >> 8)); }
39
inline
int
countOnes
(
t::uint32
i) {
return
ones
(
t::uint16
(i)) +
ones
(
t::uint16
(i >> 16)); }
40
inline
int
countOnes
(
t::uint64
i) {
return
ones
(
t::uint32
(i)) +
ones
(
t::uint32
(i >> 32)); }
41
#endif
42
43
int
msb
(
t::uint32
i);
44
inline
int
msb
(
t::int32
i) {
return
msb
(
t::uint32
(i)); }
45
int
msb
(
t::uint64
i);
46
inline
int
msb
(
t::int64
i) {
return
msb
(
t::uint64
(i)); }
47
t::uint32
leastUpperPowerOf2
(
t::uint32
v);
48
t::uint64
leastUpperPowerOf2
(
t::uint64
v);
49
50
51
// arithmetic operations
52
inline
t::uint32
abs
(
t::int32
v) {
return
v >= 0 ? v : (-v); }
53
inline
t::uint64
abs
(
t::int64
v) {
return
v >= 0 ? v : (-v); }
54
inline
t::uint32
roundup
(
t::uint32
v,
t::uint32
m)
55
{
if
(
countOnes
(m) == 1)
return
(v + m - 1) & ~(m - 1);
else
return
((v - 1) / m + 1) * m; }
56
inline
t::uint32
rounddown
(
t::uint32
v,
t::uint32
m)
57
{
if
(
countOnes
(m) == 1)
return
v & ~(m - 1);
else
return
v / m * m; }
58
59
60
// operations with overflow
61
t::uint32
mult
(
t::uint32
a,
t::uint32
,
bool
& over);
62
t::uint64
mult
(
t::uint64
a,
t::uint64
,
bool
& over);
63
64
// deprecated
65
inline
int
ones
(
t::uint8
i) {
return
countOnes
(i); }
66
inline
int
ones
(
t::uint16
i) {
return
countOnes
(i); }
67
inline
int
ones
(
t::uint32
i) {
return
countOnes
(i); }
68
inline
int
ones
(
t::uint64
i) {
return
countOnes
(i); }
69
70
}
// elms
71
72
#endif
/* ELM_INT_H_ */
elm::abs
t::uint32 abs(t::int32 v)
Definition:
int.h:52
elm::t::int32
int int32
Definition:
arch.h:30
elm::t::uint16
unsigned short uint16
Definition:
arch.h:29
elm::leastUpperPowerOf2
t::uint32 leastUpperPowerOf2(t::uint32 v)
Definition:
int.cpp:226
elm::rounddown
t::uint32 rounddown(t::uint32 v, t::uint32 m)
Definition:
int.h:56
elm::t::int64
long int64
Definition:
arch.h:32
elm::msb
int msb(t::uint32 i)
Definition:
int.cpp:126
elm::mult
t::uint32 mult(t::uint32 a, t::uint32, bool &over)
Definition:
int.cpp:263
elm::ones
int ones(t::uint8 i)
Definition:
int.h:65
elm
Definition:
adapter.h:26
elm::t::uint64
unsigned long uint64
Definition:
arch.h:33
elm::countOnes
int countOnes(t::uint16 i)
Definition:
int.h:38
elm::t::uint8
unsigned char uint8
Definition:
arch.h:27
elm::t::uint32
unsigned int uint32
Definition:
arch.h:31
elm::roundup
t::uint32 roundup(t::uint32 v, t::uint32 m)
Definition:
int.h:54
include
elm
int.h
Generated on Fri Jul 23 2021 11:32:45 for Elm by
1.8.17