Debian Codegraph @

libphobos/libdruntime/core/stdc/config.d

raw
1 /***
2 * D compatible types that correspond to various basic types in associated
3 * C and C++ compilers.
4 *
5 * Copyright: Copyright Sean Kelly 2005 - 2009.
6 * License: Distributed under the
7 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
8 * (See accompanying file LICENSE)
9 * Authors: Sean Kelly
10 * Source: $(DRUNTIMESRC core/stdc/_config.d)
11 * Standards: ISO/IEC 9899:1999 (E)
12 */
13
14 module core.stdc.config;
15
16 version (StdDdoc)
17 {
18 private
19 {
20 version (Posix)
21 enum isPosix = true;
22 else
23 enum isPosix = false;
24 static if (isPosix && (void*).sizeof > int.sizeof)
25 {
26 alias ddoc_long = long;
27 alias ddoc_ulong = ulong;
28 }
29 else
30 {
31 alias ddoc_long = int;
32 alias ddoc_ulong = uint;
33 }
34 struct ddoc_complex(T) { T re; T im; }
35 }
36
37 /***
38 * Used for a signed integer type that corresponds in size to the associated
39 * C compiler's `long` type.
40 */
41 alias c_long = ddoc_long;
42
43 /***
44 * Used for an unsigned integer type that corresponds in size to the associated
45 * C compiler's `unsigned long` type.
46 */
47 alias c_ulong = ddoc_ulong;
48
49 /***
50 * Used for a signed integer type that corresponds in size and mangling to the associated
51 * C++ compiler's `long` type.
52 */
53 alias cpp_long = c_long;
54
55 /***
56 * Used for an unsigned integer type that corresponds in size and mangling to the associated
57 * C++ compiler's `unsigned long` type.
58 */
59 alias cpp_ulong = c_ulong;
60
61 /***
62 * Used for a signed integer type that corresponds in size and mangling to the associated
63 * C++ compiler's `long long` type.
64 */
65 alias cpp_longlong = long;
66
67 /***
68 * Used for an unsigned integer type that corresponds in size and mangling to the associated
69 * C++ compiler's `unsigned long long` type.
70 */
71 alias cpp_ulonglong = ulong;
72
73 /***
74 * Used for a floating point type that corresponds in size and mangling to the associated
75 * C++ compiler's `long double` type.
76 */
77 alias c_long_double = real;
78
79 /***
80 * Used for an unsigned integer type that corresponds in size and mangling to the associated
81 * C++ compiler's `size_t` type.
82 */
83 alias cpp_size_t = size_t;
84
85 /***
86 * Used for a signed integer type that corresponds in size and mangling to the associated
87 * C++ compiler's `ptrdiff_t` type.
88 */
89 alias cpp_ptrdiff_t = ptrdiff_t;
90
91 /***
92 * Used for a complex floating point type that corresponds in size and ABI to the associated
93 * C compiler's `_Complex float` type.
94 */
95 alias c_complex_float = ddoc_complex!float;
96
97 /***
98 * Used for a complex floating point type that corresponds in size and ABI to the associated
99 * C compiler's `_Complex double` type.
100 */
101 alias c_complex_double = ddoc_complex!double;
102
103 /***
104 * Used for a complex floating point type that corresponds in size and ABI to the associated
105 * C compiler's `_Complex long double` type.
106 */
107 alias c_complex_real = ddoc_complex!real;
108 }
109 else
110 {
111
112 version (OSX)
113 version = Darwin;
114 else version (iOS)
115 version = Darwin;
116 else version (TVOS)
117 version = Darwin;
118 else version (WatchOS)
119 version = Darwin;
120
121 version (GNU)
122 {
123 import gcc.builtins;
124
125 alias __builtin_clong c_long;
126 alias __builtin_culong c_ulong;
127
128 enum __c_long : __builtin_clong;
129 enum __c_ulong : __builtin_culong;
130
131 alias __c_long cpp_long;
132 alias __c_ulong cpp_ulong;
133
134 enum __c_longlong : __builtin_clonglong;
135 enum __c_ulonglong : __builtin_culonglong;
136
137 alias __c_longlong cpp_longlong;
138 alias __c_ulonglong cpp_ulonglong;
139 }
140 else version (Windows)
141 {
142 enum __c_long : int;
143 enum __c_ulong : uint;
144
145 alias int c_long;
146 alias uint c_ulong;
147
148 alias __c_long cpp_long;
149 alias __c_ulong cpp_ulong;
150
151 alias long cpp_longlong;
152 alias ulong cpp_ulonglong;
153 }
154 else version (Posix)
155 {
156 static if ( (void*).sizeof > int.sizeof )
157 {
158 enum __c_longlong : long;
159 enum __c_ulonglong : ulong;
160
161 alias long c_long;
162 alias ulong c_ulong;
163
164 alias long cpp_long;
165 alias ulong cpp_ulong;
166
167 alias __c_longlong cpp_longlong;
168 alias __c_ulonglong cpp_ulonglong;
169 }
170 else
171 {
172 enum __c_long : int;
173 enum __c_ulong : uint;
174
175 alias int c_long;
176 alias uint c_ulong;
177
178 alias __c_long cpp_long;
179 alias __c_ulong cpp_ulong;
180
181 alias long cpp_longlong;
182 alias ulong cpp_ulonglong;
183 }
184 }
185 else version (WASI)
186 {
187 static if ( (void*).sizeof > int.sizeof )
188 {
189 enum __c_longlong : long;
190 enum __c_ulonglong : ulong;
191
192 alias long c_long;
193 alias ulong c_ulong;
194
195 alias long cpp_long;
196 alias ulong cpp_ulong;
197
198 alias __c_longlong cpp_longlong;
199 alias __c_ulonglong cpp_ulonglong;
200 }
201 else
202 {
203 enum __c_long : int;
204 enum __c_ulong : uint;
205
206 alias int c_long;
207 alias uint c_ulong;
208
209 alias __c_long cpp_long;
210 alias __c_ulong cpp_ulong;
211
212 alias long cpp_longlong;
213 alias ulong cpp_ulonglong;
214 }
215 }
216
217 version (GNU)
218 alias c_long_double = real;
219 else version (LDC)
220 alias c_long_double = real; // 64-bit real for MSVC targets
221 else version (SDC)
222 {
223 version (X86)
224 alias c_long_double = real;
225 else version (X86_64)
226 alias c_long_double = real;
227 }
228 else version (CRuntime_Microsoft)
229 {
230 /* long double is 64 bits, not 80 bits, but is mangled differently
231 * than double. To distinguish double from long double, create a wrapper to represent
232 * long double, then recognize that wrapper specially in the compiler
233 * to generate the correct name mangling and correct function call/return
234 * ABI conformance.
235 */
236 enum __c_long_double : double;
237
238 alias __c_long_double c_long_double;
239 }
240 else version (DigitalMars)
241 {
242 version (X86)
243 {
244 alias real c_long_double;
245 }
246 else version (X86_64)
247 {
248 version (linux)
249 alias real c_long_double;
250 else version (FreeBSD)
251 alias real c_long_double;
252 else version (OpenBSD)
253 alias real c_long_double;
254 else version (NetBSD)
255 alias real c_long_double;
256 else version (DragonFlyBSD)
257 alias real c_long_double;
258 else version (Solaris)
259 alias real c_long_double;
260 else version (Darwin)
261 alias real c_long_double;
262 }
263 }
264
265 static assert(is(c_long_double), "c_long_double needs to be declared for this platform/architecture.");
266
267 version (Darwin)
268 {
269 alias cpp_size_t = cpp_ulong;
270 version (D_LP64)
271 alias cpp_ptrdiff_t = cpp_long;
272 else
273 alias cpp_ptrdiff_t = ptrdiff_t;
274 }
275 else
276 {
277 alias cpp_size_t = size_t;
278 alias cpp_ptrdiff_t = ptrdiff_t;
279 }
280
281 /** ABI layout of native complex types.
282 */
283 struct _Complex(T)
284 if (is(T == float) || is(T == double) || is(T == c_long_double))
285 {
286 T re = 0;
287 T im = 0;
288
289 // Construction
290 /+ https://issues.dlang.org/show_bug.cgi?id=23788 dmd codegen problem with constructors and _Complex!float
291 this(_Complex!float c) { re = c.re; im = c.im; }
292 this(_Complex!double c) { re = c.re; im = c.im; }
293 this(_Complex!c_long_double c) { re = c.re; im = c.im; }
294
295 this(T re, T im) { this.re = re; this.im = im; }
296
297 this(T re) { this.re = re; this.im = 0; }
298 +/
299 // Cast
300 R opCast(R)()
301 if (is(R == _Complex!float) || is(R == _Complex!double) || is(R == _Complex!c_long_double))
302 {
303 return R(this.re, this.im);
304 }
305
306 // Assignment
307
308 ref _Complex opAssign(_Complex!float c) { re = c.re; im = c.im; return this; }
309 ref _Complex opAssign(_Complex!double c) { re = c.re; im = c.im; return this; }
310 ref _Complex opAssign(_Complex!c_long_double c) { re = c.re; im = c.im; return this; }
311
312 ref _Complex opAssign(T t) { re = t; im = 0; return this; }
313
314 // Equals
315
316 bool opEquals(_Complex!float c) { return re == c.re && im == c.im; }
317 bool opEquals(_Complex!double c) { return re == c.re && im == c.im; }
318 bool opEquals(_Complex!c_long_double c) { return re == c.re && im == c.im; }
319
320 bool opEquals(T t) { return re == t && im == 0; }
321
322 // Unary operators
323
324 // +complex
325 _Complex opUnary(string op)()
326 if (op == "+")
327 {
328 return this;
329 }
330
331 // -complex
332 _Complex opUnary(string op)()
333 if (op == "-")
334 {
335 return _Complex(-re, -im);
336 }
337
338 // BINARY OPERATORS
339
340 // complex op complex
341 _Complex!(CommonType!(T,R)) opBinary(string op, R)(_Complex!R z)
342 {
343 alias C = typeof(return);
344 auto w = C(this.re, this.im);
345 return w.opOpAssign!(op)(z);
346 }
347
348 // complex op numeric
349 _Complex!(CommonType!(T,R)) opBinary(string op, R)(R r)
350 if (is(R : c_long_double))
351 {
352 alias C = typeof(return);
353 auto w = C(this.re, this.im);
354 return w.opOpAssign!(op)(r);
355 }
356
357 // numeric + complex, numeric * complex
358 _Complex!(CommonType!(T, R)) opBinaryRight(string op, R)(R r)
359 if ((op == "+" || op == "*") && is(R : c_long_double))
360 {
361 return opBinary!(op)(r);
362 }
363
364 // numeric - complex
365 _Complex!(CommonType!(T, R)) opBinaryRight(string op, R)(R r)
366 if (op == "-" && is(R : c_long_double))
367 {
368 return _Complex(r - re, -im);
369 }
370
371 // numeric / complex
372 _Complex!(CommonType!(T, R)) opBinaryRight(string op, R)(R r)
373 if (op == "/" && is(R : c_long_double))
374 {
375 import core.math : fabs;
376 typeof(return) w = void;
377 if (fabs(re) < fabs(im))
378 {
379 immutable ratio = re/im;
380 immutable rdivd = r/(re*ratio + im);
381
382 w.re = rdivd*ratio;
383 w.im = -rdivd;
384 }
385 else
386 {
387 immutable ratio = im/re;
388 immutable rdivd = r/(re + im*ratio);
389
390 w.re = rdivd;
391 w.im = -rdivd*ratio;
392 }
393
394 return w;
395 }
396
397 // OP-ASSIGN OPERATORS
398
399 // complex += complex, complex -= complex
400 ref _Complex opOpAssign(string op, C)(C z)
401 if ((op == "+" || op == "-") && is(C R == _Complex!R))
402 {
403 mixin ("re "~op~"= z.re;");
404 mixin ("im "~op~"= z.im;");
405 return this;
406 }
407
408 // complex *= complex
409 ref _Complex opOpAssign(string op, C)(C z)
410 if (op == "*" && is(C R == _Complex!R))
411 {
412 auto temp = re*z.re - im*z.im;
413 im = im*z.re + re*z.im;
414 re = temp;
415 return this;
416 }
417
418 // complex /= complex
419 ref _Complex opOpAssign(string op, C)(C z)
420 if (op == "/" && is(C R == _Complex!R))
421 {
422 import core.math : fabs;
423 if (fabs(z.re) < fabs(z.im))
424 {
425 immutable ratio = z.re/z.im;
426 immutable denom = z.re*ratio + z.im;
427
428 immutable temp = (re*ratio + im)/denom;
429 im = (im*ratio - re)/denom;
430 re = temp;
431 }
432 else
433 {
434 immutable ratio = z.im/z.re;
435 immutable denom = z.re + z.im*ratio;
436
437 immutable temp = (re + im*ratio)/denom;
438 im = (im - re*ratio)/denom;
439 re = temp;
440 }
441 return this;
442 }
443
444 // complex += numeric, complex -= numeric
445 ref _Complex opOpAssign(string op, U : T)(U a)
446 if (op == "+" || op == "-")
447 {
448 mixin ("re "~op~"= a;");
449 return this;
450 }
451
452 // complex *= numeric, complex /= numeric
453 ref _Complex opOpAssign(string op, U : T)(U a)
454 if (op == "*" || op == "/")
455 {
456 mixin ("re "~op~"= a;");
457 mixin ("im "~op~"= a;");
458 return this;
459 }
460
461 // Helper properties.
462 pragma(inline, true)
463 {
464 static @property epsilon()() { return _Complex(T.epsilon, T.epsilon); }
465 static @property infinity()() { return _Complex(T.infinity, T.infinity); }
466 static @property max()() { return _Complex(T.max, T.max); }
467 static @property min_normal()() { return _Complex(T.min_normal, T.min_normal); }
468 static @property nan()() { return _Complex(T.nan, T.nan); }
469 static @property dig()() { return T.dig; }
470 static @property mant_dig()() { return T.mant_dig; }
471 static @property max_10_exp()() { return T.max_10_exp; }
472 static @property max_exp()() { return T.max_exp; }
473 static @property min_10_exp()() { return T.min_10_exp; }
474 static @property min_exp()() { return T.min_exp; }
475 }
476 }
477
478 enum __c_complex_float : _Complex!float;
479 enum __c_complex_double : _Complex!double;
480 enum __c_complex_real : _Complex!c_long_double;
481
482 alias c_complex_float = __c_complex_float;
483 alias c_complex_double = __c_complex_double;
484 alias c_complex_real = __c_complex_real;
485
486 private template CommonType(T, R)
487 {
488 // Special kludge for Microsoft c_long_double
489 static if (is(T == c_long_double))
490 alias CommonType = T;
491 else static if (is(R == c_long_double))
492 alias CommonType = R;
493 else
494 alias CommonType = typeof(true ? T.init : R.init);
495 }
496
497 /************ unittests ****************/
498
499 version (unittest)
500 {
501 private:
502
503 alias _cfloat = _Complex!float;
504 alias _cdouble = _Complex!double;
505 alias _creal = _Complex!c_long_double;
506
507 T abs(T)(T t) => t < 0 ? -t : t;
508 }
509
510 @safe pure nothrow unittest
511 {
512 auto c1 = _cdouble(1.0, 1.0);
513
514 // Check unary operations.
515 auto c2 = _cdouble(0.5, 2.0);
516
517 assert(c2 == +c2);
518
519 assert((-c2).re == -(c2.re));
520 assert((-c2).im == -(c2.im));
521 assert(c2 == -(-c2));
522
523 // Check complex-complex operations.
524 auto cpc = c1 + c2;
525 assert(cpc.re == c1.re + c2.re);
526 assert(cpc.im == c1.im + c2.im);
527
528 auto cmc = c1 - c2;
529 assert(cmc.re == c1.re - c2.re);
530 assert(cmc.im == c1.im - c2.im);
531
532 auto ctc = c1 * c2;
533 assert(ctc == _cdouble(-1.5, 2.5));
534
535 auto cdc = c1 / c2;
536 assert(abs(cdc.re - 0.5882352941177) < 1e-12);
537 assert(abs(cdc.im - -0.3529411764706) < 1e-12);
538
539 // Check complex-real operations.
540 double a = 123.456;
541
542 auto cpr = c1 + a;
543 assert(cpr.re == c1.re + a);
544 assert(cpr.im == c1.im);
545
546 auto cmr = c1 - a;
547 assert(cmr.re == c1.re - a);
548 assert(cmr.im == c1.im);
549
550 auto ctr = c1 * a;
551 assert(ctr.re == c1.re*a);
552 assert(ctr.im == c1.im*a);
553
554 auto cdr = c1 / a;
555 assert(abs(cdr.re - 0.00810005184033) < 1e-12);
556 assert(abs(cdr.im - 0.00810005184033) < 1e-12);
557
558 auto rpc = a + c1;
559 assert(rpc == cpr);
560
561 auto rmc = a - c1;
562 assert(rmc.re == a-c1.re);
563 assert(rmc.im == -c1.im);
564
565 auto rtc = a * c1;
566 assert(rtc == ctr);
567
568 auto rdc = a / c1;
569 assert(abs(rdc.re - 61.728) < 1e-12);
570 assert(abs(rdc.im - -61.728) < 1e-12);
571
572 rdc = a / c2;
573 assert(abs(rdc.re - 14.5242352941) < 1e-10);
574 assert(abs(rdc.im - -58.0969411765) < 1e-10);
575
576 // Check operations between different complex types.
577 auto cf = _cfloat(1.0, 1.0);
578 auto cr = _creal(1.0, 1.0);
579 auto c1pcf = c1 + cf;
580 auto c1pcr = c1 + cr;
581 static assert(is(typeof(c1pcf) == _cdouble));
582 static assert(is(typeof(c1pcr) == _creal));
583 assert(c1pcf.re == c1pcr.re);
584 assert(c1pcf.im == c1pcr.im);
585
586 auto c1c = c1;
587 auto c2c = c2;
588
589 c1c /= c1;
590 assert(abs(c1c.re - 1.0) < 1e-10);
591 assert(abs(c1c.im - 0.0) < 1e-10);
592
593 c1c = c1;
594 c1c /= c2;
595 assert(abs(c1c.re - 0.5882352941177) < 1e-12);
596 assert(abs(c1c.im - -0.3529411764706) < 1e-12);
597
598 c2c /= c1;
599 assert(abs(c2c.re - 1.25) < 1e-11);
600 assert(abs(c2c.im - 0.75) < 1e-12);
601
602 c2c = c2;
603 c2c /= c2;
604 assert(abs(c2c.re - 1.0) < 1e-11);
605 assert(abs(c2c.im - 0.0) < 1e-12);
606 }
607
608 @safe pure nothrow unittest
609 {
610 // Initialization
611 _cdouble a = _cdouble(1, 0);
612 assert(a.re == 1 && a.im == 0);
613 _cdouble b = _cdouble(1.0, 0);
614 assert(b.re == 1.0 && b.im == 0);
615 // _cdouble c = _creal(1.0, 2);
616 // assert(c.re == 1.0 && c.im == 2);
617 }
618
619 @safe pure nothrow unittest
620 {
621 // Assignments and comparisons
622 _cdouble z;
623
624 z = 1;
625 assert(z == 1);
626 assert(z.re == 1.0 && z.im == 0.0);
627
628 z = 2.0;
629 assert(z == 2.0);
630 assert(z.re == 2.0 && z.im == 0.0);
631
632 z = 1.0L;
633 assert(z == 1.0L);
634 assert(z.re == 1.0 && z.im == 0.0);
635
636 auto w = _creal(1.0, 1.0);
637 z = w;
638 assert(z == w);
639 assert(z.re == 1.0 && z.im == 1.0);
640
641 auto c = _cfloat(2.0, 2.0);
642 z = c;
643 assert(z == c);
644 assert(z.re == 2.0 && z.im == 2.0);
645 }
646
647 }
648
649
650 // Returns the mangled name for the 64-bit time_t versions of
651 // functions affected by musl's transition to 64-bit time_t.
652 // https://musl.libc.org/time64.html
653 version (CRuntime_Musl)
654 {
655 version (CRuntime_Musl_Pre_Time64)
656 enum muslRedirTime64 = false;
657 else
658 {
659 // time_t was defined as a C long in older Musl versions.
660 enum muslRedirTime64 = (c_long.sizeof == 4);
661 }
662 }
663 else
664 enum muslRedirTime64 = false;
665
666 package(core) template muslRedirTime64Mangle(string name, string redirectedName)
667 {
668 static if (muslRedirTime64)
669 enum muslRedirTime64Mangle = redirectedName;
670 else
671 enum muslRedirTime64Mangle = name;
672 }
673
674 version (PPC64)
675 enum PPCUseIEEE128 = real.mant_dig == 113;
676 else
677 enum PPCUseIEEE128 = false;