Blender V5.3
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
47/* Make offset a multiple of alignment, rounding up. Alignment must be a power of two. */
48ccl_device_inline size_t align_up(const size_t offset, const size_t alignment)
49{
50 return (offset + alignment - 1) & ~(alignment - 1);
51}
52
53/* Divide x by y, rounding up. */
54ccl_device_inline size_t divide_up(const size_t x, const size_t y)
55{
56 return (x + y - 1) / y;
57}
58
59/* Divide x by (1 << y), rounding up. */
60ccl_device_inline size_t divide_up_by_shift(const size_t x, const size_t y)
61{
62 return (x + ((1 << y) - 1)) >> y;
63}
64
65/* Make x a multiple of another number, rounding up. */
66ccl_device_inline size_t round_up(const size_t x, const size_t multiple)
67{
68 return ((x + multiple - 1) / multiple) * multiple;
69}
70
71/* Make x a multiple of another number, rounding down. */
72ccl_device_inline size_t round_down(const size_t x, const size_t multiple)
73{
74 return (x / multiple) * multiple;
75}
76
77/* Test if x is a power of two. */
79{
80 return (x & (x - 1)) == 0;
81}
82
84
85/* Device side printf only tested on CUDA, may work on more GPU devices. */
86#if !defined(__KERNEL_GPU__) || defined(__KERNEL_CUDA__)
87# define __KERNEL_PRINTF__
88#endif
89
90#if defined __METAL_PRINTF__
91# define print_float(label, a) metal::os_log_default.log_debug(label ": %.8f", a)
92#else
93ccl_device_inline void print_float(const ccl_private char *label, const float a)
94{
95# ifdef __KERNEL_PRINTF__
96 printf("%s: %.8f\n", label, (double)a);
97# endif
98}
99#endif
100
101/* Most GPU APIs matching native vector types, so we only need to implement them for
102 * CPU and oneAPI. */
103#if defined(__KERNEL_GPU__) && !defined(__KERNEL_ONEAPI__)
104# define __KERNEL_NATIVE_VECTOR_TYPES__
105#endif
unsigned long long int uint64_t
#define printf
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
unsigned int uchar
unsigned int uint
Definition BKE_text.h:55
unsigned short ushort
Definition BKE_text.h:56
ccl_device_inline size_t round_up(const size_t x, const size_t multiple)
Definition types_base.h:66
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:78
ccl_device_inline void print_float(const ccl_private char *label, const float a)
Definition types_base.h:93
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
Definition types_base.h:54
ccl_device_inline size_t round_down(const size_t x, const size_t multiple)
Definition types_base.h:72
ccl_device_inline size_t divide_up_by_shift(const size_t x, const size_t y)
Definition types_base.h:60
ccl_device_inline size_t align_up(const size_t offset, const size_t alignment)
Definition types_base.h:48