-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlayer_util.hpp
More file actions
136 lines (117 loc) · 2.97 KB
/
layer_util.hpp
File metadata and controls
136 lines (117 loc) · 2.97 KB
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
/*
// Copyright (c) 2022-2025 Ben Ashbaugh
//
// SPDX-License-Identifier: MIT
*/
#pragma once
#include <CL/cl_layer.h>
#include <cstring>
#include <cctype>
#include <vector>
template<class T>
cl_int writeParamToMemory(
size_t param_value_size,
T param,
size_t* param_value_size_ret,
T* pointer)
{
if (pointer != nullptr) {
if (param_value_size < sizeof(param)) {
return CL_INVALID_VALUE;
}
*pointer = param;
}
if (param_value_size_ret != nullptr) {
*param_value_size_ret = sizeof(param);
}
return CL_SUCCESS;
}
template<class T>
cl_int writeVectorToMemory(
size_t param_value_size,
const std::vector<T>& param,
size_t *param_value_size_ret,
T* pointer )
{
size_t size = param.size() * sizeof(T);
if (pointer != nullptr) {
if (param_value_size < size) {
return CL_INVALID_VALUE;
}
memcpy(pointer, param.data(), size);
}
if (param_value_size_ret != nullptr) {
*param_value_size_ret = size;
}
return CL_SUCCESS;
}
static inline cl_int writeStringToMemory(
size_t param_value_size,
const char* param,
size_t* param_value_size_ret,
char* pointer )
{
size_t size = strlen(param) + 1;
if (pointer != nullptr) {
if (param_value_size < size) {
return CL_INVALID_VALUE;
}
strcpy(pointer, param);
}
if (param_value_size_ret != nullptr) {
*param_value_size_ret = size;
}
return CL_SUCCESS;
}
static cl_version getOpenCLVersionFromString(
const char* str)
{
cl_uint major = 0;
cl_uint minor = 0;
// The device version string has the form:
// OpenCL <Major>.<Minor> <Vendor Specific Info>
const char* prefix = "OpenCL ";
size_t sz = strlen(prefix);
if (strlen(str) > sz &&
strncmp(str, prefix, sz) == 0) {
const char* check = str + sz;
while (isdigit(check[0])) {
major *= 10;
major += check[0] - '0';
++check;
}
if (check[0] == '.') {
++check;
}
while (isdigit(check[0])) {
minor *= 10;
minor += check[0] - '0';
++check;
}
}
return CL_MAKE_VERSION(major, minor, 0);
}
static inline bool checkStringForExtension(
const char* str,
const char* extensionName )
{
bool supported = false;
if (extensionName && !strchr(extensionName, ' ')) {
const char* start = str;
while (true) {
const char* where = strstr(start, extensionName);
if (!where) {
break;
}
const char* terminator = where + strlen(extensionName);
if (where == start || *(where - 1) == ' ') {
if (*terminator == ' ' || *terminator == '\0') {
supported = true;
break;
}
}
start = terminator;
}
}
return supported;
}