Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simd_mul vs * operator

Tags:

xcode

swift

simd

In many official Apple samples and Xcode templates, two float4x4 matrices are multiplied using simd_mul, like:

simd_mul(viewMatrix, modelMatrix)

Now, simd.h provides a * operator for it's types, so the above line could have been written as

viewMatrix * modelMatrix

Is there any difference, either in usage or performance between the above two methods? I can only look into simd.h so I have no idea how is the method actually implemented, but I guess it's just a call to simd_mul.

like image 343
hyperknot Avatar asked Jan 26 '18 20:01

hyperknot


1 Answers

There is no difference. Swift is open source, and in simd.swift.gyb you'll find

%   for k in [2,3,4]:
  /// Matrix multiplication (the "usual" matrix product, not the elementwise
  /// product).
%    restype = ctype[type] + str(k) + 'x' + str(rows)
%    rhstype = ctype[type] + str(k) + 'x' + str(cols)
  @_transparent
  public static func *(lhs: ${mattype}, rhs: ${rhstype}) -> ${restype} {
    return simd_mul(lhs, rhs)
  }

%   end # for k in [2,3,4]

.gyb files (“Generate Your Boilerplate“) are pre-processed by a special Swift preprocessor (compare https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20151207/000226.html). The above code is nested inside loops

%for type in floating_types:
% for rows in [2,3,4]:
%  for cols in [2,3,4]:
%   mattype = 'simd_' + ctype[type] + str(cols) + 'x' + str(rows)

// ....

%  end # for cols in [2,3,4]
% end # for rows in [2,3,4]
%end # for type in floating_types

so that it ultimately expands to * operator definitions for all possible matrix operand combinations.

like image 114
Martin R Avatar answered Oct 14 '22 05:10

Martin R