aeabi_memset.rs 5.9 KB

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