c++: auto in trailing-return-type in parameter [PR117778]
[gcc.git] / libphobos / libdruntime / core / builtins.d
blob4e0c6d4d622ed90edf4f058af533e4759757ba15
1 /**********************************************
2 To provide access to features that would be otherwise counterproductive or
3 difficult to implement, compilers provide an interface consisting of a set
4 of builtins (also called intrinsics) which can be called like normal functions.
6 This module exposes builtins both common to all D compilers
7 (those provided by the frontend) and specific to the host compiler i.e. those
8 specific to either LLVM or GCC (`ldc.intrinsics` and `gcc.builtins` are publicly imported, respectively).
9 Host-specific intrinsics cannot be reliably listed here, however listings can be found
10 at the documentation for the relevant backends, i.e.
11 $(LINK2 https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html, GCC) and
12 $(LINK2 https://llvm.org/docs/LangRef.html, LLVM). It should be noted that not all
13 builtins listed are necessarily supported by the host compiler, please file a bug
14 if this is the case for your workload.
16 Use of this module reduces the amount of conditional compilation needed
17 to use a given builtin. For example, to write a target independent function
18 that uses prefetching we can write the following:
19 ---
20 float usePrefetch(float[] x)
22 // There is only one import statement required rather than two (versioned) imports
23 import core.builtins;
24 version (GNU)
25 __builtin_prefetch(x.ptr);
26 version (LDC)
28 For the curious: 0, 3, 1 mean `x` will only be read-from (0), it will be used
29 very often (3), and it should be fetched to the data-cache (1).
31 llvm_prefetch(x.ptr, 0, 3, 1);
32 const doMath = blahBlahBlah;
33 return doMath;
35 ---
38 Copyright: Copyright © 2021, The D Language Foundation
39 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
40 Authors: Walter Bright
41 Source: $(DRUNTIMESRC core/builtins.d)
44 module core.builtins;
46 version (GNU)
47 public import gcc.builtins;
49 version (LDC)
50 public import ldc.intrinsics;
52 /// Writes `s` to `stderr` during CTFE (does nothing at runtime).
53 void __ctfeWrite(scope const(char)[] s) @nogc @safe pure nothrow {}
55 version (GNU)
57 /// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fexpect
58 alias expect = __builtin_expect;
59 /// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005ftrap
60 alias trap = __builtin_trap;
62 else version (LDC)
64 /// https://llvm.org/docs/LangRef.html#llvm-expect-intrinsic
65 alias expect = llvm_expect;
66 debug
67 /// https://llvm.org/docs/LangRef.html#llvm-debugtrap-intrinsic
68 alias trap = llvm_debugtrap;
69 else
70 /// https://llvm.org/docs/LangRef.html#llvm-trap-intrinsic
71 alias trap = llvm_trap;
73 else version (DigitalMars)
75 pragma(inline, true)
76 T expect(T)(T val, T expected) if (__traits(isIntegral, T))
78 return val;
81 /// Execute target dependent trap instruction, if supported.
82 /// Otherwise, abort execution.
83 pragma(inline, true)
84 void trap()
86 debug
88 version(D_InlineAsm_X86)
89 asm nothrow @nogc pure @trusted { int 3; }
91 assert(0);
95 /// Provide static branch and value hints for the LDC/GDC compilers.
96 /// DMD ignores these hints.
97 pragma(inline, true) bool likely()(bool b) { return !!expect(b, true); }
98 /// ditto
99 pragma(inline, true) bool unlikely()(bool b) { return !!expect(b, false); }
102 @nogc nothrow pure @safe unittest
104 int x = 12;
106 expect(x, 12);
108 if (likely(x > 0))
110 // ...
112 else if (unlikely(x == int.min))
114 // ...