-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmath.h
83 lines (67 loc) · 1.62 KB
/
math.h
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
/*
* Copyright (C) 2015-2018, Nils Moehrle
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#ifndef CACC_MATH_HEADER
#define CACC_MATH_HEADER
#include <cassert>
#include "defines.h"
CACC_NAMESPACE_BEGIN
inline
uint divup(uint a, uint b) {
return a / b + (a % b != 0);
}
/* Derived from http://stereopsis.com/radix.html */
__forceinline__ __host__ __device__
uint32_t float_to_uint32(float f)
{
static_assert(sizeof(float) == sizeof(uint32_t), "");
typedef union {
float f;
uint32_t u;
} Alias;
Alias tmp = {f};
uint32_t mask = -int32_t(tmp.u >> 31) | 0x80000000;
return tmp.u ^ mask;
}
__forceinline__ __host__ __device__
float uint32_to_float(uint32_t u)
{
static_assert(sizeof(uint32_t) == sizeof(float), "");
typedef union {
uint32_t u;
float f;
} Alias;
uint32_t mask = ((u >> 31) - 1) | 0x80000000;
Alias tmp = {u ^ mask};
return tmp.f;
}
__forceinline__ __host__ __device__
float float_to_uint32_as_float(float f)
{
static_assert(sizeof(float) == sizeof(uint32_t), "");
typedef union {
float f;
uint32_t u;
} Alias;
Alias tmp;
tmp.u = float_to_uint32(f);
return tmp.f;
}
__forceinline__ __host__ __device__
float uint32_as_float_to_float(float f)
{
static_assert(sizeof(float) == sizeof(uint32_t), "");
typedef union {
float f;
uint32_t u;
} Alias;
Alias tmp;
tmp.f = f;
return uint32_to_float(tmp.u);
}
CACC_NAMESPACE_END
#endif /* CACC_MATH_HEADER */