Microsoft MVP성태의 닷넷 이야기
닷넷: 2182. C# - .NET 7부터 추가된 Int128, UInt128 [링크 복사], [링크+제목 복사],
조회: 2564
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)

C# - .NET 7부터 추가된 Int128, UInt128

(gcc 등에서는 지원하지만) Visual C++도 지원하지 않는 Int128을 닷넷 8부터 추가해 C# 언어에서도 사용할 수 있게 되었습니다. ^^

Int128 Struct
; https://learn.microsoft.com/en-us/dotnet/api/system.int128

단지, 다른 타입과는 달리 C# 측에서 대응하는 alias가 없어 BigInteger를 사용하듯이 써야 합니다.

{
    Int128 value = long.MaxValue;
    value *= long.MaxValue;
    Console.WriteLine(value); // 85070591730234615847396907784232501249
}

{
    Int128 value = Int128.Parse("85070591730234615847396907784232501249");
    Console.WriteLine(value);
}

개인적으로 이걸 보고 궁금했던 게, Interlocked 측에서의 지원이 있느냐는 것이었습니다. 아쉽게도 여전히 (8바이트) long 형식까지만 지원하지만, 그래도 아예 불가능한 것은 아닙니다. 만들면 되니까요? ^^




이를 위해 우리는 inline asm 기법을 사용해야 합니다.

C++의 inline asm 사용을 .NET으로 포팅하는 방법
; https://www.sysnet.pe.kr/2/0/1267

게다가 InterlockedCompareExchange128 API를 구현한 소스 코드가 GitHub에 있으니,

Execute a InterlockedCompareExchange128 natively from C#
; https://gist.github.com/jduncanator/ab17e4e476300d3eb0b7c19f6f38429a

기왕이면 여기에 함수 포인터를 곁들여,

C# 9.0 - (6) 함수 포인터(Function pointers)
; https://www.sysnet.pe.kr/2/0/12374

다음과 같은 식으로,

using System.Runtime.InteropServices;

namespace int128sample;
public unsafe class InterlockedExtension
{   
    // Execute a InterlockedCompareExchange128 natively from C#
    // ; https://gist.github.com/jduncanator/ab17e4e476300d3eb0b7c19f6f38429a
    static byte[] asmCmpXchg16b = new byte[] {
                0x48, 0x89, 0x5C, 0x24, 0x08,  // MOV [RSP+0x8], RBX
                0x49, 0x8B, 0x01,              // MOV RAX, [R9]
                0x49, 0x89, 0xCA,              // MOV R10, RCX
                0x48, 0x89, 0xD1,              // MOV RCX, RDX
                0x4C, 0x89, 0xC3,              // MOV RBX, R8
                0x49, 0x8B, 0x51, 0x08,        // MOV RDX, [R9+0x8]
                0xF0, 0x49, 0x0F, 0xC7, 0x0A,  // LOCK CMPXCHG16B [R10]
                0x48, 0x8B, 0x5C, 0x24, 0x08,  // MOV RBX, [RSP+0x8]
                0x49, 0x89, 0x01,              // MOV [R9], RAX
                0x0F, 0x94, 0xC0,              // SETE AL
                0x49, 0x89, 0x51, 0x08,        // MOV [R9+0x8], RDX
                0xC2, 0x00, 0x00               // RET 0
            };

    public static delegate* unmanaged[Stdcall, SuppressGCTransition] _InterlockedCompareExchange128;
    static GCHandle _InterlockedCompareExchange128Handle;

    static InterlockedExtension()
    {
        _InterlockedCompareExchange128Handle = GCHandle.Alloc(asmCmpXchg16b, GCHandleType.Pinned);
        nint pData = (nint)_InterlockedCompareExchange128Handle.AddrOfPinnedObject().ToPointer();
        
        EnsureMemoryIsExecutable(pData, asmCmpXchg16b.Length);
        _InterlockedCompareExchange128 = (delegate* unmanaged[Stdcall, SuppressGCTransition])pData;
    }

    // ...[생략]...
}

기반을 만들 수 있습니다. 간단하게 테스트 코드는 이렇게 만들 수 있고!

{
    Int128 value = 0;
    Int128 comparand = 0;
    InterlockedExtension._InterlockedCompareExchange128((long*)&value, 0, 1, (long*)&comparand);
    Console.WriteLine(value); // 출력 결과: 1
}




그런데 실행해 보면, 지난 글에서 다룬 것과 동일한 aligned 문제가,

Visual C++ - InterlockedCompareExchange128 사용 방법
; https://www.sysnet.pe.kr/2/0/13472#align16

