-
-
Notifications
You must be signed in to change notification settings - Fork 703
Implement some of MSVC's intrinsics, for ImportC. #16372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
just-harry
wants to merge
89
commits into
dlang:master
Choose a base branch
from
just-harry:msvc-intrinsics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+14,839
−2
Open
Changes from all commits
Commits
Show all changes
89 commits
Select commit
Hold shift + click to select a range
00062e9
Add files for MSVC intrinsic implementations.
just-harry 320e23b
Scaffold some versions for the MSVC intrinsic implementations.
just-harry e394210
Implement the MSVC intrinsics for extended-width multiplication.
just-harry a6ae4f9
Implement the MSVC intrinsics for extended-width division.
just-harry 612d6da
Implement the MSVC intrinsics for the x86 `cpuid` instruction.
just-harry 920d4aa
Define `gccBuiltins` privately for the MSVC intrinsic implementations.
just-harry 1a06f80
Implement the MSVC intrinsics for fast float conversion.
just-harry 5bc4e69
Implement the MSVC intrinsics for fast double conversion.
just-harry 6269dd6
Implement the MSVC intrinsics for saturating float conversion.
just-harry d1e582e
Implement the MSVC intrinsics for saturating double conversion.
just-harry 338ee6d
Implement the MSVC intrinsics for sentinel-on-invalid float conversion.
just-harry fd2313c
Implement the MSVC intrinsics for sentinel-on-invalid double conversion.
just-harry f855148
Implement the `__halt` MSVC intrinsic.
just-harry 44a0ed4
Implement the `<op><g/f>s<size>` MSVC intrinsics.
just-harry 27a4f3a
Implement the `<op>x18<size>` MSVC intrinsics.
just-harry c13655f
Implement the `__debugbreak` MSVC intrinsic.
just-harry a79ec4f
Implement the `__fastfail` MSVC intrinsic.
just-harry fa377bf
Implement the `__faststorefence` MSVC intrinsic.
just-harry 9c3d39b
Implement the `_disable` MSVC intrinsic.
just-harry 41d2d55
Implement the `_enable` MSVC intrinsic.
just-harry d37afcb
Implement the `_mm_pause` MSVC intrinsic.
just-harry fa2c1e9
Implement the MSVC intrinsics for ARM memory barriers.
just-harry 7d2fc41
Implement the MSVC intrinsics for interlocked additions.
just-harry f5127cb
Implement the MSVC intrinsics for interlocked and-ing.
just-harry 440000a
Implement the MSVC intrinsics for interlocked bit-test-and-resets.
just-harry f4cbb2b
Implement the MSVC intrinsics for interlocked bit-test-and-sets.
just-harry 0337d1c
Implement the MSVC intrinsics for interlocked compare-and-exchanges.
just-harry e289597
Implement the MSVC intrinsics for 128-bit interlocked compare-and-exc…
just-harry 5f18233
Implement the MSVC intrinsics for interlocked pointer compare-and-exc…
just-harry 303a340
Implement the MSVC intrinsics for interlocked decrements.
just-harry bd19846
Implement the MSVC intrinsics for interlocked exchanges.
just-harry c765e2e
Implement the MSVC intrinsics for interlocked exchange-additions.
just-harry 940fec2
Implement the MSVC intrinsics for interlocked pointer exchanges.
just-harry 336daa5
Implement the MSVC intrinsics for interlocked increments.
just-harry dfd9e28
Implement the MSVC intrinsics for interlocked or-ing.
just-harry 2ab6e6c
Implement the MSVC intrinsics for interlocked xor-ing.
just-harry 78efe07
Implement the MSVC intrinsics for x86 I/O port handling.
just-harry 4ba8674
Implement the `__int2c` MSVC intrinsic.
just-harry 934946c
Implement the `__invlpg` MSVC intrinsic.
just-harry 3d6f6e2
Implement the `__lidt` MSVC intrinsic.
just-harry 6fcd7c3
Implement the MSVC intrinsics for 64-bit bit-shifts.
just-harry 4d9532d
Implement the MSVC intrinsics for `lzcnt`.
just-harry adb47d5
Implement the MSVC intrinsics for SSE float<->integer conversion.
just-harry 3930ee2
Implement the MSVC intrinsics for `tzcnt`.
just-harry 614f14a
Implement the MSVC intrinsics for some SSE4a instructions.
just-harry d70b542
Implement the `_mm_stream_si64x` MSVC intrinsic.
just-harry db0360b
Implement the MSVC intrinsics for the x86 `rep movs` instruction.
just-harry 70e86da
Implement the `__noop` MSVC intrinsic.
just-harry db5bfdd
Implement the `__nop` MSVC intrinsic.
just-harry 3e63f7a
Implement the MSVC intrinsics for `popcnt`.
just-harry 116b559
Implement the MSVC intrinsics for the x86 `rdtsc` instructions.
just-harry d9aa2eb
Implement the `__readcr<number>` MSVC intrinsics.
just-harry 0ef9334
Implement the `__readdr` MSVC intrinsic.
just-harry c846346
Implement the `__readeflags` MSVC intrinsic.
just-harry 341d3df
Implement the `__readmsr` MSVC intrinsic.
just-harry 98acd14
Implement the `__readpmc` MSVC intrinsic.
just-harry 348842f
Implement the `__segmentlimit` MSVC intrinsic.
just-harry 38ea329
Implement the MSVC intrinsics for 128-bit bit-shifts.
just-harry 960ee02
Implement the `__sidt` MSVC intrinsic.
just-harry 8299d5f
Implement the MSVC intrinsics for the x86 `rep stos` instruction.
just-harry c921469
Implement the MSVC intrinsics for the AMD x86 SVM instruction set.
just-harry f4f3ed3
Implement the `__ud2` MSVC intrinsic.
just-harry dc62a56
Implement the MSVC intrinsics for the Intel x86 VMX instruction set.
just-harry 31c6c7d
Implement the `__wbinvd` MSVC intrinsic.
just-harry 348f5b4
Implement the `__writecr<number>` MSVC intrinsics.
just-harry 2ebd70f
Implement the `__writedr` MSVC intrinsic.
just-harry 32d8b9b
Implement the `__writeeflags` MSVC intrinsic.
just-harry ceeb101
Implement the `__writemsr` MSVC intrinsic.
just-harry f412f62
Implement the MSVC intrinsics for non-atomic read/write barriers.
just-harry 9a924f1
Implement the MSVC intrinsics for bit-scanning.
just-harry 6ece64b
Implement the MSVC intrinsics for bit-test operations.
just-harry fd8660b
Implement the MSVC intrinsics for byte-swapping.
just-harry 52d817f
Implement the MSVC intrinsics for bit-rotation.
just-harry 6fed743
Implement the `__yield` MSVC intrinsic.
just-harry 04bfe0c
Implement the MSVC intrinsics for prefetching.
just-harry 813f571
Implement the `_mm_clflush` MSVC intrinsic.
just-harry 817e22b
Implement the MSVC intrinsics for loading/storing the x86 MXCSR regis…
just-harry e1ffcfb
Implement the MSVC intrinsics for AArch64 acquire/release atomic load…
just-harry 9aebeaf
Implement the MSVC intrinsics for x86 memory fences.
just-harry 979553a
Implement the MSVC intrinsics for volatile stores and loads.
just-harry 3799caa
Define `__IMPORTC_DMD__` in `importc.h` to make it possible to detect…
just-harry 828c65c
Implement the `__assume` MSVC intrinsic.
just-harry 2dfa644
Apply `#pragma function_decl(ignore)` to the MSVC intrinsics implemen…
just-harry d473a1d
Pass the resolved path of `<path>/importc.h/../include` to the C prep…
just-harry 1234559
Add a test for checking that MSVC intrinsics are actually available f…
just-harry 2b2796d
Document the alternative licence of 0BSD for the MSVC intrinsics impl…
just-harry cf2cd6e
Document the ImportC `include` directory in ImportC's specification.
just-harry f4e0954
Document the `__builtins_msvc` module in ImportC's specification.
just-harry a5c4b3b
Add a changelog entry for ImportC supporting some of MSVC's intrinsics.
just-harry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| ImportC implements the vast majority of MSVC's intrinsics: 461 of them to be exact. | ||
|
|
||
| ImportC now implements the vast majority of MSVC's intrinsics: 461 of them to be exact. | ||
| All but three of the intrinsics [listed here](https://web.archive.org/web/20240412171516/https://learn.microsoft.com/en-ie/cpp/intrinsics/alphabetical-listing-of-intrinsic-functions?view=msvc-170) are implemented, alongside a handful of undocumented intrinsics, accompanied by a smattering of ISA-specific intrinsics. | ||
|
|
||
| The presence of these intrinsics greatly improves ImportC's ability to successfully import C headers when targeting Windows. | ||
| Notably, `Windows.h` can now be successfully included by ImportC. | ||
|
|
||
| To make use of MSVC intrinsics in ImportC, simply `#include` the `importc_msvc_builtins.h` header before any C code that uses MSVC intrinsics: | ||
| ```c | ||
| #include <importc_msvc_builtins.h> | ||
| #include <windows.h> | ||
| ``` | ||
| MSVC intrinsics are available when targeting the Microsoft C runtime (that is, when the `CRuntime_Microsoft` version identifier is defined). | ||
|
|
||
| Every intrinsic implemented has received optimised code for LDC, GDC, and DMD. | ||
| Each intrinsic, where possible, has a CTFE-compatible code path. | ||
|
|
||
| (Owing to the newness of DMD's AArch64 backend, MSVC intrinsics have not yet been implemented for DMD AArch64 targets. They have been implemented for LDC and GDC AArch64 targets.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // DISABLED: osx linux freebsd openbsd netbsd dragonflybsd hurd | ||
| // LINK(windows): | ||
| // PERMUTE_ARGS: -betterC | ||
| // Checking that the MSVC intrinsics reimplemented for ImportC are actually available from C. | ||
|
|
||
| #include <importc_msvc_builtins.h> | ||
|
|
||
| #ifndef __IMPORTC_MSVC_BUILTINS__ | ||
| #error importc_msvc_builtins.h should define __IMPORTC_MSVC_BUILTINS__. | ||
| #endif | ||
|
|
||
| // It should be safe to include importc_msvc_builtins.h multiple times. | ||
| #include <importc_msvc_builtins.h> | ||
|
|
||
| // Are the MSVC intrinsics actually usable from C? | ||
| #if defined(_M_AMD64) | ||
| unsigned long long multiplyU128(unsigned long long a, unsigned long long b, unsigned long long* high) | ||
| { | ||
| return _umul128(a, b, high); | ||
| } | ||
| #elif defined(_M_IX86) | ||
| int interlockedAddLarge(long long *target, int value) | ||
| { | ||
| return _InterlockedAddLargeStatistic(target, value); | ||
| } | ||
| #elif defined(_M_ARM64) | ||
| unsigned long long multiplyUHigh64(unsigned long long a, unsigned long long b) | ||
| { | ||
| return __umulh(a, b); | ||
| } | ||
| #elif defined(_M_ARM) | ||
| void dmb(void) | ||
| { | ||
| __dmb(11); | ||
| } | ||
| #endif | ||
|
|
||
| int main(void) | ||
| { | ||
| __assume(1); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change will need to be made in LDC also.
I assume the same applies to GDC, although as I understand it GDC doesn't support targeting Windows, so whether GDC implements this change or not is immaterial at present.
To clarify, the reason for this change is to make
importc_msvc_builtins.havailable to the C preprocessor.In future, other ImportC/DRuntime-provided headers can be placed in this directory, so this is a one-time change.
Pre-emptive LDC PR: ldc-developers/ldc#5169