dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.97k stars 4.66k forks source link

Question: Why was this code written this way #12458

Closed grant-d closed 4 years ago

grant-d commented 5 years ago

Idle curiosity: I just came across the following generated (ie via .tt) code. Can anyone explain why it was written this way and not just a direct return. Perhaps it serves as a reinterpret or the like?

namespace System.Numerics
{
    internal class ConstantHelper
    {
        [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
        public static byte GetByteWithAllBitsSet() // Also GetInt16, GetInt32, etc
        {
            byte value = 0;
            unsafe
            {
                unchecked
                {
                    *((byte*)&value) = (byte)0xff;
                }
            }
            return value;
        }

https://github.com/dotnet/coreclr/blob/030a3ea9b8dbeae89c90d34441d4d9a1cf4a7de6/src/System.Private.CoreLib/shared/System/Numerics/ConstantHelper.cs

am11 commented 5 years ago

See https://github.com/dotnet/coreclr/issues/19139. It's up for grabs. :)

grant-d commented 5 years ago

Thanks @am11, I might pick up that PR