GC Heap 또는 스택에 할당된 Int128 변수에 적용되므로 이런 예외가 확률적으로 발생하게 됩니다.

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at int128sample.Program.Main(System.String[])

만약 오류가 발생한다면 다음과 같이 변수 앞에 임시 조치를 취하면,

{
    long temporary = 0; // 8바이트 점유
    Int128 value = 0; // 이전에 8바이트 정렬이었다면 (운이 따르는 경우) temporary 변수로 인해 16바이트 위치로 변경
    // ...[생략]...
}

16바이트 정렬 효과를 갖게 돼 정상적으로 실행될 것입니다. 물론, 이 방법을 (release 빌드에서는 없어지는 문제도 있고, 최적화 시 재정렬될 수도 있으므로) 업무 코드에서 사용할 수는 없습니다. 그렇다면, C/C++의 경우 전역 변수를 사용하면 16바이트 정렬이 되었는데, C#은 어떨까요?

C#은 전역 변수라는 것이 없이, class 또는 struct 내에 static으로 흉내를 낼 수 있는데요,

internal unsafe class Program
{
    static Int128 g_value = 0;

    static void Main(string[] args)
    {
        fixed(Int128* ptr = &g_value)
        {
            Int128 value = 0;
            Int128 comparand = 0;
            InterlockedExtension._InterlockedCompareExchange128((long*)ptr, 0, 1, (long*)&comparand);
            Console.WriteLine(value);
        }
    }
}

이것 역시 확률적으로 crash가 발생합니다. 이유는, g_value가 GC Heap(HighFrequencyHeap)에 할당이 될 텐데 그런 경우 16바이트 정렬된 위치에 할당이 되리라는 것을 보장할 수 없기 때문입니다.

그렇다고 C#에 C/C++과 같은 "__declspec(align(16))"이 있는 것도 아니고... 난감하군요. ^^




자, 그렇다면 C#에서 해결해야 할 가장 큰 난제는 바로 정렬입니다. 이를 위해 StructLayout의 Pack이 있지만,

[StructLayout(LayoutKind.Sequential, Pack = 16)]
public struct Data
{
    byte __padding0; // (운에 따라) 이 위치가 0x0018이라면,
    public Int128 Value; // 이 위치는 16바이트를 건너 뛴 0x0028로 정렬
}

비록 alignment에 관여를 하긴 해도, 그것은 내부 멤버들의 정렬에 영향을 주는 것이기 때문에 애당초 저 구조체가 8바이트 정렬로 되는 것을 막을 수는 없습니다.

그렇다면 Win32 API를 interop 해야 할 것 같은데, 다행히 .NET 6부터 NativeMemory.AlignedAlloc이 제공되므로,

NativeMemory.AlignedAlloc(UIntPtr, UIntPtr) Method
; https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.nativememory.alignedalloc

이것을 감싸 다음과 같은 도우미 타입을 만들 수 있습니다.

public unsafe class NativeInt128 : IDisposable
{
    nint _value;

    public NativeInt128() : this(0)
    {
    }

    public NativeInt128(long value)
    {
        _value = (nint)NativeMemory.AlignedAlloc(16, 16);
        *(Int128*)_value = value;
    }

    public nint ValuePtr
    {
        get { return _value; }
    }

