intrinsics.rs 9.1 KB

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