1 Imports System.Numerics
2 Imports System.Runtime.CompilerServices
3 Imports System.Runtime.InteropServices
4 Imports Microsoft.VisualBasic.Language
5
6 Namespace Math.Information
7
8     Public Module UncheckHelpers
9
10         ReadOnly sizeOfInt64% = Marshal.SizeOf(Long.MaxValue)
11         ReadOnly sizeOfInt32% = Marshal.SizeOf(Integer.MaxValue)
12
13         <MethodImpl(MethodImplOptions.AggressiveInlining)>
14         <Extension>
15         Public Function ToTruncateInt64(bi As BigInteger) As Long
16             With bi.ToByteArray
17                 If .Length < sizeOfInt64 Then
18                     Return CType(bi, Long)
19                 Else
20                     Return BitConverter.ToInt64(.ByRef, Scan0)
21                 End If
22             End With
23         End Function
24
25         <MethodImpl(MethodImplOptions.AggressiveInlining)>
26         <Extension>
27         Public Function ToTruncateInt32(bi As BigInteger) As Integer
28             With bi.ToByteArray
29                 If .Length < sizeOfInt32 Then
30                     Return CType(bi, Long)
31                 Else
32                     Return BitConverter.ToInt32(.ByRef, Scan0)
33                 End If
34             End With
35         End Function
36     End Module
37 End Namespace