    public Int128 Value
    {
        get
        {
            return *(Int128*)_value;
        }
        set
        {
            *(Int128*)_value = value;
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    ~NativeInt128()
    {
        Dispose(false);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_value == 0)
        {
            return;
        }

        NativeMemory.Free(_value.ToPointer());
        _value = 0;
    }
}

그런 다음, 이렇게 써야 그나마 안전하게 C#에서 Int128에 대한 InterlockedCompareExchange128 코드를 사용할 수 있습니다.

NativeInt128 data = new NativeInt128();
Int128 comparand = 0;
InterlockedExtension._InterlockedCompareExchange128((long*)data.ValuePtr, 0, 1, (long*)&comparand);

자, 여기까지 모두 준비되었으면 이제 InterlockedCompareExchange128을 이용해 Interlocked Increment 기능도 구현할 수 있습니다.

public void InterlockedIncrement()
{
    Int128 comparand = *(Int128*)_value;
    long* ptrLow;
    long* ptrHigh;

    Int128 newValue;
    ptrLow = (long*)&newValue;
    ptrHigh = ptrLow + 1;

    do
    {
        newValue = comparand + 1;
    } while (InterlockedExtension._InterlockedCompareExchange128((long*)_value,
                    *ptrHigh, *ptrLow, (long*)&comparand) == 0);
}

결국 이렇게 해서 구현하긴 했지만, 사실 이 과정을 그냥 하나의 응용 사례라고만 보시고 실제 코드는 단순히 lock을 쓰는 것이 훨씬 더 효율적입니다. ^^

object _lockValue = new object();

public void InterlockedIncrement()
{
    lock (_lockValue)
    {
        this.Value ++;
    }
}

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




참고로, System.Text.Json에서의 Int128 직렬화는 .NET 8부터 추가되었다고 합니다.

Built-in support for Half, Int128 and UInt128 numeric types
; https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-7/#built-in-support-for-half-int128-and-uint128-numeric-types




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 1/25/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2023-12-31 02시08분
NativeMemory.AlignedAlloc은 내부적으로 (윈도우의 경우) aligned_malloc을 호출합니다.

_aligned_malloc
; https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/aligned-malloc

참고로 Virtual 메모리 수준에서 메모리 정렬을 요구하는 함수도 있습니다.

How to allocate address space with a custom alignment or in a custom address region
; https://devblogs.microsoft.com/oldnewthing/20231229-00/?p=109204

VirtualAlloc2 function (memoryapi.h)
; https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc2
정성태

... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12856정성태11/23/20218907.NET Framework: 1121. C# - 동일한 IP:Port로 바인딩 가능한 서버 소켓 [2]
12855정성태11/13/20216248개발 환경 구성: 605. Azure App Service - Kudu SSH 환경에서 FTP를 이용한 파일 전송
12854정성태11/13/20217808개발 환경 구성: 604. Azure - 윈도우 VM에서 FTP 여는 방법
12853정성태11/10/20216168오류 유형: 766. Azure App Service - JBoss 호스팅 생성 시 "This region has quota of 0 PremiumV3 instances for your subscription. Try selecting different region or SKU."
12851정성태11/1/20217562스크립트: 34. 파이썬 - MySQLdb 기본 예제 코드
12850정성태10/27/20218735오류 유형: 765. 우분투에서 pip install mysqlclient 실행 시 "OSError: mysql_config not found" 오류
12849정성태10/17/20217870스크립트: 33. JavaScript와 C#의 시간 변환 [1]
12848정성태10/17/20218823스크립트: 32. 파이썬 - sqlite3 기본 예제 코드 [1]
12847정성태10/14/20218664스크립트: 31. 파이썬 gunicorn - WORKER TIMEOUT 오류 발생
12846정성태10/7/20218422스크립트: 30. 파이썬 __debug__ 플래그 변수에 따른 코드 실행 제어
12845정성태10/6/20218247.NET Framework: 1120. C# - BufferBlock<T> 사용 예제 [5]파일 다운로드1
12844정성태10/3/20216251오류 유형: 764. MSI 설치 시 "... is accessible and not read-only." 오류 메시지
12843정성태10/3/20216698스크립트: 29. 파이썬 - fork 시 기존 클라이언트 소켓 및 스레드의 동작파일 다운로드1
12842정성태10/1/202125053오류 유형: 763. 파이썬 오류 - AttributeError: type object '...' has no attribute '...'
12841정성태10/1/20218522스크립트: 28. 모든 파이썬 프로세스에 올라오는 특별한 파일 - sitecustomize.py
12840정성태9/30/20218621.NET Framework: 1119. Entity Framework의 Join 사용 시 다중 칼럼에 대한 OR 조건 쿼리파일 다운로드1
12839정성태9/15/20219672.NET Framework: 1118. C# 11 - 제네릭 타입의 특성 적용파일 다운로드1
12838정성태9/13/20219327.NET Framework: 1117. C# - Task에 전달한 Action, Func 유형에 따라 달라지는 async/await 비동기 처리 [2]파일 다운로드1
12837정성태9/11/20218231VC++: 151. Golang - fmt.Errorf, errors.Is, errors.As 설명
12836정성태9/10/20217848Linux: 45. 리눅스 - 실행 중인 다른 프로그램의 출력을 확인하는 방법
12835정성태9/7/20219078.NET Framework: 1116. C# 10 - (15) CallerArgumentExpression 특성 추가 [2]파일 다운로드1
12834정성태9/7/20217479오류 유형: 762. Visual Studio 2019 Build Tools - 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
12833정성태9/6/20216926VC++: 150. Golang - TCP client/server echo 예제 코드파일 다운로드1
12832정성태9/6/20217786VC++: 149. Golang - 인터페이스 포인터가 의미 있을까요?
12831정성태9/6/20216327VC++: 148. Golang - 채널에 따른 다중 작업 처리파일 다운로드1
12830정성태9/6/20218591오류 유형: 761. Internet Explorer에서 파일 다운로드 시 "Your current security settings do not allow this file to be downloaded." 오류
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...