@@ -308,6 +308,34 @@ public static bool TryWriteBytes(Span<byte> destination, UInt128 value)
308308 return true ;
309309 }
310310
311+ /// <summary>
312+ /// Returns the specified <see cref="BFloat16"/> value as an array of bytes.
313+ /// </summary>
314+ /// <param name="value">The number to convert.</param>
315+ /// <returns>An array of bytes with length 2.</returns>
316+ public static unsafe byte [ ] GetBytes ( BFloat16 value )
317+ {
318+ byte [ ] bytes = new byte [ sizeof ( BFloat16 ) ] ;
319+ bool success = TryWriteBytes ( bytes , value ) ;
320+ Debug . Assert ( success ) ;
321+ return bytes ;
322+ }
323+
324+ /// <summary>
325+ /// Converts a <see cref="BFloat16"/> value into a span of bytes.
326+ /// </summary>
327+ /// <param name="destination">When this method returns, the bytes representing the converted <see cref="BFloat16"/> value.</param>
328+ /// <param name="value">The <see cref="BFloat16"/> value to convert.</param>
329+ /// <returns><see langword="true"/> if the conversion was successful; <see langword="false"/> otherwise.</returns>
330+ public static unsafe bool TryWriteBytes ( Span < byte > destination , BFloat16 value )
331+ {
332+ if ( destination . Length < sizeof ( BFloat16 ) )
333+ return false ;
334+
335+ Unsafe . WriteUnaligned ( ref MemoryMarshal . GetReference ( destination ) , value ) ;
336+ return true ;
337+ }
338+
311339 /// <summary>
312340 /// Returns the specified half-precision floating point value as an array of bytes.
313341 /// </summary>
@@ -693,6 +721,31 @@ public static UInt128 ToUInt128(ReadOnlySpan<byte> value)
693721 return Unsafe . ReadUnaligned < UInt128 > ( ref MemoryMarshal . GetReference ( value ) ) ;
694722 }
695723
724+ /// <summary>
725+ /// Returns a <see cref="BFloat16"/> number converted from two bytes at a specified position in a byte array.
726+ /// </summary>
727+ /// <param name="value">An array of bytes.</param>
728+ /// <param name="startIndex">The starting position within <paramref name="value"/>.</param>
729+ /// <returns>A <see cref="BFloat16"/> number signed integer formed by two bytes beginning at <paramref name="startIndex"/>.</returns>
730+ /// <exception cref="ArgumentException"><paramref name="startIndex"/> equals the length of <paramref name="value"/> minus 1.</exception>
731+ /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
732+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.</exception>
733+ public static BFloat16 ToBFloat16 ( byte [ ] value , int startIndex ) => Int16BitsToBFloat16 ( ToInt16 ( value , startIndex ) ) ;
734+
735+ /// <summary>
736+ /// Converts a read-only byte span into a <see cref="BFloat16"/> value.
737+ /// </summary>
738+ /// <param name="value">A read-only span containing the bytes to convert.</param>
739+ /// <returns>A <see cref="BFloat16"/> value representing the converted bytes.</returns>
740+ /// <exception cref="ArgumentOutOfRangeException">The length of <paramref name="value"/> is less than 2.</exception>
741+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
742+ public static unsafe BFloat16 ToBFloat16 ( ReadOnlySpan < byte > value )
743+ {
744+ if ( value . Length < sizeof ( BFloat16 ) )
745+ ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . value ) ;
746+ return Unsafe . ReadUnaligned < BFloat16 > ( ref MemoryMarshal . GetReference ( value ) ) ;
747+ }
748+
696749 /// <summary>
697750 /// Returns a half-precision floating point number converted from two bytes at a specified position in a byte array.
698751 /// </summary>
@@ -948,9 +1001,21 @@ public static bool ToBoolean(ReadOnlySpan<byte> value)
9481001 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
9491002 public static Half Int16BitsToHalf ( short value ) => new Half ( ( ushort ) ( value ) ) ;
9501003
951- internal static short BFloat16BitsToInt16 ( BFloat16 value ) => ( short ) value . _value ;
1004+ /// <summary>
1005+ /// Converts the specified <see cref="BFloat16"/> number to a 16-bit signed integer.
1006+ /// </summary>
1007+ /// <param name="value">The number to convert.</param>
1008+ /// <returns>A 16-bit signed integer whose bits are identical to <paramref name="value"/>.</returns>
1009+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1010+ public static short BFloat16ToInt16Bits ( BFloat16 value ) => ( short ) value . _value ;
9521011
953- internal static BFloat16 Int16BitsToBFloat16 ( short value ) => new BFloat16 ( ( ushort ) ( value ) ) ;
1012+ /// <summary>
1013+ /// Converts the specified 16-bit signed integer to a <see cref="BFloat16"/> number.
1014+ /// </summary>
1015+ /// <param name="value">The number to convert.</param>
1016+ /// <returns>A <see cref="BFloat16"/> number whose bits are identical to <paramref name="value"/>.</returns>
1017+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1018+ public static BFloat16 Int16BitsToBFloat16 ( short value ) => new BFloat16 ( ( ushort ) ( value ) ) ;
9541019
9551020 /// <summary>
9561021 /// Converts the specified double-precision floating point number to a 64-bit unsigned integer.
@@ -1006,8 +1071,22 @@ public static bool ToBoolean(ReadOnlySpan<byte> value)
10061071 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
10071072 public static Half UInt16BitsToHalf ( ushort value ) => new Half ( value ) ;
10081073
1009- internal static ushort BFloat16BitsToUInt16 ( BFloat16 value ) => value . _value ;
1074+ /// <summary>
1075+ /// Converts the specified <see cref="BFloat16"/> number to a 16-bit unsigned integer.
1076+ /// </summary>
1077+ /// <param name="value">The number to convert.</param>
1078+ /// <returns>A 16-bit unsigned integer whose bits are identical to <paramref name="value"/>.</returns>
1079+ [ CLSCompliant ( false ) ]
1080+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1081+ public static ushort BFloat16ToUInt16Bits ( BFloat16 value ) => value . _value ;
10101082
1011- internal static BFloat16 UInt16BitsToBFloat16 ( ushort value ) => new BFloat16 ( value ) ;
1083+ /// <summary>
1084+ /// Converts the specified 16-bit unsigned integer to a <see cref="BFloat16"/> number.
1085+ /// </summary>
1086+ /// <param name="value">The number to convert.</param>
1087+ /// <returns>A <see cref="BFloat16"/> number whose bits are identical to <paramref name="value"/>.</returns>
1088+ [ CLSCompliant ( false ) ]
1089+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1090+ public static BFloat16 UInt16BitsToBFloat16 ( ushort value ) => new BFloat16 ( value ) ;
10121091 }
10131092}
0 commit comments