-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpcmap.h
69 lines (61 loc) · 1.86 KB
/
pcmap.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
#ifndef PC_MAP_H
#define PC_MAP_H
#include <climits>
#if !defined FPZIP_WITH_REINTERPRET_CAST && !defined FPZIP_WITH_UNION
#include <cstring>
#endif
#include "types.h"
#define bitsizeof(t) ((uint)(CHAR_BIT * sizeof(t)))
template <typename T, uint width = bitsizeof(T), typename U = void>
struct PCmap;
// specialized for integer-to-integer map
template <typename T, uint width>
struct PCmap<T, width, void> {
typedef T Domain;
typedef T Range;
static const uint bits = width; // Range bits
static const uint shift = bitsizeof(Range) - bits; // Domain\Range bits
Range forward(Domain d) const { return d >> shift; }
Domain inverse(Range r) const { return r << shift; }
Domain identity(Domain d) const { return inverse(forward(d)); }
};
// specialized for float type
template <uint width>
struct PCmap<float, width, void> {
typedef float Domain;
typedef uint32 Range;
union Union {
Union(Domain d) : d(d) {}
Union(Range r) : r(r) {}
Domain d;
Range r;
};
static const uint bits = width; // Range bits
static const uint shift = bitsizeof(Range) - bits; // Domain\Range bits
Range fcast(Domain d) const;
Domain icast(Range r) const;
Range forward(Domain d) const;
Domain inverse(Range r) const;
Domain identity(Domain d) const;
};
// specialized for double type
template <uint width>
struct PCmap<double, width, void> {
typedef double Domain;
typedef uint64 Range;
union Union {
Union(Domain d) : d(d) {}
Union(Range r) : r(r) {}
Domain d;
Range r;
};
static const uint bits = width; // Range bits
static const uint shift = bitsizeof(Range) - bits; // Domain\Range bits
Range fcast(Domain d) const;
Domain icast(Range r) const;
Range forward(Domain d) const;
Domain inverse(Range r) const;
Domain identity(Domain d) const;
};
#include "pcmap.inl"
#endif