intrinsics.rs 9.4 KB

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