intrinsics.rs 9.2 KB

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