MLX
Loading...
Searching...
No Matches
integral_constant.h
Go to the documentation of this file.
1// Copyright © 2024 Apple Inc.
2
3#pragma once
4
5#include <metal_stdlib>
7
8#pragma METAL internals : enable
9
10namespace mlx {
11namespace steel {
12
14// Integral constant with casting
16
17template <typename T, T v>
19 static constexpr constant T value = v;
20 using value_type = T;
22
23 METAL_FUNC constexpr operator value_type() const noexcept {
24 return value;
25 }
26
27 // METAL_FUNC constexpr value_type operator()() const noexcept {
28 // return value;
29 // }
30};
31
32template <bool B>
36
37template <class T>
38struct is_integral : bool_constant<metal::is_integral<T>::value> {};
39
40template <class T, T v>
42 : bool_constant<metal::is_integral<T>::value> {};
43
44template <typename T>
45constexpr constant bool is_integral_v = is_integral<T>::value;
46
47template <int val>
49
51// Binary Operators on Integral constants
53
54#define integral_const_binop(__op__, __operator__) \
55 template <typename T, T tv, typename U, U uv> \
56 METAL_FUNC constexpr auto __operator__( \
57 integral_constant<T, tv>, integral_constant<U, uv>) { \
58 constexpr auto res = tv __op__ uv; \
59 return integral_constant<decltype(res), res>{}; \
60 }
61
66
67integral_const_binop(==, operator==);
68integral_const_binop(!=, operator!=);
71integral_const_binop(<=, operator<=);
72integral_const_binop(>=, operator>=);
73
74integral_const_binop(&&, operator&&);
75integral_const_binop(||, operator||);
76
77#undef integral_const_binop
78
80// Reduction operators
82
83template <typename T>
84METAL_FUNC constexpr T sum(T x) {
85 return x;
86}
87
88template <typename T, typename... Us>
89METAL_FUNC constexpr auto sum(T x, Us... us) {
90 return x + sum(us...);
91}
92
93} // namespace steel
94} // namespace mlx
95
96#pragma METAL internals : disable
#define integral_const_binop(__op__, __operator__)
Definition integral_constant.h:54
constexpr constant bool is_integral_v
Definition integral_constant.h:45
METAL_FUNC constexpr T sum(T x)
Definition integral_constant.h:84
Definition allocator.h:7
Definition integral_constant.h:18
T value_type
Definition integral_constant.h:20
static constexpr constant T value
Definition integral_constant.h:19
Definition integral_constant.h:38