intrinsics.rs 8.2 KB

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