aeabi_memset.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #![cfg(all(
  2. target_arch = "arm",
  3. not(any(target_env = "gnu", target_env = "musl")),
  4. target_os = "linux",
  5. feature = "mem"
  6. ))]
  7. #![feature(compiler_builtins_lib)]
  8. #![feature(lang_items)]
  9. #![no_std]
  10. extern crate compiler_builtins;
  11. // test runner
  12. extern crate utest_cortex_m_qemu;
  13. // overrides `panic!`
  14. #[macro_use]
  15. extern crate utest_macros;
  16. use core::mem;
  17. macro_rules! panic {
  18. ($($tt:tt)*) => {
  19. upanic!($($tt)*);
  20. };
  21. }
  22. extern "C" {
  23. fn __aeabi_memset4(dest: *mut u8, n: usize, c: u32);
  24. }
  25. struct Aligned {
  26. array: [u8; 8],
  27. _alignment: [u32; 0],
  28. }
  29. impl Aligned {
  30. fn new(array: [u8; 8]) -> Self {
  31. Aligned {
  32. array: array,
  33. _alignment: [],
  34. }
  35. }
  36. }
  37. #[test]
  38. fn zero() {
  39. let mut aligned = Aligned::new([0u8; 8]);
  40. assert_eq!(mem::align_of_val(&aligned), 4);
  41. let xs = &mut aligned.array;
  42. let c = 0xdeadbeef;
  43. unsafe { __aeabi_memset4(xs.as_mut_ptr(), 0, c) }
  44. assert_eq!(*xs, [0; 8]);
  45. let mut aligned = Aligned::new([1u8; 8]);
  46. assert_eq!(mem::align_of_val(&aligned), 4);
  47. let xs = &mut aligned.array;
  48. let c = 0xdeadbeef;
  49. unsafe { __aeabi_memset4(xs.as_mut_ptr(), 0, c) }
  50. assert_eq!(*xs, [1; 8]);
  51. }
  52. #[test]
  53. fn one() {
  54. let mut aligned = Aligned::new([0u8; 8]);
  55. assert_eq!(mem::align_of_val(&aligned), 4);
  56. let xs = &mut aligned.array;
  57. let n = 1;
  58. let c = 0xdeadbeef;
  59. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  60. assert_eq!(*xs, [0xef, 0, 0, 0, 0, 0, 0, 0]);
  61. let mut aligned = Aligned::new([1u8; 8]);
  62. assert_eq!(mem::align_of_val(&aligned), 4);
  63. let xs = &mut aligned.array;
  64. let c = 0xdeadbeef;
  65. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  66. assert_eq!(*xs, [0xef, 1, 1, 1, 1, 1, 1, 1]);
  67. }
  68. #[test]
  69. fn two() {
  70. let mut aligned = Aligned::new([0u8; 8]);
  71. assert_eq!(mem::align_of_val(&aligned), 4);
  72. let xs = &mut aligned.array;
  73. let n = 2;
  74. let c = 0xdeadbeef;
  75. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  76. assert_eq!(*xs, [0xef, 0xef, 0, 0, 0, 0, 0, 0]);
  77. let mut aligned = Aligned::new([1u8; 8]);
  78. assert_eq!(mem::align_of_val(&aligned), 4);
  79. let xs = &mut aligned.array;
  80. let c = 0xdeadbeef;
  81. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  82. assert_eq!(*xs, [0xef, 0xef, 1, 1, 1, 1, 1, 1]);
  83. }
  84. #[test]
  85. fn three() {
  86. let mut aligned = Aligned::new([0u8; 8]);
  87. assert_eq!(mem::align_of_val(&aligned), 4);
  88. let xs = &mut aligned.array;
  89. let n = 3;
  90. let c = 0xdeadbeef;
  91. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  92. assert_eq!(*xs, [0xef, 0xef, 0xef, 0, 0, 0, 0, 0]);
  93. let mut aligned = Aligned::new([1u8; 8]);
  94. assert_eq!(mem::align_of_val(&aligned), 4);
  95. let xs = &mut aligned.array;
  96. let c = 0xdeadbeef;
  97. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  98. assert_eq!(*xs, [0xef, 0xef, 0xef, 1, 1, 1, 1, 1]);
  99. }
  100. #[test]
  101. fn four() {
  102. let mut aligned = Aligned::new([0u8; 8]);
  103. assert_eq!(mem::align_of_val(&aligned), 4);
  104. let xs = &mut aligned.array;
  105. let n = 4;
  106. let c = 0xdeadbeef;
  107. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  108. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0, 0, 0, 0]);
  109. let mut aligned = Aligned::new([1u8; 8]);
  110. assert_eq!(mem::align_of_val(&aligned), 4);
  111. let xs = &mut aligned.array;
  112. let c = 0xdeadbeef;
  113. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  114. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 1, 1, 1, 1]);
  115. }
  116. #[test]
  117. fn five() {
  118. let mut aligned = Aligned::new([0u8; 8]);
  119. assert_eq!(mem::align_of_val(&aligned), 4);
  120. let xs = &mut aligned.array;
  121. let n = 5;
  122. let c = 0xdeadbeef;
  123. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  124. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0xef, 0, 0, 0]);
  125. let mut aligned = Aligned::new([1u8; 8]);
  126. assert_eq!(mem::align_of_val(&aligned), 4);
  127. let xs = &mut aligned.array;
  128. let c = 0xdeadbeef;
  129. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  130. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0xef, 1, 1, 1]);
  131. }
  132. #[test]
  133. fn six() {
  134. let mut aligned = Aligned::new([0u8; 8]);
  135. assert_eq!(mem::align_of_val(&aligned), 4);
  136. let xs = &mut aligned.array;
  137. let n = 6;
  138. let c = 0xdeadbeef;
  139. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  140. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0, 0]);
  141. let mut aligned = Aligned::new([1u8; 8]);
  142. assert_eq!(mem::align_of_val(&aligned), 4);
  143. let xs = &mut aligned.array;
  144. let c = 0xdeadbeef;
  145. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  146. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 1, 1]);
  147. }
  148. #[test]
  149. fn seven() {
  150. let mut aligned = Aligned::new([0u8; 8]);
  151. assert_eq!(mem::align_of_val(&aligned), 4);
  152. let xs = &mut aligned.array;
  153. let n = 7;
  154. let c = 0xdeadbeef;
  155. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  156. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0]);
  157. let mut aligned = Aligned::new([1u8; 8]);
  158. assert_eq!(mem::align_of_val(&aligned), 4);
  159. let xs = &mut aligned.array;
  160. let c = 0xdeadbeef;
  161. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  162. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 1]);
  163. }
  164. #[test]
  165. fn eight() {
  166. let mut aligned = Aligned::new([0u8; 8]);
  167. assert_eq!(mem::align_of_val(&aligned), 4);
  168. let xs = &mut aligned.array;
  169. let n = 8;
  170. let c = 0xdeadbeef;
  171. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  172. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef]);
  173. let mut aligned = Aligned::new([1u8; 8]);
  174. assert_eq!(mem::align_of_val(&aligned), 4);
  175. let xs = &mut aligned.array;
  176. let c = 0xdeadbeef;
  177. unsafe { __aeabi_memset4(xs.as_mut_ptr(), n, c) }
  178. assert_eq!(*xs, [0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef]);
  179. }