aeabi_memset.rs 5.9 KB

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