Blender V5.0
types_base.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#if !defined(__KERNEL_METAL__)
8# include <cstdlib>
9#endif
10
11/* Standard Integer Types */
12
13#if !defined(__KERNEL_GPU__)
14# include <cstdint> // IWYU pragma: export
15# include <cstdio>
16#endif
17
18#include "util/defines.h"
19
20#ifndef __KERNEL_GPU__
21# include "util/optimization.h" // IWYU pragma: export
22# include "util/simd.h" // IWYU pragma: export
23#endif
24
26
27/* Types
28 *
29 * Define simpler unsigned type names, and integer with defined number of bits.
30 * Also vector types, named to be compatible with OpenCL builtin types, while
31 * working for CUDA and C++ too. */
32
33/* Shorter Unsigned Names */
34
35using uchar = unsigned char;
36using uint = unsigned int;
37using ushort = unsigned short;
38
39/* Fixed Bits Types */
40
41#ifndef __KERNEL_GPU__
42/* Generic Memory Pointer */
43
45#endif /* __KERNEL_GPU__ */
46
47ccl_device_inline size_t align_up(const size_t offset, const size_t alignment)
48{
49 return (offset + alignment - 1) & ~(alignment - 1);
50}
51
52ccl_device_inline size_t divide_up(const size_t x, const size_t y)
53{
54 return (x + y - 1) / y;
55}
56
57ccl_device_inline size_t round_up(const size_t x, const size_t multiple)
58{
59 return ((x + multiple - 1) / multiple) * multiple;
60}
61
62ccl_device_inline size_t round_down(const size_t x, const size_t multiple)
63{
64 return (x / multiple) * multiple;
65}
66
68{
69 return (x & (x - 1)) == 0;
70}
71
73
74/* Device side printf only tested on CUDA, may work on more GPU devices. */
75#if !defined(__KERNEL_GPU__) || defined(__KERNEL_CUDA__)
76# define __KERNEL_PRINTF__
77#endif
78
79#if defined __METAL_PRINTF__
80# define print_float(label, a) metal::os_log_default.log_debug(label ": %.8f", a)
81#else
82ccl_device_inline void print_float(const ccl_private char *label, const float a)
83{
84# ifdef __KERNEL_PRINTF__
85 printf("%s: %.8f\n", label, (double)a);
86# endif
87}
88#endif
89
90/* Most GPU APIs matching native vector types, so we only need to implement them for
91 * CPU and oneAPI. */
92#if defined(__KERNEL_GPU__) && !defined(__KERNEL_ONEAPI__)
93# define __KERNEL_NATIVE_VECTOR_TYPES__
94#endif
unsigned char uchar
unsigned int uint
unsigned short ushort
unsigned long long int uint64_t
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define printf(...)
ccl_device_inline size_t round_up(const size_t x, const size_t multiple)
Definition types_base.h:57
uint64_t device_ptr
Definition types_base.h:44
ccl_device_inline bool is_power_of_two(const size_t x)
Definition types_base.h:67
ccl_device_inline void print_float(const ccl_private char *label, const float a)
Definition types_base.h:82
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
Definition types_base.h:52
ccl_device_inline size_t round_down(const size_t x, const size_t multiple)
Definition types_base.h:62
ccl_device_inline size_t align_up(const size_t offset, const size_t alignment)
Definition types_base.h:47