intrinsics.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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(bench_black_box)]
  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. // truncdfsf2
  24. pub fn aeabi_d2f(x: f64) -> f32 {
  25. x as f32
  26. }
  27. // fixdfsi
  28. pub fn aeabi_d2i(x: f64) -> i32 {
  29. x as i32
  30. }
  31. // fixdfdi
  32. pub fn aeabi_d2l(x: f64) -> i64 {
  33. x as i64
  34. }
  35. // fixunsdfsi
  36. pub fn aeabi_d2uiz(x: f64) -> u32 {
  37. x as u32
  38. }
  39. // fixunsdfdi
  40. pub fn aeabi_d2ulz(x: f64) -> u64 {
  41. x as u64
  42. }
  43. // adddf3
  44. pub fn aeabi_dadd(a: f64, b: f64) -> f64 {
  45. a + b
  46. }
  47. // eqdf2
  48. pub fn aeabi_dcmpeq(a: f64, b: f64) -> bool {
  49. a == b
  50. }
  51. // gtdf2
  52. pub fn aeabi_dcmpgt(a: f64, b: f64) -> bool {
  53. a > b
  54. }
  55. // ltdf2
  56. pub fn aeabi_dcmplt(a: f64, b: f64) -> bool {
  57. a < b
  58. }
  59. // divdf3
  60. pub fn aeabi_ddiv(a: f64, b: f64) -> f64 {
  61. a / b
  62. }
  63. // muldf3
  64. pub fn aeabi_dmul(a: f64, b: f64) -> f64 {
  65. a * b
  66. }
  67. // subdf3
  68. pub fn aeabi_dsub(a: f64, b: f64) -> f64 {
  69. a - b
  70. }
  71. // extendsfdf2
  72. pub fn aeabi_f2d(x: f32) -> f64 {
  73. x as f64
  74. }
  75. // fixsfsi
  76. pub fn aeabi_f2iz(x: f32) -> i32 {
  77. x as i32
  78. }
  79. // fixsfdi
  80. pub fn aeabi_f2lz(x: f32) -> i64 {
  81. x as i64
  82. }
  83. // fixunssfsi
  84. pub fn aeabi_f2uiz(x: f32) -> u32 {
  85. x as u32
  86. }
  87. // fixunssfdi
  88. pub fn aeabi_f2ulz(x: f32) -> u64 {
  89. x as u64
  90. }
  91. // addsf3
  92. pub fn aeabi_fadd(a: f32, b: f32) -> f32 {
  93. a + b
  94. }
  95. // eqsf2
  96. pub fn aeabi_fcmpeq(a: f32, b: f32) -> bool {
  97. a == b
  98. }
  99. // gtsf2
  100. pub fn aeabi_fcmpgt(a: f32, b: f32) -> bool {
  101. a > b
  102. }
  103. // ltsf2
  104. pub fn aeabi_fcmplt(a: f32, b: f32) -> bool {
  105. a < b
  106. }
  107. // divsf3
  108. pub fn aeabi_fdiv(a: f32, b: f32) -> f32 {
  109. a / b
  110. }
  111. // mulsf3
  112. pub fn aeabi_fmul(a: f32, b: f32) -> f32 {
  113. a * b
  114. }
  115. // subsf3
  116. pub fn aeabi_fsub(a: f32, b: f32) -> f32 {
  117. a - b
  118. }
  119. // floatsidf
  120. pub fn aeabi_i2d(x: i32) -> f64 {
  121. x as f64
  122. }
  123. // floatsisf
  124. pub fn aeabi_i2f(x: i32) -> f32 {
  125. x as f32
  126. }
  127. pub fn aeabi_idiv(a: i32, b: i32) -> i32 {
  128. a.wrapping_div(b)
  129. }
  130. pub fn aeabi_idivmod(a: i32, b: i32) -> i32 {
  131. a % b
  132. }
  133. // floatdidf
  134. pub fn aeabi_l2d(x: i64) -> f64 {
  135. x as f64
  136. }
  137. // floatdisf
  138. pub fn aeabi_l2f(x: i64) -> f32 {
  139. x as f32
  140. }
  141. // divdi3
  142. pub fn aeabi_ldivmod(a: i64, b: i64) -> i64 {
  143. a / b
  144. }
  145. // muldi3
  146. pub fn aeabi_lmul(a: i64, b: i64) -> i64 {
  147. a.wrapping_mul(b)
  148. }
  149. // floatunsidf
  150. pub fn aeabi_ui2d(x: u32) -> f64 {
  151. x as f64
  152. }
  153. // floatunsisf
  154. pub fn aeabi_ui2f(x: u32) -> f32 {
  155. x as f32
  156. }
  157. pub fn aeabi_uidiv(a: u32, b: u32) -> u32 {
  158. a / b
  159. }
  160. pub fn aeabi_uidivmod(a: u32, b: u32) -> u32 {
  161. a % b
  162. }
  163. // floatundidf
  164. pub fn aeabi_ul2d(x: u64) -> f64 {
  165. x as f64
  166. }
  167. // floatundisf
  168. pub fn aeabi_ul2f(x: u64) -> f32 {
  169. x as f32
  170. }
  171. // udivdi3
  172. pub fn aeabi_uldivmod(a: u64, b: u64) -> u64 {
  173. a * b
  174. }
  175. pub fn moddi3(a: i64, b: i64) -> i64 {
  176. a % b
  177. }
  178. pub fn mulodi4(a: i64, b: i64) -> i64 {
  179. a * b
  180. }
  181. pub fn umoddi3(a: u64, b: u64) -> u64 {
  182. a % b
  183. }
  184. pub fn muloti4(a: u128, b: u128) -> Option<u128> {
  185. a.checked_mul(b)
  186. }
  187. pub fn multi3(a: u128, b: u128) -> u128 {
  188. a.wrapping_mul(b)
  189. }
  190. pub fn ashlti3(a: u128, b: usize) -> u128 {
  191. a >> b
  192. }
  193. pub fn ashrti3(a: u128, b: usize) -> u128 {
  194. a << b
  195. }
  196. pub fn lshrti3(a: i128, b: usize) -> i128 {
  197. a >> b
  198. }
  199. pub fn udivti3(a: u128, b: u128) -> u128 {
  200. a / b
  201. }
  202. pub fn umodti3(a: u128, b: u128) -> u128 {
  203. a % b
  204. }
  205. pub fn divti3(a: i128, b: i128) -> i128 {
  206. a / b
  207. }
  208. pub fn modti3(a: i128, b: i128) -> i128 {
  209. a % b
  210. }
  211. pub fn udivsi3(a: u32, b: u32) -> u32 {
  212. a / b
  213. }
  214. }
  215. fn run() {
  216. use core::hint::black_box as bb;
  217. use intrinsics::*;
  218. bb(aeabi_d2f(bb(2.)));
  219. bb(aeabi_d2i(bb(2.)));
  220. bb(aeabi_d2l(bb(2.)));
  221. bb(aeabi_d2uiz(bb(2.)));
  222. bb(aeabi_d2ulz(bb(2.)));
  223. bb(aeabi_dadd(bb(2.), bb(3.)));
  224. bb(aeabi_dcmpeq(bb(2.), bb(3.)));
  225. bb(aeabi_dcmpgt(bb(2.), bb(3.)));
  226. bb(aeabi_dcmplt(bb(2.), bb(3.)));
  227. bb(aeabi_ddiv(bb(2.), bb(3.)));
  228. bb(aeabi_dmul(bb(2.), bb(3.)));
  229. bb(aeabi_dsub(bb(2.), bb(3.)));
  230. bb(aeabi_f2d(bb(2.)));
  231. bb(aeabi_f2iz(bb(2.)));
  232. bb(aeabi_f2lz(bb(2.)));
  233. bb(aeabi_f2uiz(bb(2.)));
  234. bb(aeabi_f2ulz(bb(2.)));
  235. bb(aeabi_fadd(bb(2.), bb(3.)));
  236. bb(aeabi_fcmpeq(bb(2.), bb(3.)));
  237. bb(aeabi_fcmpgt(bb(2.), bb(3.)));
  238. bb(aeabi_fcmplt(bb(2.), bb(3.)));
  239. bb(aeabi_fdiv(bb(2.), bb(3.)));
  240. bb(aeabi_fmul(bb(2.), bb(3.)));
  241. bb(aeabi_fsub(bb(2.), bb(3.)));
  242. bb(aeabi_i2d(bb(2)));
  243. bb(aeabi_i2f(bb(2)));
  244. bb(aeabi_idiv(bb(2), bb(3)));
  245. bb(aeabi_idivmod(bb(2), bb(3)));
  246. bb(aeabi_l2d(bb(2)));
  247. bb(aeabi_l2f(bb(2)));
  248. bb(aeabi_ldivmod(bb(2), bb(3)));
  249. bb(aeabi_lmul(bb(2), bb(3)));
  250. bb(aeabi_ui2d(bb(2)));
  251. bb(aeabi_ui2f(bb(2)));
  252. bb(aeabi_uidiv(bb(2), bb(3)));
  253. bb(aeabi_uidivmod(bb(2), bb(3)));
  254. bb(aeabi_ul2d(bb(2)));
  255. bb(aeabi_ul2f(bb(2)));
  256. bb(aeabi_uldivmod(bb(2), bb(3)));
  257. bb(moddi3(bb(2), bb(3)));
  258. bb(mulodi4(bb(2), bb(3)));
  259. bb(umoddi3(bb(2), bb(3)));
  260. bb(muloti4(bb(2), bb(2)));
  261. bb(multi3(bb(2), bb(2)));
  262. bb(ashlti3(bb(2), bb(2)));
  263. bb(ashrti3(bb(2), bb(2)));
  264. bb(lshrti3(bb(2), bb(2)));
  265. bb(udivti3(bb(2), bb(2)));
  266. bb(umodti3(bb(2), bb(2)));
  267. bb(divti3(bb(2), bb(2)));
  268. bb(modti3(bb(2), bb(2)));
  269. bb(udivsi3(bb(2), bb(2)));
  270. something_with_a_dtor(&|| assert_eq!(bb(1), 1));
  271. extern "C" {
  272. fn rust_begin_unwind(x: usize);
  273. }
  274. // if bb(false) {
  275. unsafe {
  276. rust_begin_unwind(0);
  277. }
  278. // }
  279. }
  280. fn something_with_a_dtor(f: &dyn Fn()) {
  281. struct A<'a>(&'a (dyn Fn() + 'a));
  282. impl<'a> Drop for A<'a> {
  283. fn drop(&mut self) {
  284. (self.0)();
  285. }
  286. }
  287. let _a = A(f);
  288. f();
  289. }
  290. #[cfg(not(thumb))]
  291. #[start]
  292. fn main(_: isize, _: *const *const u8) -> isize {
  293. run();
  294. 0
  295. }
  296. #[cfg(thumb)]
  297. #[no_mangle]
  298. pub fn _start() -> ! {
  299. run();
  300. loop {}
  301. }
  302. #[cfg(windows)]
  303. #[link(name = "kernel32")]
  304. #[link(name = "msvcrt")]
  305. extern "C" {}
  306. // ARM targets need these symbols
  307. #[no_mangle]
  308. pub fn __aeabi_unwind_cpp_pr0() {}
  309. #[no_mangle]
  310. pub fn __aeabi_unwind_cpp_pr1() {}
  311. #[cfg(not(windows))]
  312. #[allow(non_snake_case)]
  313. #[no_mangle]
  314. pub fn _Unwind_Resume() {}
  315. #[cfg(not(windows))]
  316. #[lang = "eh_personality"]
  317. #[no_mangle]
  318. pub extern "C" fn eh_personality() {}
  319. #[cfg(all(windows, target_env = "gnu"))]
  320. mod mingw_unwinding {
  321. #[no_mangle]
  322. pub fn rust_eh_personality() {}
  323. #[no_mangle]
  324. pub fn rust_eh_unwind_resume() {}
  325. #[no_mangle]
  326. pub fn rust_eh_register_frames() {}
  327. #[no_mangle]
  328. pub fn rust_eh_unregister_frames() {}
  329. }