compiler/types: Add a contains_64bit helper
[mesa.git] / src / compiler / nir_types.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir_types.h"
29 #include "compiler/glsl/ir.h"
30
31 const char *
32 glsl_get_type_name(const glsl_type *type)
33 {
34 return type->name;
35 }
36
37 const glsl_type *
38 glsl_get_array_element(const glsl_type* type)
39 {
40 if (type->is_matrix())
41 return type->column_type();
42 else if (type->is_vector())
43 return type->get_scalar_type();
44 return type->fields.array;
45 }
46
47 const glsl_type *
48 glsl_without_array(const glsl_type *type)
49 {
50 return type->without_array();
51 }
52
53 const glsl_type *
54 glsl_without_array_or_matrix(const glsl_type *type)
55 {
56 type = type->without_array();
57 if (type->is_matrix())
58 type = type->column_type();
59 return type;
60 }
61
62 const glsl_type *
63 glsl_get_bare_type(const glsl_type *type)
64 {
65 return type->get_bare_type();
66 }
67
68 const glsl_type *
69 glsl_get_struct_field(const glsl_type *type, unsigned index)
70 {
71 return type->fields.structure[index].type;
72 }
73
74 int
75 glsl_get_struct_field_offset(const struct glsl_type *type,
76 unsigned index)
77 {
78 return type->fields.structure[index].offset;
79 }
80
81 unsigned
82 glsl_get_explicit_stride(const struct glsl_type *type)
83 {
84 return type->explicit_stride;
85 }
86
87 const glsl_type *
88 glsl_get_function_return_type(const glsl_type *type)
89 {
90 return type->fields.parameters[0].type;
91 }
92
93 const glsl_function_param *
94 glsl_get_function_param(const glsl_type *type, unsigned index)
95 {
96 return &type->fields.parameters[index + 1];
97 }
98
99 const struct glsl_type *
100 glsl_get_column_type(const struct glsl_type *type)
101 {
102 return type->column_type();
103 }
104
105 GLenum
106 glsl_get_gl_type(const struct glsl_type *type)
107 {
108 return type->gl_type;
109 }
110
111 enum glsl_base_type
112 glsl_get_base_type(const struct glsl_type *type)
113 {
114 return type->base_type;
115 }
116
117 unsigned
118 glsl_get_vector_elements(const struct glsl_type *type)
119 {
120 return type->vector_elements;
121 }
122
123 unsigned
124 glsl_get_components(const struct glsl_type *type)
125 {
126 return type->components();
127 }
128
129 unsigned
130 glsl_get_matrix_columns(const struct glsl_type *type)
131 {
132 return type->matrix_columns;
133 }
134
135 unsigned
136 glsl_get_length(const struct glsl_type *type)
137 {
138 return type->is_matrix() ? type->matrix_columns : type->length;
139 }
140
141 unsigned
142 glsl_get_aoa_size(const struct glsl_type *type)
143 {
144 return type->arrays_of_arrays_size();
145 }
146
147 unsigned
148 glsl_count_attribute_slots(const struct glsl_type *type,
149 bool is_gl_vertex_input)
150 {
151 return type->count_attribute_slots(is_gl_vertex_input);
152 }
153
154 unsigned
155 glsl_get_component_slots(const struct glsl_type *type)
156 {
157 return type->component_slots();
158 }
159
160 const char *
161 glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
162 {
163 return type->fields.structure[index].name;
164 }
165
166 glsl_sampler_dim
167 glsl_get_sampler_dim(const struct glsl_type *type)
168 {
169 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
170 return (glsl_sampler_dim)type->sampler_dimensionality;
171 }
172
173 glsl_base_type
174 glsl_get_sampler_result_type(const struct glsl_type *type)
175 {
176 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
177 return (glsl_base_type)type->sampled_type;
178 }
179
180 unsigned
181 glsl_get_sampler_target(const struct glsl_type *type)
182 {
183 assert(glsl_type_is_sampler(type));
184 return type->sampler_index();
185 }
186
187 int
188 glsl_get_sampler_coordinate_components(const struct glsl_type *type)
189 {
190 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
191 return type->coordinate_components();
192 }
193
194 unsigned
195 glsl_get_record_location_offset(const struct glsl_type *type,
196 unsigned length)
197 {
198 return type->record_location_offset(length);
199 }
200
201 bool
202 glsl_type_is_16bit(const glsl_type *type)
203 {
204 return type->is_16bit();
205 }
206
207 bool
208 glsl_type_is_32bit(const glsl_type *type)
209 {
210 return type->is_32bit();
211 }
212
213 bool
214 glsl_type_is_64bit(const glsl_type *type)
215 {
216 return type->is_64bit();
217 }
218
219 bool
220 glsl_type_is_void(const glsl_type *type)
221 {
222 return type->is_void();
223 }
224
225 bool
226 glsl_type_is_error(const glsl_type *type)
227 {
228 return type->is_error();
229 }
230
231 bool
232 glsl_type_is_vector(const struct glsl_type *type)
233 {
234 return type->is_vector();
235 }
236
237 bool
238 glsl_type_is_scalar(const struct glsl_type *type)
239 {
240 return type->is_scalar();
241 }
242
243 bool
244 glsl_type_is_vector_or_scalar(const struct glsl_type *type)
245 {
246 return type->is_vector() || type->is_scalar();
247 }
248
249 bool
250 glsl_type_is_matrix(const struct glsl_type *type)
251 {
252 return type->is_matrix();
253 }
254
255 bool
256 glsl_matrix_type_is_row_major(const struct glsl_type *type)
257 {
258 assert(type->is_matrix() && type->explicit_stride);
259 return type->interface_row_major;
260 }
261
262 bool
263 glsl_type_is_array(const struct glsl_type *type)
264 {
265 return type->is_array();
266 }
267
268 bool
269 glsl_type_is_array_of_arrays(const struct glsl_type *type)
270 {
271 return type->is_array_of_arrays();
272 }
273
274 bool
275 glsl_type_is_array_or_matrix(const struct glsl_type *type)
276 {
277 return type->is_array() || type->is_matrix();
278 }
279
280 bool
281 glsl_type_is_struct(const struct glsl_type *type)
282 {
283 return type->is_record() || type->is_interface();
284 }
285
286 bool
287 glsl_type_is_sampler(const struct glsl_type *type)
288 {
289 return type->is_sampler();
290 }
291
292 bool
293 glsl_type_is_image(const struct glsl_type *type)
294 {
295 return type->is_image();
296 }
297
298 bool
299 glsl_sampler_type_is_shadow(const struct glsl_type *type)
300 {
301 assert(glsl_type_is_sampler(type));
302 return type->sampler_shadow;
303 }
304
305 bool
306 glsl_sampler_type_is_array(const struct glsl_type *type)
307 {
308 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
309 return type->sampler_array;
310 }
311
312 bool
313 glsl_type_is_dual_slot(const struct glsl_type *type)
314 {
315 return type->is_dual_slot();
316 }
317
318 bool
319 glsl_type_is_numeric(const struct glsl_type *type)
320 {
321 return type->is_numeric();
322 }
323
324 bool
325 glsl_type_is_boolean(const struct glsl_type *type)
326 {
327 return type->is_boolean();
328 }
329 bool
330 glsl_type_is_integer(const struct glsl_type *type)
331 {
332 return type->is_integer();
333 }
334
335 bool
336 glsl_type_contains_64bit(const struct glsl_type *type)
337 {
338 return type->contains_64bit();
339 }
340
341 const glsl_type *
342 glsl_void_type(void)
343 {
344 return glsl_type::void_type;
345 }
346
347 const glsl_type *
348 glsl_float_type(void)
349 {
350 return glsl_type::float_type;
351 }
352
353 const glsl_type *
354 glsl_double_type(void)
355 {
356 return glsl_type::double_type;
357 }
358
359 const glsl_type *
360 glsl_float16_t_type(void)
361 {
362 return glsl_type::float16_t_type;
363 }
364
365 const glsl_type *
366 glsl_vec_type(unsigned n)
367 {
368 return glsl_type::vec(n);
369 }
370
371 const glsl_type *
372 glsl_dvec_type(unsigned n)
373 {
374 return glsl_type::dvec(n);
375 }
376
377 const glsl_type *
378 glsl_vec4_type(void)
379 {
380 return glsl_type::vec4_type;
381 }
382
383 const glsl_type *
384 glsl_uvec4_type(void)
385 {
386 return glsl_type::uvec4_type;
387 }
388
389 const glsl_type *
390 glsl_int_type(void)
391 {
392 return glsl_type::int_type;
393 }
394
395 const glsl_type *
396 glsl_uint_type(void)
397 {
398 return glsl_type::uint_type;
399 }
400
401 const glsl_type *
402 glsl_int64_t_type(void)
403 {
404 return glsl_type::int64_t_type;
405 }
406
407 const glsl_type *
408 glsl_uint64_t_type(void)
409 {
410 return glsl_type::uint64_t_type;
411 }
412
413 const glsl_type *
414 glsl_int16_t_type(void)
415 {
416 return glsl_type::int16_t_type;
417 }
418
419 const glsl_type *
420 glsl_uint16_t_type(void)
421 {
422 return glsl_type::uint16_t_type;
423 }
424
425 const glsl_type *
426 glsl_int8_t_type(void)
427 {
428 return glsl_type::int8_t_type;
429 }
430
431 const glsl_type *
432 glsl_uint8_t_type(void)
433 {
434 return glsl_type::uint8_t_type;
435 }
436
437 const glsl_type *
438 glsl_bool_type(void)
439 {
440 return glsl_type::bool_type;
441 }
442
443 const glsl_type *
444 glsl_scalar_type(enum glsl_base_type base_type)
445 {
446 return glsl_type::get_instance(base_type, 1, 1);
447 }
448
449 const glsl_type *
450 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
451 {
452 const glsl_type *t = glsl_type::get_instance(base_type, components, 1);
453 assert(t != glsl_type::error_type);
454 return t;
455 }
456
457 const glsl_type *
458 glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
459 {
460 const glsl_type *t = glsl_type::get_instance(base_type, rows, columns);
461 assert(t != glsl_type::error_type);
462 return t;
463 }
464
465 const glsl_type *
466 glsl_explicit_matrix_type(const glsl_type *mat,
467 unsigned stride, bool row_major)
468 {
469 assert(stride > 0);
470 const glsl_type *t = glsl_type::get_instance(mat->base_type,
471 mat->vector_elements,
472 mat->matrix_columns,
473 stride, row_major);
474 assert(t != glsl_type::error_type);
475 return t;
476 }
477
478 const glsl_type *
479 glsl_array_type(const glsl_type *base, unsigned elements,
480 unsigned explicit_stride)
481 {
482 return glsl_type::get_array_instance(base, elements, explicit_stride);
483 }
484
485 const glsl_type *
486 glsl_struct_type(const glsl_struct_field *fields,
487 unsigned num_fields, const char *name)
488 {
489 return glsl_type::get_record_instance(fields, num_fields, name);
490 }
491
492 const glsl_type *
493 glsl_interface_type(const glsl_struct_field *fields,
494 unsigned num_fields,
495 enum glsl_interface_packing packing,
496 bool row_major,
497 const char *block_name)
498 {
499 return glsl_type::get_interface_instance(fields, num_fields, packing,
500 row_major, block_name);
501 }
502
503 const struct glsl_type *
504 glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
505 enum glsl_base_type base_type)
506 {
507 return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
508 }
509
510 const struct glsl_type *
511 glsl_bare_sampler_type()
512 {
513 return glsl_type::sampler_type;
514 }
515
516 const struct glsl_type *
517 glsl_image_type(enum glsl_sampler_dim dim, bool is_array,
518 enum glsl_base_type base_type)
519 {
520 return glsl_type::get_image_instance(dim, is_array, base_type);
521 }
522
523 const glsl_type *
524 glsl_function_type(const glsl_type *return_type,
525 const glsl_function_param *params, unsigned num_params)
526 {
527 return glsl_type::get_function_instance(return_type, params, num_params);
528 }
529
530 const glsl_type *
531 glsl_transposed_type(const struct glsl_type *type)
532 {
533 assert(glsl_type_is_matrix(type));
534 return glsl_type::get_instance(type->base_type, type->matrix_columns,
535 type->vector_elements);
536 }
537
538 const glsl_type *
539 glsl_channel_type(const glsl_type *t)
540 {
541 switch (t->base_type) {
542 case GLSL_TYPE_ARRAY:
543 return glsl_array_type(glsl_channel_type(t->fields.array), t->length,
544 t->explicit_stride);
545 case GLSL_TYPE_UINT:
546 case GLSL_TYPE_INT:
547 case GLSL_TYPE_FLOAT:
548 case GLSL_TYPE_FLOAT16:
549 case GLSL_TYPE_DOUBLE:
550 case GLSL_TYPE_UINT8:
551 case GLSL_TYPE_INT8:
552 case GLSL_TYPE_UINT16:
553 case GLSL_TYPE_INT16:
554 case GLSL_TYPE_UINT64:
555 case GLSL_TYPE_INT64:
556 case GLSL_TYPE_BOOL:
557 return glsl_type::get_instance(t->base_type, 1, 1);
558 default:
559 unreachable("Unhandled base type glsl_channel_type()");
560 }
561 }
562
563 void
564 glsl_get_natural_size_align_bytes(const struct glsl_type *type,
565 unsigned *size, unsigned *align)
566 {
567 switch (type->base_type) {
568 case GLSL_TYPE_BOOL:
569 /* We special-case Booleans to 32 bits to not cause heartburn for
570 * drivers that suddenly get an 8-bit load.
571 */
572 *size = 4 * type->components();
573 *align = 4;
574 break;
575
576 case GLSL_TYPE_UINT8:
577 case GLSL_TYPE_INT8:
578 case GLSL_TYPE_UINT16:
579 case GLSL_TYPE_INT16:
580 case GLSL_TYPE_FLOAT16:
581 case GLSL_TYPE_UINT:
582 case GLSL_TYPE_INT:
583 case GLSL_TYPE_FLOAT:
584 case GLSL_TYPE_DOUBLE:
585 case GLSL_TYPE_UINT64:
586 case GLSL_TYPE_INT64: {
587 unsigned N = glsl_get_bit_size(type) / 8;
588 *size = N * type->components();
589 *align = N;
590 break;
591 }
592
593 case GLSL_TYPE_ARRAY: {
594 unsigned elem_size, elem_align;
595 glsl_get_natural_size_align_bytes(type->fields.array,
596 &elem_size, &elem_align);
597 *align = elem_align;
598 *size = type->length * ALIGN_POT(elem_size, elem_align);
599 break;
600 }
601
602 case GLSL_TYPE_STRUCT:
603 *size = 0;
604 *align = 0;
605 for (unsigned i = 0; i < type->length; i++) {
606 unsigned elem_size, elem_align;
607 glsl_get_natural_size_align_bytes(type->fields.structure[i].type,
608 &elem_size, &elem_align);
609 *align = MAX2(*align, elem_align);
610 *size = ALIGN_POT(*size, elem_align) + elem_size;
611 }
612 break;
613
614 case GLSL_TYPE_SAMPLER:
615 case GLSL_TYPE_ATOMIC_UINT:
616 case GLSL_TYPE_SUBROUTINE:
617 case GLSL_TYPE_IMAGE:
618 case GLSL_TYPE_VOID:
619 case GLSL_TYPE_ERROR:
620 case GLSL_TYPE_INTERFACE:
621 case GLSL_TYPE_FUNCTION:
622 unreachable("type does not have a natural size");
623 }
624 }
625
626 const glsl_type *
627 glsl_atomic_uint_type(void)
628 {
629 return glsl_type::atomic_uint_type;
630 }
631
632 unsigned
633 glsl_atomic_size(const struct glsl_type *type)
634 {
635 return type->atomic_size();
636 }
637
638 bool
639 glsl_contains_atomic(const struct glsl_type *type)
640 {
641 return type->contains_atomic();
642 }