ae2f_docs
Loading...
Searching...
No Matches
iddef.h
1/** @file id.h @brief utility functions for id.h */
2
3#ifndef util_iddef_h
4#define util_iddef_h
5
6#include <assert.h>
7
8#include <ae2f/LangVer.h>
9
10#include <spirv/unified1/spirv.h>
11
12#include "./id.h"
13#include "./ctx.h"
14#include "./emitx.h"
15#include "./constant.h"
16
17ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) unsigned
18util_default_is_unsigned(const e_id_default c_id_default) {
19 switch(c_id_default) {
20 case ID_DEFAULT_U8:
21 case ID_DEFAULT_U16:
22 case ID_DEFAULT_U32:
23 case ID_DEFAULT_U64:
24 return 1;
25
26 case ID_DEFAULT_I32:
27 case ID_DEFAULT_F16:
28 case ID_DEFAULT_F32:
29 case ID_DEFAULT_F64:
30 case ID_DEFAULT_END:
31 case ID_DEFAULT_VOID:
32 case ID_DEFAULT_FN_VOID:
33 case ID_DEFAULT_U8_PTR_FUNC:
34 case ID_DEFAULT_F16_PTR_FUNC:
35 case ID_DEFAULT_F32_PTR_FUNC:
36 case ID_DEFAULT_F64_PTR_FUNC:
37 case ID_DEFAULT_I32_PTR_FUNC:
38 case ID_DEFAULT_U16_PTR_FUNC:
39 case ID_DEFAULT_U32_PTR_FUNC:
40 case ID_DEFAULT_U64_PTR_FUNC:
41 case ID_DEFAULT_U32V4_PTR_INP:
42 case ID_DEFAULT_U32V4_PTR_OUT:
43 default:
44 return 0;
45 }
46}
47
48ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) unsigned
49util_default_is_signed(const e_id_default c_id_default) {
50 switch(c_id_default) {
51 case ID_DEFAULT_I32:
52 return 1;
53
54 case ID_DEFAULT_F16:
55 case ID_DEFAULT_F32:
56 case ID_DEFAULT_F64:
57 case ID_DEFAULT_U8:
58 case ID_DEFAULT_U16:
59 case ID_DEFAULT_U32:
60 case ID_DEFAULT_U64:
61 case ID_DEFAULT_END:
62 case ID_DEFAULT_VOID:
63 case ID_DEFAULT_FN_VOID:
64 case ID_DEFAULT_U8_PTR_FUNC:
65 case ID_DEFAULT_F16_PTR_FUNC:
66 case ID_DEFAULT_F32_PTR_FUNC:
67 case ID_DEFAULT_F64_PTR_FUNC:
68 case ID_DEFAULT_I32_PTR_FUNC:
69 case ID_DEFAULT_U16_PTR_FUNC:
70 case ID_DEFAULT_U32_PTR_FUNC:
71 case ID_DEFAULT_U64_PTR_FUNC:
72 case ID_DEFAULT_U32V4_PTR_INP:
73 case ID_DEFAULT_U32V4_PTR_OUT:
74 default:
75 return 0;
76 }
77}
78
79ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) unsigned
80util_default_is_int(const e_id_default c_id_default) {
81 return util_default_is_unsigned(c_id_default) || util_default_is_signed(c_id_default);
82}
83
84ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) unsigned
85util_default_is_float(const e_id_default c_id_default) {
86 switch(c_id_default) {
87 case ID_DEFAULT_F16:
88 case ID_DEFAULT_F32:
89 case ID_DEFAULT_F64:
90 return 1;
91
92 case ID_DEFAULT_U8:
93 case ID_DEFAULT_U16:
94 case ID_DEFAULT_I32:
95 case ID_DEFAULT_U32:
96 case ID_DEFAULT_U64:
97 case ID_DEFAULT_END:
98 case ID_DEFAULT_VOID:
99 case ID_DEFAULT_FN_VOID:
100 case ID_DEFAULT_U8_PTR_FUNC:
101 case ID_DEFAULT_F16_PTR_FUNC:
102 case ID_DEFAULT_F32_PTR_FUNC:
103 case ID_DEFAULT_F64_PTR_FUNC:
104 case ID_DEFAULT_I32_PTR_FUNC:
105 case ID_DEFAULT_U16_PTR_FUNC:
106 case ID_DEFAULT_U32_PTR_FUNC:
107 case ID_DEFAULT_U64_PTR_FUNC:
108 case ID_DEFAULT_U32V4_PTR_INP:
109 case ID_DEFAULT_U32V4_PTR_OUT:
110 default:
111 return 0;
112 }
113}
114
115ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) unsigned
116util_is_default(const e_id_default c_id_default) {
117 switch(c_id_default) {
118 case ID_DEFAULT_F16:
119 case ID_DEFAULT_F32:
120 case ID_DEFAULT_F64:
121 case ID_DEFAULT_U8:
122 case ID_DEFAULT_U16:
123 case ID_DEFAULT_I32:
124 case ID_DEFAULT_U32:
125 case ID_DEFAULT_U64:
126 case ID_DEFAULT_END:
127 case ID_DEFAULT_VOID:
128 case ID_DEFAULT_FN_VOID:
129 case ID_DEFAULT_U8_PTR_FUNC:
130 case ID_DEFAULT_F16_PTR_FUNC:
131 case ID_DEFAULT_F32_PTR_FUNC:
132 case ID_DEFAULT_F64_PTR_FUNC:
133 case ID_DEFAULT_I32_PTR_FUNC:
134 case ID_DEFAULT_U16_PTR_FUNC:
135 case ID_DEFAULT_U32_PTR_FUNC:
136 case ID_DEFAULT_U64_PTR_FUNC:
137 case ID_DEFAULT_U32V4_PTR_INP:
138 case ID_DEFAULT_U32V4_PTR_OUT:
139 return 1;
140 default:
141 return 0;
142 }
143}
144
145ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) unsigned
146util_default_is_number(const e_id_default c_id_default) {
147 return util_default_is_int(c_id_default) || util_default_is_float(c_id_default);
148}
149
150ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) e_id_default
151util_default_float(unsigned bit_width) {
152 switch(bit_width) {
153 case 16:
154 return ID_DEFAULT_F16;
155 case 32:
156 return ID_DEFAULT_F32;
157 case 64:
158 return ID_DEFAULT_F64;
159 default:
160 return ID_DEFAULT_END;
161 }
162}
163
164
165ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) e_id_default
166util_default_unsigned(unsigned bit_width) {
167 switch(bit_width) {
168 case 8:
169 return ID_DEFAULT_U8;
170 case 16:
171 return ID_DEFAULT_U16;
172 case 32:
173 return ID_DEFAULT_U32;
174 case 64:
175 return ID_DEFAULT_U64;
176 default:
177 return ID_DEFAULT_END;
178 }
179}
180
181ae2f_WhenCXX(ae2f_constexpr) ae2f_WhenC(ae2f_inline ae2f_ccconst static) unsigned
182util_default_bit_width(const e_id_default c_id_default) {
183 switch(c_id_default) {
184 case ID_DEFAULT_U8:
185 return 8;
186
187 case ID_DEFAULT_U16:
188 case ID_DEFAULT_F16:
189 return 16;
190
191 case ID_DEFAULT_I32:
192 case ID_DEFAULT_F32:
193 case ID_DEFAULT_U32:
194 return 32;
195
196 case ID_DEFAULT_F64:
197 case ID_DEFAULT_U64:
198 return 64;
199
200 case ID_DEFAULT_END:
201 case ID_DEFAULT_VOID:
202 case ID_DEFAULT_FN_VOID:
203 case ID_DEFAULT_U8_PTR_FUNC:
204 case ID_DEFAULT_F16_PTR_FUNC:
205 case ID_DEFAULT_F32_PTR_FUNC:
206 case ID_DEFAULT_F64_PTR_FUNC:
207 case ID_DEFAULT_I32_PTR_FUNC:
208 case ID_DEFAULT_U16_PTR_FUNC:
209 case ID_DEFAULT_U32_PTR_FUNC:
210 case ID_DEFAULT_U64_PTR_FUNC:
211 case ID_DEFAULT_U32V4_PTR_INP:
212 case ID_DEFAULT_U32V4_PTR_OUT:
213 default:
214 return 0;
215 }
216}
217
218ae2f_inline static aclspv_id_t util_get_default_id(
219 const e_id_default c_id_default,
220 h_util_ctx_t h_ctx
221 )
222{
223 assert(h_ctx);
224 ae2f_unexpected_but_if(c_id_default == ID_DEFAULT_END)
225 return 0;
226
227 if(h_ctx->m_id_defaults[c_id_default])
228 return c_id_default;
229
230#define CTX h_ctx
231#define ret_count h_ctx->m_count.m_types
232 switch(c_id_default) {
233 default:
234 case ID_DEFAULT_END:
236 return 0;
237
238 {
239 aclspv_wrd_t STORAGE_CLASS;
240 aclspv_id_t U32V4;
241
243 case ID_DEFAULT_U32V4_PTR_INP:
244 STORAGE_CLASS = SpvStorageClassInput;
245 }
246
248 case ID_DEFAULT_U32V4_PTR_OUT:
249 STORAGE_CLASS = SpvStorageClassOutput;
250 }
251
252 ae2f_expected_but_else(U32V4 = util_mk_constant_vec32_id(4, CTX)) {
253 return 0;
254 }
255
257 &CTX->m_section.m_types
258 , ret_count
259 , c_id_default
260 , STORAGE_CLASS
261 , U32V4))
262 return 0;
263 } break;
264
265
266 case ID_DEFAULT_VOID:
267 /** OpTypeVoid */
268 ae2f_expected_but_else((ret_count = emit_opcode(&CTX->m_section.m_types, ret_count, SpvOpTypeVoid, 1)))
269 return 0;
270 ae2f_expected_but_else((ret_count = util_emit_wrd(&CTX->m_section.m_types, ret_count, ID_DEFAULT_VOID)))
271 return 0;
272 break;
273
274 case ID_DEFAULT_U64_PTR_FUNC:
275 ae2f_expected_but_else(util_get_default_id(ID_DEFAULT_U64, CTX))
276 return 0;
278 &CTX->m_section.m_types
279 , ret_count
280 , ID_DEFAULT_U64_PTR_FUNC
281 , SpvStorageClassFunction
282 , ID_DEFAULT_U64)) return 0;
283 break;
284 case ID_DEFAULT_U64:
286 ret_count = util_emitx_4(
287 &CTX->m_section.m_types
288 , ret_count
289 , SpvOpTypeInt
290 , ID_DEFAULT_U64
291 , 64, 0))
292 return 0;
293
295 CTX->m_count.m_capability = util_emitx_2(
296 &CTX->m_section.m_capability
297 , CTX->m_count.m_capability
298 , SpvOpCapability
299 , SpvCapabilityInt64
300 )) return 0;
301 break;
302
303 case ID_DEFAULT_U32_PTR_FUNC:
304 ae2f_expected_but_else(util_get_default_id(ID_DEFAULT_U32, CTX))
305 return 0;
307 &CTX->m_section.m_types
308 , ret_count
309 , ID_DEFAULT_U32_PTR_FUNC
310 , SpvStorageClassFunction
311 , ID_DEFAULT_U32)) return 0;
312 break;
313 case ID_DEFAULT_U32:
315 ret_count = util_emitx_4(
316 &CTX->m_section.m_types
317 , ret_count
318 , SpvOpTypeInt
319 , ID_DEFAULT_U32
320 , 32, 0))
321 return 0;
322 break;
323
324
325 case ID_DEFAULT_I32_PTR_FUNC:
326 ae2f_expected_but_else(util_get_default_id(ID_DEFAULT_I32, CTX))
327 return 0;
329 &CTX->m_section.m_types
330 , ret_count
331 , ID_DEFAULT_I32_PTR_FUNC
332 , SpvStorageClassFunction
333 , ID_DEFAULT_I32)) return 0;
334 break;
335 case ID_DEFAULT_I32:
337 ret_count = util_emitx_4(
338 &CTX->m_section.m_types
339 , ret_count
340 , SpvOpTypeInt
341 , ID_DEFAULT_I32
342 , 32, 1))
343 return 0;
344 break;
345
346 case ID_DEFAULT_U16_PTR_FUNC:
347 ae2f_expected_but_else(util_get_default_id(ID_DEFAULT_U16, CTX))
348 return 0;
350 &CTX->m_section.m_types
351 , ret_count
352 , ID_DEFAULT_U16_PTR_FUNC
353 , SpvStorageClassFunction
354 , ID_DEFAULT_U16)) return 0;
355 break;
356 case ID_DEFAULT_U16:
358 ret_count = util_emitx_4(
359 &CTX->m_section.m_types
360 , ret_count
361 , SpvOpTypeInt
362 , ID_DEFAULT_U16
363 , 16, 0))
364 return 0;
365
367 CTX->m_count.m_capability = util_emitx_2(
368 &CTX->m_section.m_capability
369 , CTX->m_count.m_capability
370 , SpvOpCapability
371 , SpvCapabilityInt16
372 )) return 0;
373
374 break;
375
376 case ID_DEFAULT_U8_PTR_FUNC:
377 ae2f_expected_but_else(util_get_default_id(ID_DEFAULT_U8, CTX))
378 return 0;
380 &CTX->m_section.m_types
381 , ret_count
382 , ID_DEFAULT_U8_PTR_FUNC
383 , SpvStorageClassFunction
384 , ID_DEFAULT_U8)) return 0;
385 break;
386 case ID_DEFAULT_U8:
388 ret_count = util_emitx_4(
389 &CTX->m_section.m_types
390 , ret_count
391 , SpvOpTypeInt
392 , ID_DEFAULT_U8
393 , 8, 0))
394 return 0;
395
397 CTX->m_count.m_capability = util_emitx_2(
398 &CTX->m_section.m_capability
399 , CTX->m_count.m_capability
400 , SpvOpCapability
401 , SpvCapabilityInt8
402 )) return 0;
403 break;
404
405 case ID_DEFAULT_F16_PTR_FUNC:
406 ae2f_expected_but_else(util_get_default_id(ID_DEFAULT_F16, CTX))
407 return 0;
409 &CTX->m_section.m_types
410 , ret_count
411 , ID_DEFAULT_F16_PTR_FUNC
412 , SpvStorageClassFunction
413 , ID_DEFAULT_F16)) return 0;
414 break;
415 case ID_DEFAULT_F16:
416 ae2f_expected_but_else(ret_count = util_emitx_3(
417 &CTX->m_section.m_types
418 , ret_count
419 , SpvOpTypeFloat
420 , ID_DEFAULT_F16
421 , 16
422 )) return 0;
423
425 CTX->m_count.m_capability = util_emitx_2(
426 &CTX->m_section.m_capability
427 , CTX->m_count.m_capability
428 , SpvOpCapability
429 , SpvCapabilityFloat16
430 )) return 0;
431 break;
432
433 case ID_DEFAULT_F32_PTR_FUNC:
434 ae2f_expected_but_else(util_get_default_id(ID_DEFAULT_F32, CTX))
435 return 0;
437 &CTX->m_section.m_types
438 , ret_count
439 , ID_DEFAULT_F32_PTR_FUNC
440 , SpvStorageClassFunction
441 , ID_DEFAULT_F32)) return 0;
442 break;
443 case ID_DEFAULT_F32:
444 ae2f_expected_but_else(ret_count = util_emitx_3(
445 &CTX->m_section.m_types
446 , ret_count
447 , SpvOpTypeFloat
448 , ID_DEFAULT_F32
449 , 32
450 )) return 0;
451 break;
452
453 case ID_DEFAULT_F64_PTR_FUNC:
454 ae2f_expected_but_else(util_get_default_id(ID_DEFAULT_F64, CTX))
455 return 0;
457 &CTX->m_section.m_types
458 , ret_count
459 , ID_DEFAULT_F64_PTR_FUNC
460 , SpvStorageClassFunction
461 , ID_DEFAULT_F64)) return 0;
462 break;
463 case ID_DEFAULT_F64:
464 ae2f_expected_but_else(ret_count = util_emitx_3(
465 &CTX->m_section.m_types
466 , ret_count
467 , SpvOpTypeFloat
468 , ID_DEFAULT_F64
469 , 64
470 )) return 0;
471
473 CTX->m_count.m_capability = util_emitx_2(
474 &CTX->m_section.m_capability
475 , CTX->m_count.m_capability
476 , SpvOpCapability
477 , SpvCapabilityFloat64
478 )) return 0;
479 break;
480
481 case ID_DEFAULT_FN_VOID:
482 /** OpTypeFunction %void () */
483 ae2f_expected_but_else(ret_count = util_emitx_3(
484 &CTX->m_section.m_types
485 , ret_count
486 , SpvOpTypeFunction
487 , ID_DEFAULT_FN_VOID
488 , ID_DEFAULT_VOID)) return 0;
489#define EMIT_POS CTX->m_count.m_name
490 {
491 const aclspv_wrd_t POS = EMIT_POS;
492 unless((EMIT_POS = emit_opcode(&CTX->m_section.m_name, EMIT_POS, SpvOpName, 0)))
493 return 0;
494 unless((EMIT_POS = util_emit_wrd(&CTX->m_section.m_name, EMIT_POS, ID_DEFAULT_FN_VOID)))
495 return 0;
496 unless((EMIT_POS = util_emit_str(&CTX->m_section.m_name, EMIT_POS, "::fn_void")))
497 return 0;
498 set_oprnd_count_for_opcode(get_wrd_of_vec(&CTX->m_section.m_name)[POS], EMIT_POS - POS - 1);
499 }
500#undef EMIT_POS
501 break;
502 }
503#undef ret_count
504#undef CTX
505
506 return h_ctx->m_id_defaults[c_id_default] = c_id_default;
507}
508
509#endif
#define ae2f_IS_SHARED
Definition Call.auto.h:12
#define ae2f_OFF
Definition Call.auto.h:3
#define ae2f_WhenCXX(a)
Appears when the current language is C.
Definition Cxx.h:44
#define ae2f_WhenC(a)
Appears when the current language is C++.
Definition Cxx.h:38
#define unless(a)
Invokes when condition is false.
Definition Keys.h:34
#define ae2f_extern
Suggests the existence of external variable or function, in naming of C. [non-mangling].
Definition Keys.h:25
#define ae2f_NIL
Definition Nil.h:13
#define ACLSPV_ABI_DECL
Declaration as ABI.
Definition abi.h:23
#define STATE_VAL
#define FNINFO
#define cpysz
#define cpypad
#define ae2f_assume(a)
tells the compiler that value if a is false, below this keyword is not expected to be reached.
Definition cc.h:228
#define ae2f_unexpected_but_if(a)
Definition cc.h:192
#define ae2f_expected_but_else(a)
Definition cc.h:201
#define ae2f_ccconst
Keyword as [[const]] on C23..
Definition cc.h:66
#define ae2f_noexcept
marker that this function does not throw something.
Definition cc.h:133
#define ae2f_restrict
Keyword as restrict on C99.
Definition cc.h:81
#define ae2f_expected_not(a)
expectes a as false.
Definition cc.h:185
#define ae2f_unreachable()
tells the compiler that below this keyword is not expected to be reached.
Definition cc.h:213
#define ae2f_inline
inline
Definition cc.h:149
#define ae2f_expected(a)
expectes a as true.
Definition cc.h:184
#define CTX
#define EMIT_POS
#define util_emitx_type_pointer(h_wrds, c_wrdcount, c_retid, c_storage_class, c_elm_type_id)
Definition emitx.h:134
#define ret_count
#define get_buf_from_scale(h_alloc, c_scale)
Definition scale.h:34
#define aclspv_opcode_t
integer as operation code
Definition spvty.h:27
aclspv_wrd_t aclspv_wrdcount_t
the integer type represents the number of word.
Definition spvty.h:61
#define aclspv_wrd_t
integer as word
Definition spvty.h:16
aclspv_id_t m_id
id
Definition ctx.h:50
aclspv_wrdcount_t m_num_cursor
number of m_cursors. for its use see util/cursor.h
Definition ctx.h:41
util_bind_unified m_unified
Definition bind.h:20
#define emit_opcode(h_wrds, c_wrdcount, c_opcode, c_num_opprm_opt)
try emit opcode with num_opprm
Definition wrdemit.h:75
#define get_wrd_of_vec(vec)
get word buffer from vector
Definition wrdemit.h:38
#define count_to_sz(c_count)
word count to byte size
Definition wrdemit.h:32
#define set_oprnd_count_for_opcode(cr_wrd, c_num_opprm)
Definition wrdemit.h:78