Best Python code snippet using robotframework
ir_expression_operation.py
Source:ir_expression_operation.py
1#2# Copyright (C) 2015 Intel Corporation3#4# Permission is hereby granted, free of charge, to any person obtaining a5# copy of this software and associated documentation files (the "Software"),6# to deal in the Software without restriction, including without limitation7# the rights to use, copy, modify, merge, publish, distribute, sublicense,8# and/or sell copies of the Software, and to permit persons to whom the9# Software is furnished to do so, subject to the following conditions:10#11# The above copyright notice and this permission notice (including the next12# paragraph) shall be included in all copies or substantial portions of the13# Software.14#15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21# IN THE SOFTWARE.22import mako.template23import sys24class type(object):25 def __init__(self, c_type, union_field, glsl_type):26 self.c_type = c_type27 self.union_field = union_field28 self.glsl_type = glsl_type29class type_signature_iter(object):30 """Basic iterator for a set of type signatures. Various kinds of sequences of31 types come in, and an iteration of type_signature objects come out.32 """33 def __init__(self, source_types, num_operands):34 """Initialize an iterator from a sequence of input types and a number35 operands. This is for signatures where all the operands have the same36 type and the result type of the operation is the same as the input type.37 """38 self.dest_type = None39 self.source_types = source_types40 self.num_operands = num_operands41 self.i = 042 def __init__(self, dest_type, source_types, num_operands):43 """Initialize an iterator from a result tpye, a sequence of input types and a44 number operands. This is for signatures where all the operands have the45 same type but the result type of the operation is different from the46 input type.47 """48 self.dest_type = dest_type49 self.source_types = source_types50 self.num_operands = num_operands51 self.i = 052 def __iter__(self):53 return self54 def next(self):55 if self.i < len(self.source_types):56 i = self.i57 self.i += 158 if self.dest_type is None:59 dest_type = self.source_types[i]60 else:61 dest_type = self.dest_type62 return (dest_type, self.num_operands * (self.source_types[i],))63 else:64 raise StopIteration()65uint_type = type("unsigned", "u", "GLSL_TYPE_UINT")66int_type = type("int", "i", "GLSL_TYPE_INT")67uint64_type = type("uint64_t", "u64", "GLSL_TYPE_UINT64")68int64_type = type("int64_t", "i64", "GLSL_TYPE_INT64")69float_type = type("float", "f", "GLSL_TYPE_FLOAT")70double_type = type("double", "d", "GLSL_TYPE_DOUBLE")71bool_type = type("bool", "b", "GLSL_TYPE_BOOL")72all_types = (uint_type, int_type, float_type, double_type, uint64_type, int64_type, bool_type)73numeric_types = (uint_type, int_type, float_type, double_type, uint64_type, int64_type)74signed_numeric_types = (int_type, float_type, double_type, int64_type)75integer_types = (uint_type, int_type, uint64_type, int64_type)76real_types = (float_type, double_type)77# This template is for operations that can have operands of a several78# different types, and each type may or may not has a different C expression.79# This is used by most operations.80constant_template_common = mako.template.Template("""\81 case ${op.get_enum_name()}:82 for (unsigned c = 0; c < op[0]->type->components(); c++) {83 switch (op[0]->type->base_type) {84 % for dst_type, src_types in op.signatures():85 case ${src_types[0].glsl_type}:86 data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types)};87 break;88 % endfor89 default:90 unreachable("invalid type");91 }92 }93 break;""")94# This template is for binary operations that can operate on some combination95# of scalar and vector operands.96constant_template_vector_scalar = mako.template.Template("""\97 case ${op.get_enum_name()}:98 % if "mixed" in op.flags:99 % for i in xrange(op.num_operands):100 assert(op[${i}]->type->base_type == ${op.source_types[0].glsl_type} ||101 % for src_type in op.source_types[1:-1]:102 op[${i}]->type->base_type == ${src_type.glsl_type} ||103 % endfor104 op[${i}]->type->base_type == ${op.source_types[-1].glsl_type});105 % endfor106 % else:107 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);108 % endif109 for (unsigned c = 0, c0 = 0, c1 = 0;110 c < components;111 c0 += c0_inc, c1 += c1_inc, c++) {112 switch (op[0]->type->base_type) {113 % for dst_type, src_types in op.signatures():114 case ${src_types[0].glsl_type}:115 data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types, ("c0", "c1", "c2"))};116 break;117 % endfor118 default:119 unreachable("invalid type");120 }121 }122 break;""")123# This template is for multiplication. It is unique because it has to support124# matrix * vector and matrix * matrix operations, and those are just different.125constant_template_mul = mako.template.Template("""\126 case ${op.get_enum_name()}:127 /* Check for equal types, or unequal types involving scalars */128 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())129 || op0_scalar || op1_scalar) {130 for (unsigned c = 0, c0 = 0, c1 = 0;131 c < components;132 c0 += c0_inc, c1 += c1_inc, c++) {133 switch (op[0]->type->base_type) {134 % for dst_type, src_types in op.signatures():135 case ${src_types[0].glsl_type}:136 data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types, ("c0", "c1", "c2"))};137 break;138 % endfor139 default:140 unreachable("invalid type");141 }142 }143 } else {144 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());145 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either146 * matrix can be a GLSL vector, either N or P can be 1.147 *148 * For vec*mat, the vector is treated as a row vector. This149 * means the vector is a 1-row x M-column matrix.150 *151 * For mat*vec, the vector is treated as a column vector. Since152 * matrix_columns is 1 for vectors, this just works.153 */154 const unsigned n = op[0]->type->is_vector()155 ? 1 : op[0]->type->vector_elements;156 const unsigned m = op[1]->type->vector_elements;157 const unsigned p = op[1]->type->matrix_columns;158 for (unsigned j = 0; j < p; j++) {159 for (unsigned i = 0; i < n; i++) {160 for (unsigned k = 0; k < m; k++) {161 if (op[0]->type->is_double())162 data.d[i+n*j] += op[0]->value.d[i+n*k]*op[1]->value.d[k+m*j];163 else164 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];165 }166 }167 }168 }169 break;""")170# This template is for operations that are horizontal and either have only a171# single type or the implementation for all types is identical. That is, the172# operation consumes a vector and produces a scalar.173constant_template_horizontal_single_implementation = mako.template.Template("""\174 case ${op.get_enum_name()}:175 data.${op.dest_type.union_field}[0] = ${op.c_expression['default']};176 break;""")177# This template is for operations that are horizontal and do not assign the178# result. The various unpack operations are examples.179constant_template_horizontal_nonassignment = mako.template.Template("""\180 case ${op.get_enum_name()}:181 ${op.c_expression['default']};182 break;""")183# This template is for binary operations that are horizontal. That is, the184# operation consumes a vector and produces a scalar.185constant_template_horizontal = mako.template.Template("""\186 case ${op.get_enum_name()}:187 switch (op[0]->type->base_type) {188 % for dst_type, src_types in op.signatures():189 case ${src_types[0].glsl_type}:190 data.${dst_type.union_field}[0] = ${op.get_c_expression(src_types)};191 break;192 % endfor193 default:194 unreachable("invalid type");195 }196 break;""")197# This template is for ir_binop_vector_extract.198constant_template_vector_extract = mako.template.Template("""\199 case ${op.get_enum_name()}: {200 const int c = CLAMP(op[1]->value.i[0], 0,201 (int) op[0]->type->vector_elements - 1);202 switch (op[0]->type->base_type) {203 % for dst_type, src_types in op.signatures():204 case ${src_types[0].glsl_type}:205 data.${dst_type.union_field}[0] = op[0]->value.${src_types[0].union_field}[c];206 break;207 % endfor208 default:209 unreachable("invalid type");210 }211 break;212 }""")213# This template is for ir_triop_vector_insert.214constant_template_vector_insert = mako.template.Template("""\215 case ${op.get_enum_name()}: {216 const unsigned idx = op[2]->value.u[0];217 memcpy(&data, &op[0]->value, sizeof(data));218 switch (this->type->base_type) {219 % for dst_type, src_types in op.signatures():220 case ${src_types[0].glsl_type}:221 data.${dst_type.union_field}[idx] = op[1]->value.${src_types[0].union_field}[0];222 break;223 % endfor224 default:225 unreachable("invalid type");226 }227 break;228 }""")229# This template is for ir_quadop_vector.230constant_template_vector = mako.template.Template("""\231 case ${op.get_enum_name()}:232 for (unsigned c = 0; c < this->type->vector_elements; c++) {233 switch (this->type->base_type) {234 % for dst_type, src_types in op.signatures():235 case ${src_types[0].glsl_type}:236 data.${dst_type.union_field}[c] = op[c]->value.${src_types[0].union_field}[0];237 break;238 % endfor239 default:240 unreachable("invalid type");241 }242 }243 break;""")244# This template is for ir_triop_lrp.245constant_template_lrp = mako.template.Template("""\246 case ${op.get_enum_name()}: {247 assert(op[0]->type->is_float() || op[0]->type->is_double());248 assert(op[1]->type->is_float() || op[1]->type->is_double());249 assert(op[2]->type->is_float() || op[2]->type->is_double());250 unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;251 for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {252 switch (this->type->base_type) {253 % for dst_type, src_types in op.signatures():254 case ${src_types[0].glsl_type}:255 data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types, ("c", "c", "c2"))};256 break;257 % endfor258 default:259 unreachable("invalid type");260 }261 }262 break;263 }""")264# This template is for ir_triop_csel. This expression is really unique265# because not all of the operands are the same type, and the second operand266# determines the type of the expression (instead of the first).267constant_template_csel = mako.template.Template("""\268 case ${op.get_enum_name()}:269 for (unsigned c = 0; c < components; c++) {270 switch (this->type->base_type) {271 % for dst_type, src_types in op.signatures():272 case ${src_types[1].glsl_type}:273 data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types)};274 break;275 % endfor276 default:277 unreachable("invalid type");278 }279 }280 break;""")281vector_scalar_operation = "vector-scalar"282horizontal_operation = "horizontal"283types_identical_operation = "identical"284non_assign_operation = "nonassign"285mixed_type_operation = "mixed"286class operation(object):287 def __init__(self, name, num_operands, printable_name = None, source_types = None, dest_type = None, c_expression = None, flags = None, all_signatures = None):288 self.name = name289 self.num_operands = num_operands290 if printable_name is None:291 self.printable_name = name292 else:293 self.printable_name = printable_name294 self.all_signatures = all_signatures295 if source_types is None:296 self.source_types = tuple()297 else:298 self.source_types = source_types299 self.dest_type = dest_type300 if c_expression is None:301 self.c_expression = None302 elif isinstance(c_expression, str):303 self.c_expression = {'default': c_expression}304 else:305 self.c_expression = c_expression306 if flags is None:307 self.flags = frozenset()308 elif isinstance(flags, str):309 self.flags = frozenset([flags])310 else:311 self.flags = frozenset(flags)312 def get_enum_name(self):313 return "ir_{0}op_{1}".format(("un", "bin", "tri", "quad")[self.num_operands-1], self.name)314 def get_template(self):315 if self.c_expression is None:316 return None317 if horizontal_operation in self.flags:318 if non_assign_operation in self.flags:319 return constant_template_horizontal_nonassignment.render(op=self)320 elif types_identical_operation in self.flags:321 return constant_template_horizontal_single_implementation.render(op=self)322 else:323 return constant_template_horizontal.render(op=self)324 if self.num_operands == 2:325 if self.name == "mul":326 return constant_template_mul.render(op=self)327 elif self.name == "vector_extract":328 return constant_template_vector_extract.render(op=self)329 elif vector_scalar_operation in self.flags:330 return constant_template_vector_scalar.render(op=self)331 elif self.num_operands == 3:332 if self.name == "vector_insert":333 return constant_template_vector_insert.render(op=self)334 elif self.name == "lrp":335 return constant_template_lrp.render(op=self)336 elif self.name == "csel":337 return constant_template_csel.render(op=self)338 elif self.num_operands == 4:339 if self.name == "vector":340 return constant_template_vector.render(op=self)341 return constant_template_common.render(op=self)342 def get_c_expression(self, types, indices=("c", "c", "c")):343 src0 = "op[0]->value.{0}[{1}]".format(types[0].union_field, indices[0])344 src1 = "op[1]->value.{0}[{1}]".format(types[1].union_field, indices[1]) if len(types) >= 2 else "ERROR"345 src2 = "op[2]->value.{0}[{1}]".format(types[2].union_field, indices[2]) if len(types) >= 3 else "ERROR"346 src3 = "op[3]->value.{0}[c]".format(types[3].union_field) if len(types) >= 4 else "ERROR"347 expr = self.c_expression[types[0].union_field] if types[0].union_field in self.c_expression else self.c_expression['default']348 return expr.format(src0=src0,349 src1=src1,350 src2=src2,351 src3=src3)352 def signatures(self):353 if self.all_signatures is not None:354 return self.all_signatures355 else:356 return type_signature_iter(self.dest_type, self.source_types, self.num_operands)357ir_expression_operation = [358 operation("bit_not", 1, printable_name="~", source_types=integer_types, c_expression="~ {src0}"),359 operation("logic_not", 1, printable_name="!", source_types=(bool_type,), c_expression="!{src0}"),360 operation("neg", 1, source_types=numeric_types, c_expression={'u': "-((int) {src0})", 'default': "-{src0}"}),361 operation("abs", 1, source_types=signed_numeric_types, c_expression={'i': "{src0} < 0 ? -{src0} : {src0}", 'f': "fabsf({src0})", 'd': "fabs({src0})", 'i64': "{src0} < 0 ? -{src0} : {src0}"}),362 operation("sign", 1, source_types=signed_numeric_types, c_expression={'i': "({src0} > 0) - ({src0} < 0)", 'f': "float(({src0} > 0.0F) - ({src0} < 0.0F))", 'd': "double(({src0} > 0.0) - ({src0} < 0.0))", 'i64': "({src0} > 0) - ({src0} < 0)"}),363 operation("rcp", 1, source_types=real_types, c_expression={'f': "1.0F / {src0}", 'd': "1.0 / {src0}"}),364 operation("rsq", 1, source_types=real_types, c_expression={'f': "1.0F / sqrtf({src0})", 'd': "1.0 / sqrt({src0})"}),365 operation("sqrt", 1, source_types=real_types, c_expression={'f': "sqrtf({src0})", 'd': "sqrt({src0})"}),366 operation("exp", 1, source_types=(float_type,), c_expression="expf({src0})"), # Log base e on gentype367 operation("log", 1, source_types=(float_type,), c_expression="logf({src0})"), # Natural log on gentype368 operation("exp2", 1, source_types=(float_type,), c_expression="exp2f({src0})"),369 operation("log2", 1, source_types=(float_type,), c_expression="log2f({src0})"),370 # Float-to-integer conversion.371 operation("f2i", 1, source_types=(float_type,), dest_type=int_type, c_expression="(int) {src0}"),372 # Float-to-unsigned conversion.373 operation("f2u", 1, source_types=(float_type,), dest_type=uint_type, c_expression="(unsigned) {src0}"),374 # Integer-to-float conversion.375 operation("i2f", 1, source_types=(int_type,), dest_type=float_type, c_expression="(float) {src0}"),376 # Float-to-boolean conversion377 operation("f2b", 1, source_types=(float_type,), dest_type=bool_type, c_expression="{src0} != 0.0F ? true : false"),378 # Boolean-to-float conversion379 operation("b2f", 1, source_types=(bool_type,), dest_type=float_type, c_expression="{src0} ? 1.0F : 0.0F"),380 # int-to-boolean conversion381 operation("i2b", 1, source_types=(uint_type, int_type), dest_type=bool_type, c_expression="{src0} ? true : false"),382 # Boolean-to-int conversion383 operation("b2i", 1, source_types=(bool_type,), dest_type=int_type, c_expression="{src0} ? 1 : 0"),384 # Unsigned-to-float conversion.385 operation("u2f", 1, source_types=(uint_type,), dest_type=float_type, c_expression="(float) {src0}"),386 # Integer-to-unsigned conversion.387 operation("i2u", 1, source_types=(int_type,), dest_type=uint_type, c_expression="{src0}"),388 # Unsigned-to-integer conversion.389 operation("u2i", 1, source_types=(uint_type,), dest_type=int_type, c_expression="{src0}"),390 # Double-to-float conversion.391 operation("d2f", 1, source_types=(double_type,), dest_type=float_type, c_expression="{src0}"),392 # Float-to-double conversion.393 operation("f2d", 1, source_types=(float_type,), dest_type=double_type, c_expression="{src0}"),394 # Double-to-integer conversion.395 operation("d2i", 1, source_types=(double_type,), dest_type=int_type, c_expression="{src0}"),396 # Integer-to-double conversion.397 operation("i2d", 1, source_types=(int_type,), dest_type=double_type, c_expression="{src0}"),398 # Double-to-unsigned conversion.399 operation("d2u", 1, source_types=(double_type,), dest_type=uint_type, c_expression="{src0}"),400 # Unsigned-to-double conversion.401 operation("u2d", 1, source_types=(uint_type,), dest_type=double_type, c_expression="{src0}"),402 # Double-to-boolean conversion.403 operation("d2b", 1, source_types=(double_type,), dest_type=bool_type, c_expression="{src0} != 0.0"),404 # 'Bit-identical int-to-float "conversion"405 operation("bitcast_i2f", 1, source_types=(int_type,), dest_type=float_type, c_expression="bitcast_u2f({src0})"),406 # 'Bit-identical float-to-int "conversion"407 operation("bitcast_f2i", 1, source_types=(float_type,), dest_type=int_type, c_expression="bitcast_f2u({src0})"),408 # 'Bit-identical uint-to-float "conversion"409 operation("bitcast_u2f", 1, source_types=(uint_type,), dest_type=float_type, c_expression="bitcast_u2f({src0})"),410 # 'Bit-identical float-to-uint "conversion"411 operation("bitcast_f2u", 1, source_types=(float_type,), dest_type=uint_type, c_expression="bitcast_f2u({src0})"),412 # Bit-identical u64-to-double "conversion"413 operation("bitcast_u642d", 1, source_types=(uint64_type,), dest_type=double_type, c_expression="bitcast_u642d({src0})"),414 # Bit-identical i64-to-double "conversion"415 operation("bitcast_i642d", 1, source_types=(int64_type,), dest_type=double_type, c_expression="bitcast_i642d({src0})"),416 # Bit-identical double-to_u64 "conversion"417 operation("bitcast_d2u64", 1, source_types=(double_type,), dest_type=uint64_type, c_expression="bitcast_d2u64({src0})"),418 # Bit-identical double-to-i64 "conversion"419 operation("bitcast_d2i64", 1, source_types=(double_type,), dest_type=int64_type, c_expression="bitcast_d2i64({src0})"),420 # i64-to-i32 conversion421 operation("i642i", 1, source_types=(int64_type,), dest_type=int_type, c_expression="{src0}"),422 # ui64-to-i32 conversion423 operation("u642i", 1, source_types=(uint64_type,), dest_type=int_type, c_expression="{src0}"),424 operation("i642u", 1, source_types=(int64_type,), dest_type=uint_type, c_expression="{src0}"),425 operation("u642u", 1, source_types=(uint64_type,), dest_type=uint_type, c_expression="{src0}"),426 operation("i642b", 1, source_types=(int64_type,), dest_type=bool_type, c_expression="{src0} != 0"),427 operation("i642f", 1, source_types=(int64_type,), dest_type=float_type, c_expression="{src0}"),428 operation("u642f", 1, source_types=(uint64_type,), dest_type=float_type, c_expression="{src0}"),429 operation("i642d", 1, source_types=(int64_type,), dest_type=double_type, c_expression="{src0}"),430 operation("u642d", 1, source_types=(uint64_type,), dest_type=double_type, c_expression="{src0}"),431 operation("i2i64", 1, source_types=(int_type,), dest_type=int64_type, c_expression="{src0}"),432 operation("u2i64", 1, source_types=(uint_type,), dest_type=int64_type, c_expression="{src0}"),433 operation("b2i64", 1, source_types=(bool_type,), dest_type=int64_type, c_expression="{src0}"),434 operation("f2i64", 1, source_types=(float_type,), dest_type=int64_type, c_expression="{src0}"),435 operation("d2i64", 1, source_types=(double_type,), dest_type=int64_type, c_expression="{src0}"),436 operation("i2u64", 1, source_types=(int_type,), dest_type=uint64_type, c_expression="{src0}"),437 operation("u2u64", 1, source_types=(uint_type,), dest_type=uint64_type, c_expression="{src0}"),438 operation("f2u64", 1, source_types=(float_type,), dest_type=uint64_type, c_expression="{src0}"),439 operation("d2u64", 1, source_types=(double_type,), dest_type=uint64_type, c_expression="{src0}"),440 operation("u642i64", 1, source_types=(uint64_type,), dest_type=int64_type, c_expression="{src0}"),441 operation("i642u64", 1, source_types=(int64_type,), dest_type=uint64_type, c_expression="{src0}"),442 # Unary floating-point rounding operations.443 operation("trunc", 1, source_types=real_types, c_expression={'f': "truncf({src0})", 'd': "trunc({src0})"}),444 operation("ceil", 1, source_types=real_types, c_expression={'f': "ceilf({src0})", 'd': "ceil({src0})"}),445 operation("floor", 1, source_types=real_types, c_expression={'f': "floorf({src0})", 'd': "floor({src0})"}),446 operation("fract", 1, source_types=real_types, c_expression={'f': "{src0} - floorf({src0})", 'd': "{src0} - floor({src0})"}),447 operation("round_even", 1, source_types=real_types, c_expression={'f': "_mesa_roundevenf({src0})", 'd': "_mesa_roundeven({src0})"}),448 # Trigonometric operations.449 operation("sin", 1, source_types=(float_type,), c_expression="sinf({src0})"),450 operation("cos", 1, source_types=(float_type,), c_expression="cosf({src0})"),451 # Partial derivatives.452 operation("dFdx", 1, source_types=(float_type,), c_expression="0.0f"),453 operation("dFdx_coarse", 1, printable_name="dFdxCoarse", source_types=(float_type,), c_expression="0.0f"),454 operation("dFdx_fine", 1, printable_name="dFdxFine", source_types=(float_type,), c_expression="0.0f"),455 operation("dFdy", 1, source_types=(float_type,), c_expression="0.0f"),456 operation("dFdy_coarse", 1, printable_name="dFdyCoarse", source_types=(float_type,), c_expression="0.0f"),457 operation("dFdy_fine", 1, printable_name="dFdyFine", source_types=(float_type,), c_expression="0.0f"),458 # Floating point pack and unpack operations.459 operation("pack_snorm_2x16", 1, printable_name="packSnorm2x16", source_types=(float_type,), dest_type=uint_type, c_expression="pack_2x16(pack_snorm_1x16, op[0]->value.f[0], op[0]->value.f[1])", flags=horizontal_operation),460 operation("pack_snorm_4x8", 1, printable_name="packSnorm4x8", source_types=(float_type,), dest_type=uint_type, c_expression="pack_4x8(pack_snorm_1x8, op[0]->value.f[0], op[0]->value.f[1], op[0]->value.f[2], op[0]->value.f[3])", flags=horizontal_operation),461 operation("pack_unorm_2x16", 1, printable_name="packUnorm2x16", source_types=(float_type,), dest_type=uint_type, c_expression="pack_2x16(pack_unorm_1x16, op[0]->value.f[0], op[0]->value.f[1])", flags=horizontal_operation),462 operation("pack_unorm_4x8", 1, printable_name="packUnorm4x8", source_types=(float_type,), dest_type=uint_type, c_expression="pack_4x8(pack_unorm_1x8, op[0]->value.f[0], op[0]->value.f[1], op[0]->value.f[2], op[0]->value.f[3])", flags=horizontal_operation),463 operation("pack_half_2x16", 1, printable_name="packHalf2x16", source_types=(float_type,), dest_type=uint_type, c_expression="pack_2x16(pack_half_1x16, op[0]->value.f[0], op[0]->value.f[1])", flags=horizontal_operation),464 operation("unpack_snorm_2x16", 1, printable_name="unpackSnorm2x16", source_types=(uint_type,), dest_type=float_type, c_expression="unpack_2x16(unpack_snorm_1x16, op[0]->value.u[0], &data.f[0], &data.f[1])", flags=frozenset((horizontal_operation, non_assign_operation))),465 operation("unpack_snorm_4x8", 1, printable_name="unpackSnorm4x8", source_types=(uint_type,), dest_type=float_type, c_expression="unpack_4x8(unpack_snorm_1x8, op[0]->value.u[0], &data.f[0], &data.f[1], &data.f[2], &data.f[3])", flags=frozenset((horizontal_operation, non_assign_operation))),466 operation("unpack_unorm_2x16", 1, printable_name="unpackUnorm2x16", source_types=(uint_type,), dest_type=float_type, c_expression="unpack_2x16(unpack_unorm_1x16, op[0]->value.u[0], &data.f[0], &data.f[1])", flags=frozenset((horizontal_operation, non_assign_operation))),467 operation("unpack_unorm_4x8", 1, printable_name="unpackUnorm4x8", source_types=(uint_type,), dest_type=float_type, c_expression="unpack_4x8(unpack_unorm_1x8, op[0]->value.u[0], &data.f[0], &data.f[1], &data.f[2], &data.f[3])", flags=frozenset((horizontal_operation, non_assign_operation))),468 operation("unpack_half_2x16", 1, printable_name="unpackHalf2x16", source_types=(uint_type,), dest_type=float_type, c_expression="unpack_2x16(unpack_half_1x16, op[0]->value.u[0], &data.f[0], &data.f[1])", flags=frozenset((horizontal_operation, non_assign_operation))),469 # Bit operations, part of ARB_gpu_shader5.470 operation("bitfield_reverse", 1, source_types=(uint_type, int_type), c_expression="bitfield_reverse({src0})"),471 operation("bit_count", 1, source_types=(uint_type, int_type), dest_type=int_type, c_expression="_mesa_bitcount({src0})"),472 operation("find_msb", 1, source_types=(uint_type, int_type), dest_type=int_type, c_expression={'u': "find_msb_uint({src0})", 'i': "find_msb_int({src0})"}),473 operation("find_lsb", 1, source_types=(uint_type, int_type), dest_type=int_type, c_expression="find_msb_uint({src0} & -{src0})"),474 operation("saturate", 1, printable_name="sat", source_types=(float_type,), c_expression="CLAMP({src0}, 0.0f, 1.0f)"),475 # Double packing, part of ARB_gpu_shader_fp64.476 operation("pack_double_2x32", 1, printable_name="packDouble2x32", source_types=(uint_type,), dest_type=double_type, c_expression="memcpy(&data.d[0], &op[0]->value.u[0], sizeof(double))", flags=frozenset((horizontal_operation, non_assign_operation))),477 operation("unpack_double_2x32", 1, printable_name="unpackDouble2x32", source_types=(double_type,), dest_type=uint_type, c_expression="memcpy(&data.u[0], &op[0]->value.d[0], sizeof(double))", flags=frozenset((horizontal_operation, non_assign_operation))),478 # Sampler/Image packing, part of ARB_bindless_texture.479 operation("pack_sampler_2x32", 1, printable_name="packSampler2x32", source_types=(uint_type,), dest_type=uint64_type, c_expression="memcpy(&data.u64[0], &op[0]->value.u[0], sizeof(uint64_t))", flags=frozenset((horizontal_operation, non_assign_operation))),480 operation("pack_image_2x32", 1, printable_name="packImage2x32", source_types=(uint_type,), dest_type=uint64_type, c_expression="memcpy(&data.u64[0], &op[0]->value.u[0], sizeof(uint64_t))", flags=frozenset((horizontal_operation, non_assign_operation))),481 operation("unpack_sampler_2x32", 1, printable_name="unpackSampler2x32", source_types=(uint64_type,), dest_type=uint_type, c_expression="memcpy(&data.u[0], &op[0]->value.u64[0], sizeof(uint64_t))", flags=frozenset((horizontal_operation, non_assign_operation))),482 operation("unpack_image_2x32", 1, printable_name="unpackImage2x32", source_types=(uint64_type,), dest_type=uint_type, c_expression="memcpy(&data.u[0], &op[0]->value.u64[0], sizeof(uint64_t))", flags=frozenset((horizontal_operation, non_assign_operation))),483 operation("frexp_sig", 1),484 operation("frexp_exp", 1),485 operation("noise", 1),486 operation("subroutine_to_int", 1),487 # Interpolate fs input at centroid488 #489 # operand0 is the fs input.490 operation("interpolate_at_centroid", 1),491 # Ask the driver for the total size of a buffer block.492 # operand0 is the ir_constant buffer block index in the linked shader.493 operation("get_buffer_size", 1),494 # Calculate length of an unsized array inside a buffer block.495 # This opcode is going to be replaced in a lowering pass inside496 # the linker.497 #498 # operand0 is the unsized array's ir_value for the calculation499 # of its length.500 operation("ssbo_unsized_array_length", 1),501 # 64-bit integer packing ops.502 operation("pack_int_2x32", 1, printable_name="packInt2x32", source_types=(int_type,), dest_type=int64_type, c_expression="memcpy(&data.i64[0], &op[0]->value.i[0], sizeof(int64_t))", flags=frozenset((horizontal_operation, non_assign_operation))),503 operation("pack_uint_2x32", 1, printable_name="packUint2x32", source_types=(uint_type,), dest_type=uint64_type, c_expression="memcpy(&data.u64[0], &op[0]->value.u[0], sizeof(uint64_t))", flags=frozenset((horizontal_operation, non_assign_operation))),504 operation("unpack_int_2x32", 1, printable_name="unpackInt2x32", source_types=(int64_type,), dest_type=int_type, c_expression="memcpy(&data.i[0], &op[0]->value.i64[0], sizeof(int64_t))", flags=frozenset((horizontal_operation, non_assign_operation))),505 operation("unpack_uint_2x32", 1, printable_name="unpackUint2x32", source_types=(uint64_type,), dest_type=uint_type, c_expression="memcpy(&data.u[0], &op[0]->value.u64[0], sizeof(uint64_t))", flags=frozenset((horizontal_operation, non_assign_operation))),506 operation("add", 2, printable_name="+", source_types=numeric_types, c_expression="{src0} + {src1}", flags=vector_scalar_operation),507 operation("sub", 2, printable_name="-", source_types=numeric_types, c_expression="{src0} - {src1}", flags=vector_scalar_operation),508 # "Floating-point or low 32-bit integer multiply."509 operation("mul", 2, printable_name="*", source_types=numeric_types, c_expression="{src0} * {src1}"),510 operation("imul_high", 2), # Calculates the high 32-bits of a 64-bit multiply.511 operation("div", 2, printable_name="/", source_types=numeric_types, c_expression={'u': "{src1} == 0 ? 0 : {src0} / {src1}", 'i': "{src1} == 0 ? 0 : {src0} / {src1}", 'u64': "{src1} == 0 ? 0 : {src0} / {src1}", 'i64': "{src1} == 0 ? 0 : {src0} / {src1}", 'default': "{src0} / {src1}"}, flags=vector_scalar_operation),512 # Returns the carry resulting from the addition of the two arguments.513 operation("carry", 2),514 # Returns the borrow resulting from the subtraction of the second argument515 # from the first argument.516 operation("borrow", 2),517 # Either (vector % vector) or (vector % scalar)518 #519 # We don't use fmod because it rounds toward zero; GLSL specifies the use520 # of floor.521 operation("mod", 2, printable_name="%", source_types=numeric_types, c_expression={'u': "{src1} == 0 ? 0 : {src0} % {src1}", 'i': "{src1} == 0 ? 0 : {src0} % {src1}", 'f': "{src0} - {src1} * floorf({src0} / {src1})", 'd': "{src0} - {src1} * floor({src0} / {src1})", 'u64': "{src1} == 0 ? 0 : {src0} % {src1}", 'i64': "{src1} == 0 ? 0 : {src0} % {src1}"}, flags=vector_scalar_operation),522 # Binary comparison operators which return a boolean vector.523 # The type of both operands must be equal.524 operation("less", 2, printable_name="<", source_types=numeric_types, dest_type=bool_type, c_expression="{src0} < {src1}"),525 operation("gequal", 2, printable_name=">=", source_types=numeric_types, dest_type=bool_type, c_expression="{src0} >= {src1}"),526 operation("equal", 2, printable_name="==", source_types=all_types, dest_type=bool_type, c_expression="{src0} == {src1}"),527 operation("nequal", 2, printable_name="!=", source_types=all_types, dest_type=bool_type, c_expression="{src0} != {src1}"),528 # Returns single boolean for whether all components of operands[0]529 # equal the components of operands[1].530 operation("all_equal", 2, source_types=all_types, dest_type=bool_type, c_expression="op[0]->has_value(op[1])", flags=frozenset((horizontal_operation, types_identical_operation))),531 # Returns single boolean for whether any component of operands[0]532 # is not equal to the corresponding component of operands[1].533 operation("any_nequal", 2, source_types=all_types, dest_type=bool_type, c_expression="!op[0]->has_value(op[1])", flags=frozenset((horizontal_operation, types_identical_operation))),534 # Bit-wise binary operations.535 operation("lshift", 2, printable_name="<<", source_types=integer_types, c_expression="{src0} << {src1}", flags=frozenset((vector_scalar_operation, mixed_type_operation))),536 operation("rshift", 2, printable_name=">>", source_types=integer_types, c_expression="{src0} >> {src1}", flags=frozenset((vector_scalar_operation, mixed_type_operation))),537 operation("bit_and", 2, printable_name="&", source_types=integer_types, c_expression="{src0} & {src1}", flags=vector_scalar_operation),538 operation("bit_xor", 2, printable_name="^", source_types=integer_types, c_expression="{src0} ^ {src1}", flags=vector_scalar_operation),539 operation("bit_or", 2, printable_name="|", source_types=integer_types, c_expression="{src0} | {src1}", flags=vector_scalar_operation),540 operation("logic_and", 2, printable_name="&&", source_types=(bool_type,), c_expression="{src0} && {src1}"),541 operation("logic_xor", 2, printable_name="^^", source_types=(bool_type,), c_expression="{src0} != {src1}"),542 operation("logic_or", 2, printable_name="||", source_types=(bool_type,), c_expression="{src0} || {src1}"),543 operation("dot", 2, source_types=real_types, c_expression={'f': "dot_f(op[0], op[1])", 'd': "dot_d(op[0], op[1])"}, flags=horizontal_operation),544 operation("min", 2, source_types=numeric_types, c_expression="MIN2({src0}, {src1})", flags=vector_scalar_operation),545 operation("max", 2, source_types=numeric_types, c_expression="MAX2({src0}, {src1})", flags=vector_scalar_operation),546 operation("pow", 2, source_types=(float_type,), c_expression="powf({src0}, {src1})"),547 # Load a value the size of a given GLSL type from a uniform block.548 #549 # operand0 is the ir_constant uniform block index in the linked shader.550 # operand1 is a byte offset within the uniform block.551 operation("ubo_load", 2),552 # Multiplies a number by two to a power, part of ARB_gpu_shader5.553 operation("ldexp", 2,554 all_signatures=((float_type, (float_type, int_type)),555 (double_type, (double_type, int_type))),556 c_expression={'f': "ldexpf_flush_subnormal({src0}, {src1})",557 'd': "ldexp_flush_subnormal({src0}, {src1})"}),558 # Extract a scalar from a vector559 #560 # operand0 is the vector561 # operand1 is the index of the field to read from operand0562 operation("vector_extract", 2, source_types=all_types, c_expression="anything-except-None"),563 # Interpolate fs input at offset564 #565 # operand0 is the fs input566 # operand1 is the offset from the pixel center567 operation("interpolate_at_offset", 2),568 # Interpolate fs input at sample position569 #570 # operand0 is the fs input571 # operand1 is the sample ID572 operation("interpolate_at_sample", 2),573 # Fused floating-point multiply-add, part of ARB_gpu_shader5.574 operation("fma", 3, source_types=real_types, c_expression="{src0} * {src1} + {src2}"),575 operation("lrp", 3, source_types=real_types, c_expression={'f': "{src0} * (1.0f - {src2}) + ({src1} * {src2})", 'd': "{src0} * (1.0 - {src2}) + ({src1} * {src2})"}),576 # Conditional Select577 #578 # A vector conditional select instruction (like ?:, but operating per-579 # component on vectors).580 #581 # See also lower_instructions_visitor::ldexp_to_arith582 operation("csel", 3,583 all_signatures=zip(all_types, zip(len(all_types) * (bool_type,), all_types, all_types)),584 c_expression="{src0} ? {src1} : {src2}"),585 operation("bitfield_extract", 3,586 all_signatures=((int_type, (uint_type, int_type, int_type)),587 (int_type, (int_type, int_type, int_type))),588 c_expression={'u': "bitfield_extract_uint({src0}, {src1}, {src2})",589 'i': "bitfield_extract_int({src0}, {src1}, {src2})"}),590 # Generate a value with one field of a vector changed591 #592 # operand0 is the vector593 # operand1 is the value to write into the vector result594 # operand2 is the index in operand0 to be modified595 operation("vector_insert", 3, source_types=all_types, c_expression="anything-except-None"),596 operation("bitfield_insert", 4,597 all_signatures=((uint_type, (uint_type, uint_type, int_type, int_type)),598 (int_type, (int_type, int_type, int_type, int_type))),599 c_expression="bitfield_insert({src0}, {src1}, {src2}, {src3})"),600 operation("vector", 4, source_types=all_types, c_expression="anything-except-None"),601]602if __name__ == "__main__":603 copyright = """/*604 * Copyright (C) 2010 Intel Corporation605 *606 * Permission is hereby granted, free of charge, to any person obtaining a607 * copy of this software and associated documentation files (the "Software"),608 * to deal in the Software without restriction, including without limitation609 * the rights to use, copy, modify, merge, publish, distribute, sublicense,610 * and/or sell copies of the Software, and to permit persons to whom the611 * Software is furnished to do so, subject to the following conditions:612 *613 * The above copyright notice and this permission notice (including the next614 * paragraph) shall be included in all copies or substantial portions of the615 * Software.616 *617 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR618 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,619 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL620 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER621 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING622 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER623 * DEALINGS IN THE SOFTWARE.624 */625"""626 enum_template = mako.template.Template(copyright + """627enum ir_expression_operation {628% for item in values:629 ${item.get_enum_name()},630% endfor631 /* Sentinels marking the last of each kind of operation. */632% for item in lasts:633 ir_last_${("un", "bin", "tri", "quad")[item.num_operands - 1]}op = ${item.get_enum_name()},634% endfor635 ir_last_opcode = ir_quadop_${lasts[3].name}636};""")637 strings_template = mako.template.Template(copyright + """638const char *const ir_expression_operation_strings[] = {639% for item in values:640 "${item.printable_name}",641% endfor642};643const char *const ir_expression_operation_enum_strings[] = {644% for item in values:645 "${item.name}",646% endfor647};""")648 constant_template = mako.template.Template("""\649 switch (this->operation) {650% for op in values:651 % if op.c_expression is not None:652${op.get_template()}653 % endif654% endfor655 default:656 /* FINISHME: Should handle all expression types. */657 return NULL;658 }659""")660 if sys.argv[1] == "enum":661 lasts = [None, None, None, None]662 for item in reversed(ir_expression_operation):663 i = item.num_operands - 1664 if lasts[i] is None:665 lasts[i] = item666 print(enum_template.render(values=ir_expression_operation,667 lasts=lasts))668 elif sys.argv[1] == "strings":669 print(strings_template.render(values=ir_expression_operation))670 elif sys.argv[1] == "constant":...
models.py
Source:models.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3from django.db import models4from django.utils.translation import ugettext_lazy as _56class Country(models.Model):7 """8 International Organization for Standardization (ISO) 3166-1 Country list9 10 * ``iso`` = ISO 3166-1 alpha-211 * ``name`` = Official country names used by the ISO 3166/MA in capital letters12 * ``printable_name`` = Printable country names for in-text use13 * ``iso3`` = ISO 3166-1 alpha-314 * ``numcode`` = ISO 3166-1 numeric15 16 Note::17 This model is fixed to the database table 'country' to be more general.18 Change ``db_table`` if this cause conflicts with your database layout.19 Or comment out the line for default django behaviour.20 21 """22 iso = models.CharField(_('ISO alpha-2'), max_length=2, primary_key=True)23 name = models.CharField(_('Official name (CAPS)'), max_length=128)24 printable_name = models.CharField(_('Country name'), max_length=128)25 iso3 = models.CharField(_('ISO alpha-3'), max_length=3, null=True)26 numcode = models.PositiveSmallIntegerField(_('ISO numeric'), null=True)27 28 class Meta:29 db_table = 'country'30 verbose_name = _('Country')31 verbose_name_plural = _('Countries')32 ordering = ('name',)33 34 class Admin:35 list_display = ('printable_name', 'iso',)36 37 def __unicode__(self):38 return self.printable_name394041class UsState(models.Model):42 """43 United States Postal Service (USPS) State Abbreviations44 45 Note::46 This model is fixed to the database table 'usstate' to be more general.47 Change ``db_table`` if this cause conflicts with your database layout.48 Or comment out the line for default django behaviour.49 50 """51 id = models.AutoField(primary_key=True)52 name = models.CharField(_('State name'), max_length=50, null=False)53 abbrev = models.CharField(_('Abbreviation'), max_length=2, null=False)5455 class Meta:56 db_table = 'usstate'57 verbose_name = _('US State')58 verbose_name_plural = _('US States')59 ordering = ('name',)6061 class Admin:62 list_display = ('name', 'abbrev',)6364 def __unicode__(self):
...
snippet.py
Source:snippet.py
1# -*- encoding: utf-8 -*-2from __future__ import print_function3from enum import Enum4class PrintableValue(object):5 def __init__(self, value, printable_name):6 self.value = value7 self.printable_name = printable_name8class EnumWithName(Enum):9 def __new__(cls, value):10 obj = object.__new__(cls)11 obj._value_ = value.value12 obj.printable_name = value.printable_name13 return obj14class Country(EnumWithName):15 AU = PrintableValue('AU', 'Australia')16 US = PrintableValue('US', 'United States of America')17 CA = PrintableValue('CA', 'Canada')18us = Country('US')19print('Enum name:', us.name)20print('Enum value:', us.value)21print('Printable name:', us.printable_name)22print("US == US:", Country('US') == us)...
b3f4e178bcf9957e25a1.py
Source:b3f4e178bcf9957e25a1.py
1# -*- encoding: utf-8 -*-2from __future__ import print_function3from enum import Enum4class PrintableValue(object):5 def __init__(self, value, printable_name):6 self.value = value7 self.printable_name = printable_name8class EnumWithName(Enum):9 def __new__(cls, value):10 obj = object.__new__(cls)11 obj._value_ = value.value12 obj.printable_name = value.printable_name13 return obj14class Country(EnumWithName):15 AU = PrintableValue('AU', 'Australia')16 US = PrintableValue('US', 'United States of America')17 CA = PrintableValue('CA', 'Canada')18us = Country('US')19print('Enum name:', us.name)20print('Enum value:', us.value)21print('Printable name:', us.printable_name)22print("US == US:", Country('US') == us)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!