Compiler intrinsics in Burst?

Any word on the subject from Unity’s developers? I’m particularly interested in things corresponding to GCC’s __builtin_clz or C#'s _BitScanReverse.

math.lzcnt
If it isn’t in the Burst package, it is probably in the math package. Burst has intrinsic understanding of the math package and will replace invocations with compiler metadata that leads to raw instructions.

1 Like

Thank you! ^-^

Random note

Is the tzcnt documentation wrong for all the intX implementations?

        /// <summary>Returns number of trailing zeros in the binary representations of an int value.</summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int tzcnt(int x) { return tzcnt((uint)x); }

        /// <summary>Returns the componentwise number of leading zeros in the binary representations of an int2 vector.</summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int2 tzcnt(int2 x) { return int2(tzcnt(x.x), tzcnt(x.y)); }

        /// <summary>Returns the componentwise number of leading zeros in the binary representations of an int3 vector.</summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int3 tzcnt(int3 v) { return int3(tzcnt(v.x), tzcnt(v.y), tzcnt(v.z)); }

        /// <summary>Returns the componentwise number of leading zeros in the binary representations of an int4 vector.</summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int4 tzcnt(int4 v) { return int4(tzcnt(v.x), tzcnt(v.y), tzcnt(v.z), tzcnt(v.w)); }

Seems to be using the lzcnt documentation.

2 Likes

I think you’re onto something… :stuck_out_tongue:

Thanks for reporting the documentation error, I’ve forwarded it to the right people :slight_smile:

2 Likes

…and in case anyone wants to follow up, see here: Fix incorrect tzcnt documentation. by unpacklo · Pull Request #175 · Unity-Technologies/Unity.Mathematics · GitHub

1 Like

another doc issue.

        /// <summary>Returns a float2x2 matrix representing a counter-clockwise rotation of angle degrees.</summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static float2x2 Rotate(float angle)
        {
            float s, c;
            sincos(angle, out s, out c);
            return float2x2(c, -s,
                            s,  c);
        }

It’s a counter-clockwise rotation of angle radian instead of degrees.

@s_schoener also…

In Unity.Entity.Transforms

   public struct LocalToWorld : IComponentData
   {
       public float4x4 Value;
       public float3 Right => new float3(Value.c0.x, Value.c0.y, Value.c0.z);
       public float3 Up => new float3(Value.c1.x, Value.c1.y, Value.c1.z);
       public float3 Forward => new float3(Value.c2.x, Value.c2.y, Value.c2.z);
       public float3 Position => new float3(Value.c3.x, Value.c3.y, Value.c3.z);
       public quaternion Rotation => new quaternion(Value);
   }

new quaternion(float4x4 ) is used directly.
But It looks like that function is not expecting and scale in the matrix, even uniform scale.
So when the matrix has scale the return quaternion would be wrong. from my tests.
I tried with my own decompose Shear Scale Rotation function and the return quaternion is right.
Is this an intentional optimization when L2W is not expected to have scale.
Or is this a bug?
P.S. the float4x4 I tested is an orthonormal transform not even sheared just uniformly scaled, with none-burst version new quaternion(float4x4 ).

also Right/Up/Forward are all not normalized. but that’s fine.

originally Dose math.mul(quaternion,quaternion) return value needs to be normalized? · Issue #171 · Unity-Technologies/Unity.Mathematics · GitHub