-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Dispatch.h
146 lines (139 loc) · 8.1 KB
/
Dispatch.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2023 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
#pragma once
#include "open3d/core/Dtype.h"
#include "open3d/utility/Logging.h"
/// Call a numerical templated function based on Dtype. Wrap the function to
/// a lambda function to use DISPATCH_DTYPE_TO_TEMPLATE.
///
/// Before:
/// if (dtype == core::Float32) {
/// func<float>(args);
/// } else if (dtype == core::Float64) {
/// func<double>(args);
/// } else ...
///
/// Now:
/// DISPATCH_DTYPE_TO_TEMPLATE(dtype, [&]() {
/// func<scalar_t>(args);
/// });
///
/// Inspired by:
/// https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/Dispatch.h
#define DISPATCH_DTYPE_TO_TEMPLATE(DTYPE, ...) \
[&] { \
if (DTYPE == open3d::core::Float32) { \
using scalar_t = float; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Float64) { \
using scalar_t = double; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Int8) { \
using scalar_t = int8_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Int16) { \
using scalar_t = int16_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Int32) { \
using scalar_t = int32_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Int64) { \
using scalar_t = int64_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::UInt8) { \
using scalar_t = uint8_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::UInt16) { \
using scalar_t = uint16_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::UInt32) { \
using scalar_t = uint32_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::UInt64) { \
using scalar_t = uint64_t; \
return __VA_ARGS__(); \
} else { \
open3d::utility::LogError("Unsupported data type."); \
} \
}()
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE, ...) \
[&] { \
if (DTYPE == open3d::core::Bool) { \
using scalar_t = bool; \
return __VA_ARGS__(); \
} else { \
DISPATCH_DTYPE_TO_TEMPLATE(DTYPE, __VA_ARGS__); \
} \
}()
#define DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(DTYPE, ...) \
[&] { \
if (DTYPE == open3d::core::Float32) { \
using scalar_t = float; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Float64) { \
using scalar_t = double; \
return __VA_ARGS__(); \
} else { \
open3d::utility::LogError("Unsupported data type."); \
} \
}()
#define DISPATCH_FLOAT_INT_DTYPE_TO_TEMPLATE(FDTYPE, IDTYPE, ...) \
[&] { \
if (FDTYPE == open3d::core::Float32 && \
IDTYPE == open3d::core::Int32) { \
using scalar_t = float; \
using int_t = int32_t; \
return __VA_ARGS__(); \
} else if (FDTYPE == open3d::core::Float32 && \
IDTYPE == open3d::core::Int64) { \
using scalar_t = float; \
using int_t = int64_t; \
return __VA_ARGS__(); \
} else if (FDTYPE == open3d::core::Float64 && \
IDTYPE == open3d::core::Int32) { \
using scalar_t = double; \
using int_t = int32_t; \
return __VA_ARGS__(); \
} else if (FDTYPE == open3d::core::Float64 && \
IDTYPE == open3d::core::Int64) { \
using scalar_t = double; \
using int_t = int64_t; \
return __VA_ARGS__(); \
} else { \
open3d::utility::LogError("Unsupported data type."); \
} \
}()
#define DISPATCH_INT_DTYPE_PREFIX_TO_TEMPLATE(DTYPE, PREFIX, ...) \
[&] { \
if (DTYPE == open3d::core::Int8) { \
using scalar_##PREFIX##_t = int8_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Int16) { \
using scalar_##PREFIX##_t = int16_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Int32) { \
using scalar_##PREFIX##_t = int32_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::Int64) { \
using scalar_##PREFIX##_t = int64_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::UInt8) { \
using scalar_##PREFIX##_t = uint8_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::UInt16) { \
using scalar_##PREFIX##_t = uint16_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::UInt32) { \
using scalar_##PREFIX##_t = uint32_t; \
return __VA_ARGS__(); \
} else if (DTYPE == open3d::core::UInt64) { \
using scalar_##PREFIX##_t = uint64_t; \
return __VA_ARGS__(); \
} else { \
open3d::utility::LogError("Unsupported data type."); \
} \
}()