As for ODR violations, this is a must-read: 
https://randomascii.wordpress.com/2016/12/05/vc-archavx-option-unsafe-at-any-speed/
 :-)

BR,
Luca
________________________________
Da: Luca Bacci <[email protected]>
Inviato: mercoledì 10 giugno 2026 17:33
A: LIU Hao <[email protected]>; [email protected] 
<[email protected]>
Oggetto: R: [Mingw-w64-public] Question: library version of interlocked 
functions

Thanks for the insights!

> There's possibility that some of them might not be inlined

This brings us another issue however: functions defined in headers should be 
'static always_inline'. Why? If the compiler decides to emit function calls 
(say because code takes the address of the functions) we can introduce ODR 
violations. This happens when object files (or static libraries) are compiled 
with different compiler arguments, or if object files (or static libraries) are 
compiled with a previous version of mingw-w64 headers.

Now, it occurs to me that there may be another reason for library functions. 
Code targeting MSVC can use intrinsics even without including intrin.h:

source.c:
------------
long _InterlockedExchange(long volatile* _Target, long _Value);
#pragma intrinsic(_InterlockedExchange)

int main(void)
{
    long value = 0;
    return _InterlockedExchange(&value, 1);
}

This also works on GCC, probably thanks to library implementations in crt.o.

Best Regards!
Luca
________________________________
Da: LIU Hao
Inviato: Mercoledì, 10 Giugno, 2026 16:11
A: [email protected]; Luca Bacci
Oggetto: Re: [Mingw-w64-public] Question: library version of interlocked 
functions

在 2026-6-10 21:20, Luca Bacci 写道:
> Hello,
>
> I want to implement weak interlocked functions (relaxed, acquire, release 
> variants). As explained in 
> https://github.com/mingw-w64/mingw-w64/blob/00349888/mingw-w64-headers/include/psdk_inc/intrin-impl.h#L12<https://github.com/mingw-w64/mingw-w64/blob/00349888007fef589c2604bf60d0ea14c0e2d606/mingw-w64-headers/include/psdk_inc/intrin-impl.h#L12>,
>  interlocked functions come in three forms:
>
>
>    1.
> As MSVC intrinsics replacements (for example, _InterlockedExchange from 
> intrin.h)
>    2.
> As Windows.h inline functions (InterlockedExchange from winnt.h)
>    3.
> As library functions
>
> I don't understand the need for 3). Can anybody explain the need for library 
> implementations?
There's possibility that some of them might not be inlined.

Maybe there's no such possibility; for example, many SSE intrinsics in GCC 
headers are `gnu_inline` and
`always_inline`, but are not defined elsewhere.


--
Best regards,
LIU Hao

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to