MRPT
2.0.3
mrpt
core
round.h
Go to the documentation of this file.
1
/* +------------------------------------------------------------------------+
2
| Mobile Robot Programming Toolkit (MRPT) |
3
| https://www.mrpt.org/ |
4
| |
5
| Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6
| See: https://www.mrpt.org/Authors - All rights reserved. |
7
| Released under BSD License. See: https://www.mrpt.org/License |
8
+------------------------------------------------------------------------+ */
9
10
#pragma once
11
12
#include <
mrpt/core/SSE_types.h
>
// needed by SSE intrinsics used in some inline functions below.
13
#include <
mrpt/core/cpu.h
>
14
#include <cmath>
// pow(), lrint()
15
16
namespace
mrpt
17
{
18
/** \addtogroup mrpt_round Round functions (in #include <mrpt/core/round.h>)
19
* \ingroup mrpt_core_grp
20
* @{ */
21
22
/** Returns the closer integer (int) to x */
23
template
<
typename
T>
24
inline
int
round
(
const
T value)
25
{
26
#if MRPT_HAS_SSE2
27
if
(
mrpt::cpu::supports
(
mrpt::cpu::feature::SSE2
))
28
{
29
__m128d t = _mm_set_sd(value);
30
return
_mm_cvtsd_si32(t);
31
}
32
else
33
#endif
34
return
static_cast<
int
>
(lrint(value));
35
}
36
37
/** Returns the closer integer (long) to x */
38
template
<
typename
T>
39
inline
long
round_long
(
const
T value)
40
{
41
#if MRPT_HAS_SSE2 && MRPT_WORD_SIZE == 64
42
if
(
mrpt::cpu::supports
(
mrpt::cpu::feature::SSE2
))
43
{
44
__m128d t = _mm_set_sd(value);
45
return
_mm_cvtsd_si64(t);
46
}
47
else
48
#endif
49
return
lrint(value);
50
}
51
52
/** Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and
53
* also fractions)
54
* power10 means round up to: 1 -> 10, 2 -> 100, 3 -> 1000, ... -1 -> 0.1, -2
55
* -> 0.01, ...
56
*/
57
template
<
class
T>
58
T
round_10power
(T
val
,
int
power10)
59
{
60
long
double
F = ::pow((
long
double
)10.0, -(
long
double
)power10);
61
long
int
t =
round_long
(
val
* F);
62
return
T(t / F);
63
}
64
65
/** @} */
66
}
// namespace mrpt
mrpt::round_long
long round_long(const T value)
Returns the closer integer (long) to x.
Definition:
round.h:39
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition:
BaseAppDataSource.h:15
SSE_types.h
val
int val
Definition:
mrpt_jpeglib.h:957
mrpt::round_10power
T round_10power(T val, int power10)
Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and also fractions) power10 m...
Definition:
round.h:58
mrpt::cpu::feature::SSE2
@ SSE2
mrpt::round
int round(const T value)
Returns the closer integer (int) to x.
Definition:
round.h:24
mrpt::cpu::supports
bool supports(feature f) noexcept
Returns true if the current CPU (and OS) supports the given CPU feature.
Definition:
cpu.h:75
cpu.h
Page generated by
Doxygen 1.8.17
for MRPT 2.0.3 at Fri May 29 13:06:46 UTC 2020