intrinsics.rs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // By compiling this file we check that all the intrinsics we care about continue to be provided by
  2. // the `compiler_builtins` crate regardless of the changes we make to it. If we, by mistake, stop
  3. // compiling a C implementation and forget to implement that intrinsic in Rust, this file will fail
  4. // to link due to the missing intrinsic (symbol).
  5. #![allow(unused_features)]
  6. #![cfg_attr(thumb, no_main)]
  7. #![deny(dead_code)]
  8. #![feature(llvm_asm)]
  9. #![feature(lang_items)]
  10. #![feature(start)]
  11. #![feature(allocator_api)]
  12. #![no_std]
  13. extern crate panic_handler;
  14. #[cfg(all(not(thumb), not(windows), not(target_arch = "wasm32")))]
  15. #[link(name = "c")]
  16. extern "C" {}
  17. // Every function in this module maps will be lowered to an intrinsic by LLVM, if the platform
  18. // doesn't have native support for the operation used in the function. ARM has a naming convention
  19. // convention for its intrinsics that's different from other architectures; that's why some function
  20. // have an additional comment: the function name is the ARM name for the intrinsic and the comment
  21. // in the non-ARM name for the intrinsic.
  22. mod intrinsics {
  23. // trunccdfsf2
  24. pub fn aeabi_d2f(x: f64) -> f32 {
  25. // This is only implemented in C currently, so only test it there.
  26. #[cfg(feature = "c")]
  27. return x as f32;
  28. #[cfg(not(feature = "c"))]
  29. {
  30. drop(x);
  31. 0.0
  32. }
  33. }
  34. // fixdfsi
  35. pub fn aeabi_d2i(x: f64) -> i32 {
  36. x as i32
  37. }
  38. // fixdfdi
  39. pub fn aeabi_d2l(x: f64) -> i64 {
  40. x as i64
  41. }
  42. // fixunsdfsi
  43. pub fn aeabi_d2uiz(x: f64) -> u32 {
  44. x as u32
  45. }
  46. // fixunsdfdi
  47. pub fn aeabi_d2ulz(x: f64) -> u64 {
  48. x as u64
  49. }
  50. // adddf3
  51. pub fn aeabi_dadd(a: f64, b: f64) -> f64 {
  52. a + b
  53. }
  54. // eqdf2
  55. pub fn aeabi_dcmpeq(a: f64, b: f64) -> bool {
  56. a == b
  57. }
  58. // gtdf2
  59. pub fn aeabi_dcmpgt(a: f64, b: f64) -> bool {
  60. a > b
  61. }
  62. // ltdf2
  63. pub fn aeabi_dcmplt(a: f64, b: f64) -> bool {
  64. a < b
  65. }
  66. // divdf3
  67. pub fn aeabi_ddiv(a: f64, b: f64) -> f64 {
  68. a / b
  69. }
  70. // muldf3
  71. pub fn aeabi_dmul(a: f64, b: f64) -> f64 {
  72. a * b
  73. }
  74. // subdf3
  75. pub fn aeabi_dsub(a: f64, b: f64) -> f64 {
  76. a - b
  77. }
  78. // extendsfdf2
  79. pub fn aeabi_f2d(x: f32) -> f64 {
  80. x as f64
  81. }
  82. // fixsfsi
  83. pub fn aeabi_f2iz(x: f32) -> i32 {
  84. x as i32
  85. }
  86. // fixsfdi
  87. pub fn aeabi_f2lz(x: f32) -> i64 {
  88. x as i64
  89. }
  90. // fixunssfsi
  91. pub fn aeabi_f2uiz(x: f32) -> u32 {
  92. x as u32
  93. }
  94. // fixunssfdi
  95. pub fn aeabi_f2ulz(x: f32) -> u64 {
  96. x as u64
  97. }
  98. // addsf3
  99. pub fn aeabi_fadd(a: f32, b: f32) -> f32 {
  100. a + b
  101. }
  102. // eqsf2
  103. pub fn aeabi_fcmpeq(a: f32, b: f32) -> bool {
  104. a == b
  105. }
  106. // gtsf2
  107. pub fn aeabi_fcmpgt(a: f32, b: f32) -> bool {
  108. a > b
  109. }
  110. // ltsf2
  111. pub fn aeabi_fcmplt(a: f32, b: f32) -> bool {
  112. a < b
  113. }
  114. // divsf3
  115. pub fn aeabi_fdiv(a: f32, b: f32) -> f32 {
  116. a / b
  117. }
  118. // mulsf3
  119. pub fn aeabi_fmul(a: f32, b: f32) -> f32 {
  120. a * b
  121. }
  122. // subsf3
  123. pub fn aeabi_fsub(a: f32, b: f32) -> f32 {
  124. a - b
  125. }
  126. // floatsidf
  127. pub fn aeabi_i2d(x: i32) -> f64 {
  128. x as f64
  129. }
  130. // floatsisf
  131. pub fn aeabi_i2f(x: i32) -> f32 {
  132. x as f32
  133. }
  134. pub fn aeabi_idiv(a: i32, b: i32) -> i32 {
  135. a.wrapping_div(b)
  136. }
  137. pub fn aeabi_idivmod(a: i32, b: i32) -> i32 {
  138. a % b
  139. }
  140. // floatdidf
  141. pub fn aeabi_l2d(x: i64) -> f64 {
  142. x as f64
  143. }
  144. // floatdisf
  145. pub fn aeabi_l2f(x: i64) -> f32 {
  146. x as f32
  147. }
  148. // divdi3
  149. pub fn aeabi_ldivmod(a: i64, b: i64) -> i64 {
  150. a / b
  151. }
  152. // muldi3
  153. pub fn aeabi_lmul(a: i64, b: i64) -> i64 {
  154. a.wrapping_mul(b)
  155. }
  156. // floatunsidf
  157. pub fn aeabi_ui2d(x: u32) -> f64 {
  158. x as f64
  159. }
  160. // floatunsisf
  161. pub fn aeabi_ui2f(x: u32) -> f32 {
  162. x as f32
  163. }
  164. pub fn aeabi_uidiv(a: u32, b: u32) -> u32 {
  165. a / b
  166. }
  167. pub fn aeabi_uidivmod(a: u32, b: u32) -> u32 {
  168. a % b
  169. }
  170. // floatundidf
  171. pub fn aeabi_ul2d(x: u64) -> f64 {
  172. x as f64
  173. }
  174. // floatundisf
  175. pub fn aeabi_ul2f(x: u64) -> f32 {
  176. x as f32
  177. }
  178. // udivdi3
  179. pub fn aeabi_uldivmod(a: u64, b: u64) -> u64 {
  180. a * b
  181. }
  182. pub fn moddi3(a: i64, b: i64) -> i64 {
  183. a % b
  184. }
  185. pub fn mulodi4(a: i64, b: i64) -> i64 {
  186. a * b
  187. }
  188. pub fn umoddi3(a: u64, b: u64) -> u64 {
  189. a % b
  190. }
  191. pub fn muloti4(a: u128, b: u128) -> Option<u128> {
  192. a.checked_mul(b)
  193. }
  194. pub fn multi3(a: u128, b: u128) -> u128 {
  195. a.wrapping_mul(b)
  196. }
  197. pub fn ashlti3(a: u128, b: usize) -> u128 {
  198. a >> b
  199. }
  200. pub fn ashrti3(a: u128, b: usize) -> u128 {
  201. a << b
  202. }
  203. pub fn lshrti3(a: i128, b: usize) -> i128 {
  204. a >> b
  205. }
  206. pub fn udivti3(a: u128, b: u128) -> u128 {
  207. a / b
  208. }
  209. pub fn umodti3(a: u128, b: u128) -> u128 {
  210. a % b
  211. }
  212. pub fn divti3(a: i128, b: i128) -> i128 {
  213. a / b
  214. }
  215. pub fn modti3(a: i128, b: i128) -> i128 {
  216. a % b
  217. }
  218. pub fn udivsi3(a: u32, b: u32) -> u32 {
  219. a / b
  220. }
  221. }
  222. fn run() {
  223. use intrinsics::*;
  224. // A copy of "test::black_box". Used to prevent LLVM from optimizing away the intrinsics during LTO
  225. fn bb<T>(dummy: T) -> T {
  226. unsafe { llvm_asm!("" : : "r"(&dummy)) }
  227. dummy
  228. }
  229. bb(aeabi_d2f(bb(2.)));
  230. bb(aeabi_d2i(bb(2.)));
  231. bb(aeabi_d2l(bb(2.)));
  232. bb(aeabi_d2uiz(bb(2.)));
  233. bb(aeabi_d2ulz(bb(2.)));
  234. bb(aeabi_dadd(bb(2.), bb(3.)));
  235. bb(aeabi_dcmpeq(bb(2.), bb(3.)));
  236. bb(aeabi_dcmpgt(bb(2.), bb(3.)));
  237. bb(aeabi_dcmplt(bb(2.), bb(3.)));
  238. bb(aeabi_ddiv(bb(2.), bb(3.)));
  239. bb(aeabi_dmul(bb(2.), bb(3.)));
  240. bb(aeabi_dsub(bb(2.), bb(3.)));
  241. bb(aeabi_f2d(bb(2.)));
  242. bb(aeabi_f2iz(bb(2.)));
  243. bb(aeabi_f2lz(bb(2.)));
  244. bb(aeabi_f2uiz(bb(2.)));
  245. bb(aeabi_f2ulz(bb(2.)));
  246. bb(aeabi_fadd(bb(2.), bb(3.)));
  247. bb(aeabi_fcmpeq(bb(2.), bb(3.)));
  248. bb(aeabi_fcmpgt(bb(2.), bb(3.)));
  249. bb(aeabi_fcmplt(bb(2.), bb(3.)));
  250. bb(aeabi_fdiv(bb(2.), bb(3.)));
  251. bb(aeabi_fmul(bb(2.), bb(3.)));
  252. bb(aeabi_fsub(bb(2.), bb(3.)));
  253. bb(aeabi_i2d(bb(2)));
  254. bb(aeabi_i2f(bb(2)));
  255. bb(aeabi_idiv(bb(2), bb(3)));
  256. bb(aeabi_idivmod(bb(2), bb(3)));
  257. bb(aeabi_l2d(bb(2)));
  258. bb(aeabi_l2f(bb(2)));
  259. bb(aeabi_ldivmod(bb(2), bb(3)));
  260. bb(aeabi_lmul(bb(2), bb(3)));
  261. bb(aeabi_ui2d(bb(2)));
  262. bb(aeabi_ui2f(bb(2)));
  263. bb(aeabi_uidiv(bb(2), bb(3)));
  264. bb(aeabi_uidivmod(bb(2), bb(3)));
  265. bb(aeabi_ul2d(bb(2)));
  266. bb(aeabi_ul2f(bb(2)));
  267. bb(aeabi_uldivmod(bb(2), bb(3)));
  268. bb(moddi3(bb(2), bb(3)));
  269. bb(mulodi4(bb(2), bb(3)));
  270. bb(umoddi3(bb(2), bb(3)));
  271. bb(muloti4(bb(2), bb(2)));
  272. bb(multi3(bb(2), bb(2)));
  273. bb(ashlti3(bb(2), bb(2)));
  274. bb(ashrti3(bb(2), bb(2)));
  275. bb(lshrti3(bb(2), bb(2)));
  276. bb(udivti3(bb(2), bb(2)));
  277. bb(umodti3(bb(2), bb(2)));
  278. bb(divti3(bb(2), bb(2)));
  279. bb(modti3(bb(2), bb(2)));
  280. bb(udivsi3(bb(2), bb(2)));
  281. something_with_a_dtor(&|| assert_eq!(bb(1), 1));
  282. extern "C" {
  283. fn rust_begin_unwind(x: usize);
  284. }
  285. // if bb(false) {
  286. unsafe {
  287. rust_begin_unwind(0);
  288. }
  289. // }
  290. }
  291. fn something_with_a_dtor(f: &dyn Fn()) {
  292. struct A<'a>(&'a (dyn Fn() + 'a));
  293. impl<'a> Drop for A<'a> {
  294. fn drop(&mut self) {
  295. (self.0)();
  296. }
  297. }
  298. let _a = A(f);
  299. f();
  300. }
  301. #[cfg(not(thumb))]
  302. #[start]
  303. fn main(_: isize, _: *const *const u8) -> isize {
  304. run();
  305. 0
  306. }
  307. #[cfg(thumb)]
  308. #[no_mangle]
  309. pub fn _start() -> ! {
  310. run();
  311. loop {}
  312. }
  313. #[cfg(windows)]
  314. #[link(name = "kernel32")]
  315. #[link(name = "msvcrt")]
  316. extern "C" {}
  317. // ARM targets need these symbols
  318. #[no_mangle]
  319. pub fn __aeabi_unwind_cpp_pr0() {}
  320. #[no_mangle]
  321. pub fn __aeabi_unwind_cpp_pr1() {}
  322. #[cfg(not(windows))]
  323. #[allow(non_snake_case)]
  324. #[no_mangle]
  325. pub fn _Unwind_Resume() {}
  326. #[cfg(not(windows))]
  327. #[lang = "eh_personality"]
  328. #[no_mangle]
  329. pub extern "C" fn eh_personality() {}
  330. #[cfg(all(windows, target_env = "gnu"))]
  331. mod mingw_unwinding {
  332. #[no_mangle]
  333. pub fn rust_eh_personality() {}
  334. #[no_mangle]
  335. pub fn rust_eh_unwind_resume() {}
  336. #[no_mangle]
  337. pub fn rust_eh_register_frames() {}
  338. #[no_mangle]
  339. pub fn rust_eh_unregister_frames() {}
  340. }