Skip to content

Commit d3a782e

Browse files
authored
Enable multiply and divide vector2/4 by scalar (#93061)
- refactor `type_is_simd_vector`
1 parent 26a809a commit d3a782e

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

src/mono/mono/mini/simd-intrinsics.c

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ check_no_intrinsic_cattr (MonoMethod *method)
192192
}
193193
}
194194

195+
static gboolean
196+
type_is_simd_vector (MonoType *type)
197+
{
198+
return m_class_is_simd_type (mono_class_from_mono_type_internal (type));
199+
}
195200
/*
196201
* Return a simd vreg for the simd value represented by SRC.
197202
* SRC is the 'this' argument to methods.
@@ -341,14 +346,10 @@ emit_simd_ins_for_binary_op (MonoCompile *cfg, MonoClass *klass, MonoMethodSigna
341346
case SN_Divide:
342347
case SN_op_Division: {
343348
const char *class_name = m_class_get_name (klass);
344-
if (!strcmp("Vector4", class_name) && fsig->params [1]->type == MONO_TYPE_R4) {
345-
// Handles Vector4 / scalar
346-
return handle_mul_div_by_scalar (cfg, klass, MONO_TYPE_R4, args [1]->dreg, args [0]->dreg, OP_FDIV);
347-
}
348-
if (strcmp ("Vector2", class_name) && strcmp ("Vector4", class_name) && strcmp ("Quaternion", class_name) && strcmp ("Plane", class_name)) {
349-
if ((fsig->params [0]->type == MONO_TYPE_GENERICINST) && (fsig->params [1]->type != MONO_TYPE_GENERICINST))
349+
if (strcmp ("Quaternion", class_name) && strcmp ("Plane", class_name)) {
350+
if (!type_is_simd_vector (fsig->params [1]))
350351
return handle_mul_div_by_scalar (cfg, klass, arg_type, args [1]->dreg, args [0]->dreg, OP_FDIV);
351-
else if ((fsig->params [0]->type == MONO_TYPE_GENERICINST) && (fsig->params [1]->type == MONO_TYPE_GENERICINST)) {
352+
else if (type_is_simd_vector (fsig->params [0]) && type_is_simd_vector (fsig->params [1])) {
352353
instc0 = OP_FDIV;
353354
break;
354355
} else {
@@ -367,20 +368,12 @@ emit_simd_ins_for_binary_op (MonoCompile *cfg, MonoClass *klass, MonoMethodSigna
367368
case SN_Multiply:
368369
case SN_op_Multiply: {
369370
const char *class_name = m_class_get_name (klass);
370-
if (!strcmp("Vector4", class_name)) {
371-
// Handles scalar * Vector4 and Vector4 * scalar
372-
if (fsig->params [0]->type == MONO_TYPE_R4) {
373-
return handle_mul_div_by_scalar (cfg, klass, MONO_TYPE_R4, args [0]->dreg, args [1]->dreg, OP_FMUL);
374-
} else if (fsig->params [1]->type == MONO_TYPE_R4) {
375-
return handle_mul_div_by_scalar (cfg, klass, MONO_TYPE_R4, args [1]->dreg, args [0]->dreg, OP_FMUL);
376-
}
377-
}
378-
if (strcmp ("Vector2", class_name) && strcmp ("Vector4", class_name) && strcmp ("Quaternion", class_name) && strcmp ("Plane", class_name)) {
379-
if (fsig->params [1]->type != MONO_TYPE_GENERICINST)
371+
if (strcmp ("Quaternion", class_name) && strcmp ("Plane", class_name)) {
372+
if (!type_is_simd_vector (fsig->params [1]))
380373
return handle_mul_div_by_scalar (cfg, klass, arg_type, args [1]->dreg, args [0]->dreg, OP_FMUL);
381-
else if (fsig->params [0]->type != MONO_TYPE_GENERICINST)
374+
else if (!type_is_simd_vector (fsig->params [0]))
382375
return handle_mul_div_by_scalar (cfg, klass, arg_type, args [0]->dreg, args [1]->dreg, OP_FMUL);
383-
else if ((fsig->params [0]->type == MONO_TYPE_GENERICINST) && (fsig->params [1]->type == MONO_TYPE_GENERICINST)) {
376+
else if (type_is_simd_vector (fsig->params [0]) && type_is_simd_vector (fsig->params [1])) {
384377
instc0 = OP_FMUL;
385378
break;
386379
} else {
@@ -486,12 +479,6 @@ emit_simd_ins_for_unary_op (MonoCompile *cfg, MonoClass *klass, MonoMethodSignat
486479
#endif
487480
}
488481

489-
static gboolean
490-
type_is_simd_vector (MonoType *type)
491-
{
492-
return type->type == MONO_TYPE_GENERICINST && m_class_is_simd_type (mono_class_from_mono_type_internal (type));
493-
}
494-
495482
static gboolean
496483
is_hw_intrinsics_class (MonoClass *klass, const char *name, gboolean *is_64bit)
497484
{
@@ -2906,8 +2893,6 @@ emit_vector_2_3_4 (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *f
29062893
// FIXME https://github.com/dotnet/runtime/issues/82408
29072894
if ((id == SN_op_Multiply || id == SN_Multiply || id == SN_op_Division || id == SN_Divide) && !strcmp (klass_name, "Quaternion"))
29082895
return NULL;
2909-
if (!(!fsig->hasthis && fsig->param_count == 2 && mono_metadata_type_equal (fsig->ret, type) && mono_metadata_type_equal (fsig->params [0], type) && mono_metadata_type_equal (fsig->params [1], type)))
2910-
return NULL;
29112896
return emit_simd_ins_for_binary_op (cfg, klass, fsig, args, MONO_TYPE_R4, id);
29122897
}
29132898
case SN_Dot: {

0 commit comments

Comments
 (0)