1use rucc_base::float::Format;
23use rucc_session::{GnucVersion, OptLevel, Options, Std};
24use rucc_target::{Arch, Env, Os, TargetInfo};
25
26pub const BUILT_IN: &str = "<built-in>";
28
29pub const COMMAND_LINE: &str = "<command-line>";
31
32#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct Timestamp {
39 pub date: String,
41 pub time: String,
43}
44
45impl Timestamp {
46 pub fn now() -> Timestamp {
51 let seconds = match std::env::var("SOURCE_DATE_EPOCH").ok().and_then(|v| v.parse().ok()) {
52 Some(fixed) => fixed,
53 None => std::time::SystemTime::now()
54 .duration_since(std::time::UNIX_EPOCH)
55 .map_or(0, |d| d.as_secs() as i64),
56 };
57 Timestamp::from_unix(seconds)
58 }
59
60 pub fn from_unix(seconds: i64) -> Timestamp {
65 let days = seconds.div_euclid(86_400);
66 let rest = seconds.rem_euclid(86_400);
67 let (year, month, day) = civil_from_days(days);
68 const MONTHS: [&str; 12] =
69 ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
70 let name = MONTHS[(month - 1) as usize];
71 Timestamp {
72 date: format!("{name} {day:2} {year}"),
73 time: format!("{:02}:{:02}:{:02}", rest / 3600, (rest / 60) % 60, rest % 60),
74 }
75 }
76}
77
78fn civil_from_days(days: i64) -> (i64, u32, u32) {
84 let shifted = days + 719_468;
87 let era = shifted.div_euclid(146_097);
88 let day_of_era = shifted.rem_euclid(146_097);
89 let year_of_era =
90 (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
91 let year = year_of_era + era * 400;
92 let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
93 let marched = (5 * day_of_year + 2) / 153;
94 let day = (day_of_year - (153 * marched + 2) / 5 + 1) as u32;
95 let month = if marched < 10 { marched + 3 } else { marched - 9 } as u32;
96 (year + i64::from(month <= 2), month, day)
97}
98
99#[derive(Debug, Clone, PartialEq, Eq)]
101pub struct Predef {
102 pub std: Std,
104 pub gnu_extensions: bool,
107 pub gnuc: GnucVersion,
109 pub opt_level: OptLevel,
111 pub hosted: bool,
113 pub timestamp: Timestamp,
115 pub defines: Vec<String>,
117 pub undefines: Vec<String>,
119}
120
121impl Predef {
122 pub fn new() -> Predef {
124 Predef {
125 std: Std::default(),
126 gnu_extensions: true,
127 gnuc: GnucVersion::default(),
128 opt_level: OptLevel::O0,
129 hosted: true,
130 timestamp: Timestamp::now(),
131 defines: Vec::new(),
132 undefines: Vec::new(),
133 }
134 }
135}
136
137impl Predef {
138 pub fn for_options(opts: &Options) -> Predef {
144 Predef {
145 std: opts.std,
146 gnu_extensions: opts.gnu_extensions,
147 gnuc: opts.gnuc,
148 opt_level: opts.opt_level,
149 hosted: opts.hosted,
150 timestamp: Timestamp::now(),
151 defines: opts.defines.clone(),
152 undefines: opts.undefines.clone(),
153 }
154 }
155}
156
157impl Default for Predef {
158 fn default() -> Predef {
159 Predef::new()
160 }
161}
162
163struct Defs {
165 text: String,
166}
167
168impl Defs {
169 fn new() -> Defs {
170 Defs { text: String::new() }
171 }
172
173 fn set(&mut self, name: &str, value: &str) {
175 self.text.push_str("#define ");
176 self.text.push_str(name);
177 self.text.push(' ');
178 self.text.push_str(value);
179 self.text.push('\n');
180 }
181
182 fn flag(&mut self, name: &str) {
184 self.set(name, "1");
185 }
186
187 fn set_if(&mut self, when: bool, name: &str, value: &str) {
188 if when {
189 self.set(name, value);
190 }
191 }
192
193 fn flag_if(&mut self, when: bool, name: &str) {
194 if when {
195 self.flag(name);
196 }
197 }
198}
199
200pub(crate) fn built_in(target: &TargetInfo, opts: &Predef) -> String {
202 let mut d = Defs::new();
203 identity(&mut d, opts);
204 d.set("__DATE__", &format!("\"{}\"", opts.timestamp.date));
208 d.set("__TIME__", &format!("\"{}\"", opts.timestamp.time));
209 dialect(&mut d, opts);
210 optimization(&mut d, opts);
211 platform(&mut d, target, opts);
212 sizes(&mut d, target);
213 integers(&mut d, target);
214 floats(&mut d, target);
215 atomics(&mut d, target);
216 d.text
217}
218
219pub(crate) fn command_line(opts: &Predef) -> String {
225 let mut d = Defs::new();
226 for define in &opts.defines {
227 match define.split_once('=') {
228 Some((name, value)) => d.set(name, value),
229 None => d.flag(define),
232 }
233 }
234 for name in &opts.undefines {
235 d.text.push_str("#undef ");
236 d.text.push_str(name);
237 d.text.push('\n');
238 }
239 d.text
240}
241
242fn identity(d: &mut Defs, opts: &Predef) {
244 d.flag("__rucc__");
245 d.set("__rucc_version__", "\"0.1.0\"");
246 d.set("__rucc_major__", "0");
247 d.set("__rucc_minor__", "1");
248 d.set("__rucc_patchlevel__", "0");
249 d.set("__GNUC__", &opts.gnuc.major.to_string());
251 d.set("__GNUC_MINOR__", &opts.gnuc.minor.to_string());
252 d.set("__GNUC_PATCHLEVEL__", &opts.gnuc.patch.to_string());
253 d.set("__VERSION__", "\"rucc 0.1.0\"");
254 d.flag("__GNUC_STDC_INLINE__");
257}
258
259fn dialect(d: &mut Defs, opts: &Predef) {
261 d.flag("__STDC__");
262 d.set_if(opts.hosted, "__STDC_HOSTED__", "1");
263 d.set_if(!opts.hosted, "__STDC_HOSTED__", "0");
264 if let Some(version) = opts.std.stdc_version() {
265 d.set("__STDC_VERSION__", version);
266 }
267 d.flag_if(!opts.gnu_extensions, "__STRICT_ANSI__");
270 d.flag("__STDC_UTF_16__");
271 d.flag("__STDC_UTF_32__");
272 d.flag("__STDC_IEC_559__");
273 d.flag("__STDC_IEC_559_COMPLEX__");
274 d.set_if(opts.std == Std::C23, "__STDC_IEC_60559_BFP__", "202311L");
275 d.set("__STDC_ISO_10646__", "201706L");
276 d.set_if(opts.std == Std::C23, "__CHAR8_TYPE__", "unsigned char");
282 if opts.std.has_c11() {
294 d.flag("__STDC_NO_ATOMICS__");
295 d.flag("__STDC_NO_THREADS__");
296 d.flag("__STDC_NO_COMPLEX__");
297 }
298 d.set("__STDC_EMBED_NOT_FOUND__", "0");
303 d.set("__STDC_EMBED_FOUND__", "1");
304 d.set("__STDC_EMBED_EMPTY__", "2");
305}
306
307fn atomics(d: &mut Defs, target: &TargetInfo) {
319 d.set("__ATOMIC_RELAXED", "0");
320 d.set("__ATOMIC_CONSUME", "1");
321 d.set("__ATOMIC_ACQUIRE", "2");
322 d.set("__ATOMIC_RELEASE", "3");
323 d.set("__ATOMIC_ACQ_REL", "4");
324 d.set("__ATOMIC_SEQ_CST", "5");
325 let llong = if target.pointer_width == 64 { "2" } else { "1" };
328 for name in [
329 "BOOL", "CHAR", "CHAR8_T", "CHAR16_T", "CHAR32_T", "WCHAR_T", "SHORT", "INT", "LONG",
330 "POINTER",
331 ] {
332 d.set(&format!("__GCC_ATOMIC_{name}_LOCK_FREE"), "2");
333 }
334 d.set("__GCC_ATOMIC_LLONG_LOCK_FREE", llong);
338 d.set("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
339}
340
341fn optimization(d: &mut Defs, opts: &Predef) {
343 d.flag_if(opts.opt_level.runs_optimizer(), "__OPTIMIZE__");
344 d.flag_if(opts.opt_level.is_size(), "__OPTIMIZE_SIZE__");
345 d.flag_if(!opts.opt_level.runs_optimizer(), "__NO_INLINE__");
348}
349
350fn platform(d: &mut Defs, target: &TargetInfo, opts: &Predef) {
352 let triple = target.triple;
353 match triple.arch {
354 Arch::X86_64 => {
355 d.flag("__x86_64__");
356 d.flag("__x86_64");
357 d.flag("__amd64__");
358 d.flag("__amd64");
359 d.flag("__SSE__");
360 d.flag("__SSE2__");
361 d.flag("__MMX__");
362 d.flag("__SSE_MATH__");
363 d.flag("__SSE2_MATH__");
364 d.flag("__k8");
365 d.flag("__k8__");
366 }
367 Arch::Aarch64 => {
368 d.flag("__aarch64__");
369 d.flag("__AARCH64EL__");
370 d.set("__ARM_ARCH", "8");
371 d.set("__ARM_ARCH_PROFILE", "'A'");
372 d.set("__ARM_64BIT_STATE", "1");
373 d.set("__ARM_ALIGN_MAX_PWR", "28");
374 d.set("__ARM_FP", "0xe");
375 d.set("__ARM_NEON", "1");
376 d.set("__ARM_FEATURE_UNALIGNED", "1");
377 d.set("__ARM_PCS_AAPCS64", "1");
378 }
379 Arch::Riscv64 => {
380 d.flag("__riscv");
381 d.set("__riscv_xlen", "64");
382 d.set("__riscv_flen", "64");
383 d.flag("__riscv_float_abi_double");
384 d.flag("__riscv_muldiv");
385 d.flag("__riscv_atomic");
386 d.flag("__riscv_compressed");
387 d.set("__riscv_cmodel_medlow", "1");
388 }
389 }
390 match triple.os {
391 Os::Linux => {
392 d.flag("__linux__");
393 d.flag("__linux");
394 d.flag("__unix__");
395 d.flag("__unix");
396 d.flag("__gnu_linux__");
397 d.flag("__ELF__");
398 if opts.gnu_extensions {
401 d.flag("linux");
402 d.flag("unix");
403 }
404 }
405 Os::Darwin => {
406 d.flag("__APPLE__");
407 d.flag("__MACH__");
408 d.flag("__unix__");
409 d.flag("__unix");
410 d.set("__APPLE_CC__", "6000");
411 d.set("__DYNAMIC__", "1");
412 if triple.arch == Arch::Aarch64 {
413 d.flag("__arm64__");
418 d.flag("__arm64");
419 }
420 if opts.gnu_extensions {
421 d.flag("unix");
422 }
423 }
424 Os::Windows => {
425 d.flag("_WIN32");
426 d.flag("__WIN32__");
427 d.flag("_WIN64");
428 d.flag("__WIN64__");
429 d.flag("__MINGW32__");
430 }
431 Os::None => {
432 d.flag("__ELF__");
435 }
436 }
437 match triple.env {
438 Env::Musl => d.flag("__musl__"),
439 Env::Gnu | Env::None | Env::Msvc => {}
440 }
441 if target.long_width == 64 && target.pointer_width == 64 {
444 d.flag("__LP64__");
445 d.flag("_LP64");
446 }
447 d.set("__USER_LABEL_PREFIX__", if triple.os == Os::Darwin { "_" } else { "" });
454
455 if !matches!(triple.os, Os::Windows) {
458 d.set("__PIC__", "2");
459 d.set("__pic__", "2");
460 }
461}
462
463fn sizes(d: &mut Defs, target: &TargetInfo) {
465 let pointer = target.pointer_width / 8;
466 let long = target.long_width / 8;
467 let long_double = target.long_double_width / 8;
468 d.set("__CHAR_BIT__", "8");
469 d.set("__SIZEOF_SHORT__", "2");
470 d.set("__SIZEOF_INT__", "4");
471 d.set("__SIZEOF_LONG__", &long.to_string());
472 d.set("__SIZEOF_LONG_LONG__", "8");
473 d.set("__SIZEOF_INT128__", "16");
474 d.set("__SIZEOF_FLOAT__", "4");
475 d.set("__SIZEOF_DOUBLE__", "8");
476 d.set("__SIZEOF_LONG_DOUBLE__", &long_double.to_string());
477 d.set("__SIZEOF_POINTER__", &pointer.to_string());
478 d.set("__SIZEOF_SIZE_T__", &pointer.to_string());
479 d.set("__SIZEOF_PTRDIFF_T__", &pointer.to_string());
480 d.set("__SIZEOF_WCHAR_T__", &wchar(target).size.to_string());
481 d.set("__SIZEOF_WINT_T__", "4");
482 d.set("__BIGGEST_ALIGNMENT__", "16");
483 d.set("__ORDER_LITTLE_ENDIAN__", "1234");
487 d.set("__ORDER_BIG_ENDIAN__", "4321");
488 d.set("__ORDER_PDP_ENDIAN__", "3412");
489 let order =
490 if target.little_endian { "__ORDER_LITTLE_ENDIAN__" } else { "__ORDER_BIG_ENDIAN__" };
491 d.set("__BYTE_ORDER__", order);
492 d.set("__FLOAT_WORD_ORDER__", order);
493 d.flag_if(!target.char_is_signed, "__CHAR_UNSIGNED__");
494}
495
496struct Wchar {
498 spelling: &'static str,
500 size: u32,
502 max: &'static str,
504 min: &'static str,
506}
507
508fn wchar(target: &TargetInfo) -> Wchar {
518 match (target.wchar_width, target.wchar_is_signed) {
519 (16, false) => Wchar { spelling: "short unsigned int", size: 2, max: "0xffff", min: "0" },
520 (16, true) => Wchar { spelling: "short int", size: 2, max: "0x7fff", min: "(-32767 - 1)" },
521 (_, false) => Wchar { spelling: "unsigned int", size: 4, max: "0xffffffffU", min: "0U" },
522 (_, true) => {
523 Wchar { spelling: "int", size: 4, max: "0x7fffffff", min: "(-__WCHAR_MAX__ - 1)" }
524 }
525 }
526}
527
528struct Wint {
530 spelling: &'static str,
532 max: &'static str,
534 min: &'static str,
536 width: u32,
538}
539
540fn wint(target: &TargetInfo) -> Wint {
547 match target.triple.os {
548 Os::Windows => Wint { spelling: "short unsigned int", max: "0xffff", min: "0", width: 16 },
549 Os::Darwin => {
550 Wint { spelling: "int", max: "0x7fffffff", min: "(-__WINT_MAX__ - 1)", width: 32 }
551 }
552 _ => Wint { spelling: "unsigned int", max: "0xffffffffU", min: "0U", width: 32 },
553 }
554}
555
556fn integers(d: &mut Defs, target: &TargetInfo) {
558 let lp64 = target.long_width == 64;
562 let wide = if lp64 { "long int" } else { "long long int" };
563 let wide_unsigned = if lp64 { "long unsigned int" } else { "long long unsigned int" };
564 let wide_suffix = if lp64 { "L" } else { "LL" };
565 let wide_max = format!("0x7fffffffffffffff{wide_suffix}");
566 let wide_umax = format!("0xffffffffffffffffU{wide_suffix}");
567
568 d.set("__SCHAR_MAX__", "0x7f");
569 d.set("__SHRT_MAX__", "0x7fff");
570 d.set("__INT_MAX__", "0x7fffffff");
571 d.set("__LONG_MAX__", if lp64 { "0x7fffffffffffffffL" } else { "0x7fffffffL" });
572 d.set("__LONG_LONG_MAX__", "0x7fffffffffffffffLL");
573 d.set("__INTMAX_MAX__", &wide_max);
574 d.set("__UINTMAX_MAX__", &wide_umax);
575 d.set("__SIZE_MAX__", &wide_umax);
576 d.set("__PTRDIFF_MAX__", &wide_max);
577 d.set("__INTPTR_MAX__", &wide_max);
578 d.set("__UINTPTR_MAX__", &wide_umax);
579 d.set("__SIG_ATOMIC_MAX__", "0x7fffffff");
580 d.set("__SIG_ATOMIC_MIN__", "(-__SIG_ATOMIC_MAX__ - 1)");
581 d.set("__BITINT_MAXWIDTH__", "128");
588
589 let wchar = wchar(target);
590 d.set("__WCHAR_TYPE__", wchar.spelling);
591 d.set("__WCHAR_MAX__", wchar.max);
592 d.set("__WCHAR_MIN__", wchar.min);
593 let wint = wint(target);
594 d.set("__WINT_TYPE__", wint.spelling);
595 d.set("__WINT_MAX__", wint.max);
596 d.set("__WINT_MIN__", wint.min);
597 d.set("__SIZE_TYPE__", wide_unsigned);
598 d.set("__PTRDIFF_TYPE__", wide);
599 d.set("__INTMAX_TYPE__", wide);
600 d.set("__UINTMAX_TYPE__", wide_unsigned);
601 d.set("__INTPTR_TYPE__", wide);
602 d.set("__UINTPTR_TYPE__", wide_unsigned);
603 d.set("__SIG_ATOMIC_TYPE__", "int");
604 d.set("__CHAR16_TYPE__", "short unsigned int");
605 d.set("__CHAR32_TYPE__", "unsigned int");
606 d.set("__INTMAX_C(c)", &format!("c ## {wide_suffix}"));
607 d.set("__UINTMAX_C(c)", &format!("c ## U{wide_suffix}"));
608
609 exact(d, 8, "signed char", "unsigned char", "0x7f", "0xff", "");
611 exact(d, 16, "short int", "short unsigned int", "0x7fff", "0xffff", "");
612 exact(d, 32, "int", "unsigned int", "0x7fffffff", "0xffffffffU", "");
615 exact(d, 64, wide, wide_unsigned, &wide_max, &wide_umax, wide_suffix);
616
617 let fast_is_wide = target.triple.arch == Arch::X86_64 && lp64 && target.triple.env != Env::Musl;
628 let fast_middle = if fast_is_wide { wide } else { "int" };
629 d.set("__INT_FAST8_TYPE__", "signed char");
630 d.set("__UINT_FAST8_TYPE__", "unsigned char");
631 d.set("__INT_FAST8_MAX__", "0x7f");
632 d.set("__UINT_FAST8_MAX__", "0xff");
633 for width in [16, 32] {
634 let unsigned = if fast_middle == "int" { "unsigned int" } else { wide_unsigned };
635 let max = if fast_middle == "int" { "0x7fffffff" } else { wide_max.as_str() };
636 let umax = if fast_middle == "int" { "0xffffffffU" } else { wide_umax.as_str() };
637 d.set(&format!("__INT_FAST{width}_TYPE__"), fast_middle);
638 d.set(&format!("__UINT_FAST{width}_TYPE__"), unsigned);
639 d.set(&format!("__INT_FAST{width}_MAX__"), max);
640 d.set(&format!("__UINT_FAST{width}_MAX__"), umax);
641 }
642 d.set("__INT_FAST64_TYPE__", wide);
643 d.set("__UINT_FAST64_TYPE__", wide_unsigned);
644 d.set("__INT_FAST64_MAX__", &wide_max);
645 d.set("__UINT_FAST64_MAX__", &wide_umax);
646
647 widths(d, target, &wchar, &wint, if fast_is_wide { 64 } else { 32 });
648}
649
650fn widths(d: &mut Defs, target: &TargetInfo, wchar: &Wchar, wint: &Wint, fast_middle: u32) {
661 let pointer = target.pointer_width;
662 d.set("__SCHAR_WIDTH__", "8");
663 d.set("__SHRT_WIDTH__", "16");
664 d.set("__INT_WIDTH__", "32");
665 d.set("__LONG_WIDTH__", &target.long_width.to_string());
666 d.set("__LONG_LONG_WIDTH__", "64");
667 d.set("__INTMAX_WIDTH__", "64");
668 d.set("__INTPTR_WIDTH__", &pointer.to_string());
669 d.set("__PTRDIFF_WIDTH__", &pointer.to_string());
670 d.set("__SIZE_WIDTH__", &pointer.to_string());
671 d.set("__SIG_ATOMIC_WIDTH__", "32");
672 d.set("__WCHAR_WIDTH__", &(wchar.size * 8).to_string());
673 d.set("__WINT_WIDTH__", &wint.width.to_string());
674 for width in [8, 16, 32, 64] {
675 d.set(&format!("__INT_LEAST{width}_WIDTH__"), &width.to_string());
676 }
677 d.set("__INT_FAST8_WIDTH__", "8");
678 d.set("__INT_FAST16_WIDTH__", &fast_middle.to_string());
679 d.set("__INT_FAST32_WIDTH__", &fast_middle.to_string());
680 d.set("__INT_FAST64_WIDTH__", "64");
681}
682
683fn exact(
685 d: &mut Defs,
686 width: u32,
687 signed: &str,
688 unsigned: &str,
689 max: &str,
690 umax: &str,
691 width_suffix: &str,
695) {
696 d.set(&format!("__INT{width}_TYPE__"), signed);
697 d.set(&format!("__UINT{width}_TYPE__"), unsigned);
698 d.set(&format!("__INT{width}_MAX__"), max);
699 d.set(&format!("__UINT{width}_MAX__"), umax);
700 d.set(&format!("__INT_LEAST{width}_TYPE__"), signed);
701 d.set(&format!("__UINT_LEAST{width}_TYPE__"), unsigned);
702 d.set(&format!("__INT_LEAST{width}_MAX__"), max);
703 d.set(&format!("__UINT_LEAST{width}_MAX__"), umax);
704 let unsigned_after_promotion = width >= 32;
714 let u = if unsigned_after_promotion { "U" } else { "" };
715 if width_suffix.is_empty() && u.is_empty() {
716 d.set(&format!("__INT{width}_C(c)"), "c");
717 d.set(&format!("__UINT{width}_C(c)"), "c");
718 } else if width_suffix.is_empty() {
719 d.set(&format!("__INT{width}_C(c)"), "c");
720 d.set(&format!("__UINT{width}_C(c)"), &format!("c ## {u}"));
721 } else {
722 d.set(&format!("__INT{width}_C(c)"), &format!("c ## {width_suffix}"));
723 d.set(&format!("__UINT{width}_C(c)"), &format!("c ## {u}{width_suffix}"));
724 }
725}
726
727struct Characteristics {
733 mant_dig: &'static str,
734 dig: &'static str,
735 min_exp: &'static str,
736 min_10_exp: &'static str,
737 max_exp: &'static str,
738 max_10_exp: &'static str,
739 decimal_dig: &'static str,
740 max: &'static str,
741 min: &'static str,
742 epsilon: &'static str,
743 denorm_min: &'static str,
744 is_iec_60559: &'static str,
747}
748
749const HALF: Characteristics = Characteristics {
751 mant_dig: "11",
752 dig: "3",
753 min_exp: "(-13)",
754 min_10_exp: "(-4)",
755 max_exp: "16",
756 max_10_exp: "4",
757 decimal_dig: "5",
758 max: "6.55040000000000000000000000000000000e+4",
759 min: "6.10351562500000000000000000000000000e-5",
760 epsilon: "9.76562500000000000000000000000000000e-4",
761 denorm_min: "5.96046447753906250000000000000000000e-8",
762 is_iec_60559: "1",
763};
764
765const BFLOAT16: Characteristics = Characteristics {
767 mant_dig: "8",
768 dig: "2",
769 min_exp: "(-125)",
770 min_10_exp: "(-37)",
771 max_exp: "128",
772 max_10_exp: "38",
773 decimal_dig: "4",
774 max: "3.38953138925153547590470800371487867e+38",
775 min: "1.17549435082228750796873653722224568e-38",
776 epsilon: "7.81250000000000000000000000000000000e-3",
777 denorm_min: "9.18354961579912115600575419704879436e-41",
778 is_iec_60559: "0",
779};
780
781const SINGLE: Characteristics = Characteristics {
783 mant_dig: "24",
784 dig: "6",
785 min_exp: "(-125)",
786 min_10_exp: "(-37)",
787 max_exp: "128",
788 max_10_exp: "38",
789 decimal_dig: "9",
790 max: "3.40282346638528859811704183484516925e+38",
791 min: "1.17549435082228750796873653722224568e-38",
792 epsilon: "1.19209289550781250000000000000000000e-7",
793 denorm_min: "1.40129846432481707092372958328991613e-45",
794 is_iec_60559: "1",
795};
796
797const DOUBLE: Characteristics = Characteristics {
800 mant_dig: "53",
801 dig: "15",
802 min_exp: "(-1021)",
803 min_10_exp: "(-307)",
804 max_exp: "1024",
805 max_10_exp: "308",
806 decimal_dig: "17",
807 max: "1.79769313486231570814527423731704357e+308",
808 min: "2.22507385850720138309023271733240406e-308",
809 epsilon: "2.22044604925031308084726333618164062e-16",
810 denorm_min: "4.94065645841246544176568792868221372e-324",
811 is_iec_60559: "1",
812};
813
814const X87: Characteristics = Characteristics {
816 mant_dig: "64",
817 dig: "18",
818 min_exp: "(-16381)",
819 min_10_exp: "(-4931)",
820 max_exp: "16384",
821 max_10_exp: "4932",
822 decimal_dig: "21",
823 max: "1.18973149535723176502126385303097021e+4932",
824 min: "3.36210314311209350626267781732175260e-4932",
825 epsilon: "1.08420217248550443400745280086994171e-19",
826 denorm_min: "3.64519953188247460252840593361941982e-4951",
827 is_iec_60559: "1",
828};
829
830const QUAD: Characteristics = Characteristics {
833 mant_dig: "113",
834 dig: "33",
835 min_exp: "(-16381)",
836 min_10_exp: "(-4931)",
837 max_exp: "16384",
838 max_10_exp: "4932",
839 decimal_dig: "36",
840 max: "1.18973149535723176508575932662800702e+4932",
841 min: "3.36210314311209350626267781732175260e-4932",
842 epsilon: "1.92592994438723585305597794258492732e-34",
843 denorm_min: "6.47517511943802511092443895822764655e-4966",
844 is_iec_60559: "1",
845};
846
847const fn characteristics(format: Format) -> &'static Characteristics {
850 match format {
851 Format::Half => &HALF,
852 Format::BFloat16 => &BFLOAT16,
853 Format::Single => &SINGLE,
854 Format::Double => &DOUBLE,
855 Format::X87Extended => &X87,
856 Format::Quad => &QUAD,
857 }
858}
859
860fn floats(d: &mut Defs, target: &TargetInfo) {
871 d.set("__FLT_RADIX__", "2");
872 d.set("__FLT_EVAL_METHOD__", "0");
877 d.set("__FLT_EVAL_METHOD_C99__", "0");
878 d.set("__FLT_EVAL_METHOD_TS_18661_3__", "0");
879
880 family(d, "FLT", &SINGLE, |value| format!("{value}F"));
881 family(d, "DBL", &DOUBLE, |value| format!("((double){value}L)"));
884 family(d, "LDBL", characteristics(target.long_double_format), |value| format!("{value}L"));
885
886 family(d, "FLT16", &HALF, |value| format!("{value}F16"));
887 family(d, "FLT32", &SINGLE, |value| format!("{value}F32"));
888 family(d, "FLT64", &DOUBLE, |value| format!("{value}F64"));
889 family(d, "FLT128", &QUAD, |value| format!("{value}F128"));
890 family(d, "FLT32X", &DOUBLE, |value| format!("{value}F32x"));
891 family(d, "FLT64X", characteristics(target.float64x_format), |value| format!("{value}F64x"));
892
893 d.set("__DECIMAL_DIG__", characteristics(target.long_double_format).decimal_dig);
898}
899
900fn family(d: &mut Defs, prefix: &str, c: &Characteristics, write: impl Fn(&str) -> String) {
906 d.set(&format!("__{prefix}_MANT_DIG__"), c.mant_dig);
907 d.set(&format!("__{prefix}_DIG__"), c.dig);
908 d.set(&format!("__{prefix}_MIN_EXP__"), c.min_exp);
909 d.set(&format!("__{prefix}_MIN_10_EXP__"), c.min_10_exp);
910 d.set(&format!("__{prefix}_MAX_EXP__"), c.max_exp);
911 d.set(&format!("__{prefix}_MAX_10_EXP__"), c.max_10_exp);
912 d.set(&format!("__{prefix}_DECIMAL_DIG__"), c.decimal_dig);
913 d.set(&format!("__{prefix}_MAX__"), &write(c.max));
914 d.set(&format!("__{prefix}_NORM_MAX__"), &write(c.max));
915 d.set(&format!("__{prefix}_MIN__"), &write(c.min));
916 d.set(&format!("__{prefix}_EPSILON__"), &write(c.epsilon));
917 d.set(&format!("__{prefix}_DENORM_MIN__"), &write(c.denorm_min));
918 d.set(&format!("__{prefix}_IS_IEC_60559__"), c.is_iec_60559);
919 d.set(&format!("__{prefix}_HAS_DENORM__"), "1");
920 d.set(&format!("__{prefix}_HAS_INFINITY__"), "1");
921 d.set(&format!("__{prefix}_HAS_QUIET_NAN__"), "1");
922}
923
924#[cfg(test)]
925mod tests {
926 use rucc_target::Triple;
927
928 use super::*;
929
930 fn set_for(triple: &str) -> String {
931 let triple: Triple = triple.parse().expect("a triple the compiler supports");
932 built_in(&TargetInfo::new(triple), &Predef::new())
933 }
934
935 fn has(text: &str, line: &str) -> bool {
936 text.lines().any(|l| l == line)
937 }
938
939 #[test]
940 fn the_set_is_driven_by_the_target_rather_than_by_the_host() {
941 let x86 = set_for("x86_64-unknown-linux-gnu");
942 let arm = set_for("aarch64-unknown-linux-gnu");
943 assert!(has(&x86, "#define __x86_64__ 1"));
944 assert!(!has(&x86, "#define __aarch64__ 1"));
945 assert!(has(&arm, "#define __aarch64__ 1"));
946 assert!(!has(&arm, "#define __x86_64__ 1"));
947 assert!(has(&x86, "#define __linux__ 1") && has(&arm, "#define __linux__ 1"));
948 }
949
950 #[test]
951 fn windows_is_the_target_that_makes_long_thirty_two_bits() {
952 let windows = set_for("x86_64-pc-windows-msvc");
953 let linux = set_for("x86_64-unknown-linux-gnu");
954 assert!(has(&windows, "#define __SIZEOF_LONG__ 4"));
955 assert!(has(&windows, "#define __SIZE_TYPE__ long long unsigned int"));
956 assert!(has(&windows, "#define __INT64_TYPE__ long long int"));
957 assert!(!has(&windows, "#define __LP64__ 1"));
958 assert!(has(&linux, "#define __SIZEOF_LONG__ 8"));
959 assert!(has(&linux, "#define __SIZE_TYPE__ long unsigned int"));
960 assert!(has(&linux, "#define __INT64_TYPE__ long int"));
961 assert!(has(&linux, "#define __LP64__ 1"));
962 }
963
964 #[test]
965 fn wchar_t_is_the_type_that_divides_the_targets() {
966 assert!(has(&set_for("x86_64-unknown-linux-gnu"), "#define __WCHAR_TYPE__ int"));
968 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __WCHAR_TYPE__ unsigned int"));
969 let windows = set_for("x86_64-pc-windows-msvc");
970 assert!(has(&windows, "#define __WCHAR_TYPE__ short unsigned int"));
971 assert!(has(&windows, "#define __SIZEOF_WCHAR_T__ 2"));
972 }
973
974 #[test]
975 fn apple_spells_the_architecture_its_own_way_and_its_headers_only_know_that_spelling() {
976 let darwin = set_for("aarch64-apple-darwin");
979 assert!(has(&darwin, "#define __arm64__ 1"));
980 assert!(has(&darwin, "#define __arm64 1"));
981 assert!(has(&darwin, "#define __aarch64__ 1"), "the portable spelling stays too");
982 let linux = set_for("aarch64-unknown-linux-gnu");
983 assert!(!has(&linux, "#define __arm64__ 1"), "Apple's spelling is Apple's alone");
984 assert!(!has(&set_for("x86_64-apple-darwin"), "#define __arm64__ 1"));
985 }
986
987 #[test]
988 fn every_limit_is_spelled_in_hexadecimal_the_way_gcc_spells_it() {
989 let linux = set_for("x86_64-unknown-linux-gnu");
996 for line in [
997 "#define __SCHAR_MAX__ 0x7f",
998 "#define __SHRT_MAX__ 0x7fff",
999 "#define __INT_MAX__ 0x7fffffff",
1000 "#define __LONG_MAX__ 0x7fffffffffffffffL",
1001 "#define __LONG_LONG_MAX__ 0x7fffffffffffffffLL",
1002 "#define __INTMAX_MAX__ 0x7fffffffffffffffL",
1003 "#define __UINTMAX_MAX__ 0xffffffffffffffffUL",
1004 "#define __SIZE_MAX__ 0xffffffffffffffffUL",
1005 "#define __PTRDIFF_MAX__ 0x7fffffffffffffffL",
1006 "#define __SIG_ATOMIC_MAX__ 0x7fffffff",
1007 "#define __INT8_MAX__ 0x7f",
1008 "#define __UINT8_MAX__ 0xff",
1009 "#define __INT16_MAX__ 0x7fff",
1010 "#define __UINT16_MAX__ 0xffff",
1011 "#define __INT32_MAX__ 0x7fffffff",
1012 "#define __UINT32_MAX__ 0xffffffffU",
1013 "#define __INT64_MAX__ 0x7fffffffffffffffL",
1014 "#define __UINT64_MAX__ 0xffffffffffffffffUL",
1015 "#define __INT_FAST8_MAX__ 0x7f",
1016 "#define __UINT_FAST8_MAX__ 0xff",
1017 ] {
1018 assert!(has(&linux, line), "{line}");
1019 }
1020 let windows = set_for("x86_64-pc-windows-msvc");
1023 assert!(has(&windows, "#define __LONG_MAX__ 0x7fffffffL"));
1024 assert!(has(&windows, "#define __INTMAX_MAX__ 0x7fffffffffffffffLL"));
1025 assert!(has(&windows, "#define __UINTMAX_MAX__ 0xffffffffffffffffULL"));
1026 }
1027
1028 #[test]
1029 fn wint_t_does_not_follow_wchar_t() {
1030 let darwin = set_for("aarch64-apple-darwin");
1032 assert!(has(&darwin, "#define __WINT_TYPE__ int"));
1033 assert!(has(&darwin, "#define __WINT_MAX__ 0x7fffffff"));
1034 assert!(has(&darwin, "#define __WCHAR_TYPE__ int"));
1035 let linux = set_for("aarch64-unknown-linux-gnu");
1036 assert!(has(&linux, "#define __WINT_TYPE__ unsigned int"));
1037 assert!(has(&linux, "#define __WINT_MAX__ 0xffffffffU"));
1038 assert!(has(&linux, "#define __WCHAR_TYPE__ unsigned int"), "and wchar_t is its own");
1039 assert!(has(
1040 &set_for("x86_64-pc-windows-msvc"),
1041 "#define __WINT_TYPE__ short unsigned int"
1042 ));
1043 }
1044
1045 #[test]
1046 fn the_widths_say_what_the_type_holds_and_follow_the_target_that_changes_it() {
1047 let linux = set_for("x86_64-unknown-linux-gnu");
1051 assert_eq!(linux.lines().filter(|line| line.contains("_WIDTH__")).count(), 20);
1052 assert!(has(&linux, "#define __LONG_WIDTH__ 64"));
1053 assert!(has(&linux, "#define __SIZE_WIDTH__ 64"));
1054 assert!(has(&linux, "#define __WCHAR_WIDTH__ 32"));
1055 assert!(has(&linux, "#define __INT_LEAST16_WIDTH__ 16"));
1056 assert!(has(&linux, "#define __INT_FAST16_WIDTH__ 64"));
1059 assert!(has(&set_for("x86_64-unknown-linux-musl"), "#define __INT_FAST16_WIDTH__ 32"));
1060 let windows = set_for("x86_64-pc-windows-msvc");
1063 assert!(has(&windows, "#define __LONG_WIDTH__ 32"));
1064 assert!(has(&windows, "#define __WINT_WIDTH__ 16"));
1065 assert!(has(&windows, "#define __SIZE_WIDTH__ 64"));
1066 assert!(has(&windows, "#define __INTMAX_WIDTH__ 64"));
1067 }
1068
1069 #[test]
1070 fn a_constant_maker_gets_the_suffix_its_width_needs_and_no_other() {
1071 let linux = set_for("x86_64-unknown-linux-gnu");
1075 assert!(has(&linux, "#define __INT32_C(c) c"));
1076 assert!(has(&linux, "#define __UINT32_C(c) c ## U"));
1077 assert!(has(&linux, "#define __INT16_C(c) c"));
1078 assert!(has(&linux, "#define __UINT16_C(c) c"));
1081 assert!(has(&linux, "#define __UINT8_C(c) c"));
1082 assert!(has(&linux, "#define __INT64_C(c) c ## L"));
1084 assert!(has(&linux, "#define __UINT64_C(c) c ## UL"));
1085 let windows = set_for("x86_64-pc-windows-msvc");
1087 assert!(has(&windows, "#define __INT64_C(c) c ## LL"));
1088 assert!(has(&windows, "#define __UINT64_C(c) c ## ULL"));
1089 }
1090
1091 #[test]
1092 fn the_symbol_prefix_is_defined_everywhere_including_where_it_is_empty() {
1093 for triple in
1098 ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
1099 {
1100 assert!(has(&set_for(triple), "#define __USER_LABEL_PREFIX__ "), "{triple}");
1101 }
1102 assert!(has(&set_for("aarch64-apple-darwin"), "#define __USER_LABEL_PREFIX__ _"));
1104 }
1105
1106 #[test]
1107 fn the_memory_orders_are_there_even_without_atomics() {
1108 let linux = set_for("x86_64-unknown-linux-gnu");
1113 assert!(has(&linux, "#define __ATOMIC_RELAXED 0"));
1114 assert!(has(&linux, "#define __ATOMIC_SEQ_CST 5"));
1115 assert!(has(&linux, "#define __STDC_NO_ATOMICS__ 1"), "and we still have no _Atomic");
1116 assert!(has(&linux, "#define __GCC_ATOMIC_INT_LOCK_FREE 2"));
1117 assert!(has(&linux, "#define __GCC_ATOMIC_LLONG_LOCK_FREE 2"));
1118 assert!(has(&set_for("x86_64-pc-windows-msvc"), "#define __GCC_ATOMIC_LLONG_LOCK_FREE 2"));
1119 }
1120
1121 #[test]
1122 fn long_double_is_three_types_and_the_macros_say_which() {
1123 assert!(has(&set_for("x86_64-unknown-linux-gnu"), "#define __LDBL_MANT_DIG__ 64"));
1124 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __LDBL_MANT_DIG__ 113"));
1125 assert!(has(&set_for("aarch64-apple-darwin"), "#define __LDBL_MANT_DIG__ 53"));
1126 }
1127
1128 #[test]
1129 fn the_extended_floating_types_have_the_limits_their_formats_have() {
1130 let linux = set_for("x86_64-unknown-linux-gnu");
1133 assert!(has(&linux, "#define __FLT16_MANT_DIG__ 11"));
1134 assert!(has(&linux, "#define __FLT32_MANT_DIG__ 24"));
1135 assert!(has(&linux, "#define __FLT64_MANT_DIG__ 53"));
1136 assert!(has(&linux, "#define __FLT128_MANT_DIG__ 113"));
1137 assert!(has(&linux, "#define __FLT32X_MANT_DIG__ 53"));
1138 assert!(has(&linux, "#define __FLT16_MAX__ 6.55040000000000000000000000000000000e+4F16"));
1141 assert!(has(
1142 &linux,
1143 "#define __FLT32X_MIN__ 2.22507385850720138309023271733240406e-308F32x"
1144 ));
1145 assert!(!linux.contains("__FLT128X_"));
1148 }
1149
1150 #[test]
1151 fn float64x_keeps_the_width_that_long_double_loses_on_apple() {
1152 let linux = set_for("x86_64-unknown-linux-gnu");
1155 assert!(has(&linux, "#define __FLT64X_MANT_DIG__ 64"));
1156 assert!(has(&linux, "#define __LDBL_MANT_DIG__ 64"));
1157 let mac = set_for("aarch64-apple-darwin");
1158 assert!(has(&mac, "#define __FLT64X_MANT_DIG__ 113"));
1159 assert!(has(&mac, "#define __LDBL_MANT_DIG__ 53"));
1160 let windows = set_for("x86_64-pc-windows-msvc");
1161 assert!(has(&windows, "#define __FLT64X_MANT_DIG__ 64"));
1162 assert!(has(&windows, "#define __LDBL_MANT_DIG__ 53"));
1163 }
1164
1165 #[test]
1166 fn the_largest_value_of_a_binary_format_is_also_its_largest_normal_one() {
1167 let linux = set_for("x86_64-unknown-linux-gnu");
1170 for prefix in ["FLT", "DBL", "LDBL", "FLT16", "FLT32", "FLT64", "FLT128", "FLT32X"] {
1171 let value = |suffix: &str| {
1172 let name = format!("#define __{prefix}_{suffix}__ ");
1173 let line = linux
1174 .lines()
1175 .find(|line| line.starts_with(&name))
1176 .unwrap_or_else(|| panic!("__{prefix}_{suffix}__ is defined"));
1177 line[name.len()..].to_owned()
1178 };
1179 assert_eq!(value("MAX"), value("NORM_MAX"), "__{prefix}_NORM_MAX__");
1180 }
1181 }
1182
1183 #[test]
1184 fn the_widest_bit_int_is_said_in_every_dialect() {
1185 let mut opts = Predef::new();
1189 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1190 assert!(has(&built_in(&target, &opts), "#define __BITINT_MAXWIDTH__ 128"));
1191 opts.std = Std::C17;
1192 assert!(has(&built_in(&target, &opts), "#define __BITINT_MAXWIDTH__ 128"));
1193 }
1194
1195 #[test]
1196 fn char_signedness_is_recorded_only_when_it_is_unsigned() {
1197 assert!(has(&set_for("aarch64-unknown-linux-gnu"), "#define __CHAR_UNSIGNED__ 1"));
1199 assert!(!has(&set_for("x86_64-unknown-linux-gnu"), "#define __CHAR_UNSIGNED__ 1"));
1200 }
1201
1202 #[test]
1203 fn the_dialect_decides_the_standard_macros() {
1204 let mut opts = Predef::new();
1205 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1206 assert!(has(&built_in(&target, &opts), "#define __STDC_VERSION__ 202311L"));
1207 assert!(!has(&built_in(&target, &opts), "#define __STRICT_ANSI__ 1"));
1208 assert!(has(&built_in(&target, &opts), "#define linux 1"));
1209
1210 opts.gnu_extensions = false;
1211 assert!(has(&built_in(&target, &opts), "#define __STRICT_ANSI__ 1"));
1212 assert!(!has(&built_in(&target, &opts), "#define linux 1"), "not a reserved name");
1213
1214 opts.std = Std::C89;
1215 let c89 = built_in(&target, &opts);
1216 assert!(!c89.contains("__STDC_VERSION__"), "C89 does not define it at all");
1217 assert!(has(&c89, "#define __STDC__ 1"));
1218 }
1219
1220 #[test]
1223 fn the_only_things_claimed_missing_are_the_ones_that_are_missing() {
1224 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1225 let opts = Predef::new();
1226 let set = built_in(&target, &opts);
1227 assert!(has(&set, "#define __STDC_NO_ATOMICS__ 1"), "there is no stdatomic.h to include");
1228 assert!(has(&set, "#define __STDC_NO_THREADS__ 1"), "nor a threads.h");
1229 assert!(has(&set, "#define __STDC_NO_COMPLEX__ 1"), "the arithmetic is not lowered");
1230 assert!(!set.contains("__STDC_NO_VLA__"), "variable length arrays work");
1231 }
1232
1233 #[test]
1237 fn the_type_behind_char8_t_is_defined_in_c23_and_in_no_dialect_before_it() {
1238 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1239 let mut opts = Predef::new();
1240 assert!(has(&built_in(&target, &opts), "#define __CHAR8_TYPE__ unsigned char"));
1241
1242 for older in [Std::C17, Std::C11, Std::C99, Std::C89] {
1243 opts.std = older;
1244 assert!(!built_in(&target, &opts).contains("__CHAR8_TYPE__"), "{older:?}");
1245 }
1246 }
1247
1248 #[test]
1249 fn the_optimizer_level_is_visible_to_the_preprocessor() {
1250 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1251 let mut opts = Predef::new();
1252 assert!(has(&built_in(&target, &opts), "#define __NO_INLINE__ 1"));
1253 assert!(!built_in(&target, &opts).contains("__OPTIMIZE__"));
1254
1255 opts.opt_level = OptLevel::O2;
1256 assert!(has(&built_in(&target, &opts), "#define __OPTIMIZE__ 1"));
1257 assert!(!built_in(&target, &opts).contains("__OPTIMIZE_SIZE__"));
1258
1259 opts.opt_level = OptLevel::Os;
1260 assert!(has(&built_in(&target, &opts), "#define __OPTIMIZE_SIZE__ 1"));
1261 }
1262
1263 #[test]
1264 fn a_command_line_define_with_no_value_is_one() {
1265 let mut opts = Predef::new();
1266 opts.defines = vec!["FOO".to_owned(), "BAR=2".to_owned(), "F(x)=x + 1".to_owned()];
1267 opts.undefines = vec!["__linux__".to_owned()];
1268 let text = command_line(&opts);
1269 assert!(has(&text, "#define FOO 1"));
1270 assert!(has(&text, "#define BAR 2"));
1271 assert!(has(&text, "#define F(x) x + 1"));
1272 assert!(text.trim_end().ends_with("#undef __linux__"));
1274 }
1275
1276 #[test]
1277 fn no_command_line_macros_is_no_file_at_all() {
1278 assert!(command_line(&Predef::new()).is_empty());
1279 }
1280
1281 #[test]
1282 fn a_date_is_spelled_the_way_the_standard_fixes() {
1283 let epoch = Timestamp::from_unix(0);
1285 assert_eq!(epoch.date, "Jan 1 1970");
1286 assert_eq!(epoch.time, "00:00:00");
1287 let leap = Timestamp::from_unix(1_709_164_800);
1288 assert_eq!(leap.date, "Feb 29 2024", "2024 is a leap year");
1289 let late = Timestamp::from_unix(1_735_689_599);
1290 assert_eq!(late.date, "Dec 31 2024");
1291 assert_eq!(late.time, "23:59:59");
1292 }
1293
1294 #[test]
1295 fn a_date_before_the_epoch_still_comes_out_right() {
1296 assert_eq!(Timestamp::from_unix(-1).date, "Dec 31 1969");
1299 assert_eq!(Timestamp::from_unix(-1).time, "23:59:59");
1300 }
1301
1302 #[test]
1303 fn the_gnuc_version_is_a_knob_rather_than_a_constant() {
1304 let target = TargetInfo::new("x86_64-unknown-linux-gnu".parse().unwrap());
1305 let mut opts = Predef::new();
1306 assert!(has(&built_in(&target, &opts), "#define __GNUC__ 7"));
1307 opts.gnuc = GnucVersion { major: 15, minor: 1, patch: 0 };
1308 assert!(has(&built_in(&target, &opts), "#define __GNUC__ 15"));
1309 assert!(has(&built_in(&target, &opts), "#define __GNUC_MINOR__ 1"));
1310 }
1311
1312 #[test]
1313 fn musl_and_glibc_disagree_about_the_fast_types_on_the_same_processor() {
1314 let gnu = set_for("x86_64-unknown-linux-gnu");
1319 let musl = set_for("x86_64-unknown-linux-musl");
1320 assert!(has(&gnu, "#define __INT_FAST16_TYPE__ long int"));
1321 assert!(has(&gnu, "#define __INT_FAST32_TYPE__ long int"));
1322 assert!(has(&gnu, "#define __UINT_FAST16_TYPE__ long unsigned int"));
1323 assert!(has(&musl, "#define __INT_FAST16_TYPE__ int"));
1324 assert!(has(&musl, "#define __INT_FAST32_TYPE__ int"));
1325 assert!(has(&musl, "#define __UINT_FAST16_TYPE__ unsigned int"));
1326 assert!(has(&gnu, "#define __INT_FAST16_MAX__ 0x7fffffffffffffffL"));
1329 assert!(has(&musl, "#define __INT_FAST16_MAX__ 0x7fffffff"));
1330 assert!(has(&musl, "#define __UINT_FAST16_MAX__ 0xffffffffU"));
1331 }
1332
1333 #[test]
1334 fn the_libc_only_moves_the_two_fast_types_it_is_allowed_to_move() {
1335 let gnu = set_for("x86_64-unknown-linux-gnu");
1338 let musl = set_for("x86_64-unknown-linux-musl");
1339 for line in [
1340 "#define __INT_FAST8_TYPE__ signed char",
1341 "#define __INT_FAST64_TYPE__ long int",
1342 "#define __INT64_TYPE__ long int",
1343 "#define __SIZE_TYPE__ long unsigned int",
1344 "#define __SIZEOF_LONG__ 8",
1345 "#define __LP64__ 1",
1346 ] {
1347 assert!(has(&gnu, line), "glibc lost {line}");
1348 assert!(has(&musl, line), "musl lost {line}");
1349 }
1350 }
1351
1352 #[test]
1353 fn a_non_x86_target_has_int_sized_fast_types_whatever_the_libc() {
1354 let arm_gnu = set_for("aarch64-unknown-linux-gnu");
1357 let arm_musl = set_for("aarch64-unknown-linux-musl");
1358 assert!(has(&arm_gnu, "#define __INT_FAST16_TYPE__ int"));
1359 assert!(has(&arm_musl, "#define __INT_FAST16_TYPE__ int"));
1360 }
1361}