@@ -41,44 +41,42 @@ unsafe fn read_usize_unaligned(x: *const usize) -> usize {
41
41
core:: mem:: transmute ( x_read)
42
42
}
43
43
44
- /// Loads a `T`-sized chunk from `src` into `dst` at offset `offset`, if that does not exceed
45
- /// `load_sz`. The offset pointers must both be `T`-aligned. Returns the new offset, advanced by the
46
- /// chunk size if a load happened.
47
- #[ cfg( not( feature = "mem-unaligned" ) ) ]
48
44
#[ inline( always) ]
49
- unsafe fn load_chunk_aligned < T : Copy > (
50
- src : * const usize ,
51
- dst : * mut usize ,
52
- load_sz : usize ,
53
- offset : usize ,
54
- ) -> usize {
55
- let chunk_sz = core:: mem:: size_of :: < T > ( ) ;
56
- if ( load_sz & chunk_sz) != 0 {
57
- * dst. wrapping_byte_add ( offset) . cast :: < T > ( ) = * src. wrapping_byte_add ( offset) . cast :: < T > ( ) ;
58
- offset | chunk_sz
59
- } else {
60
- offset
45
+ unsafe fn copy_forward_bytes ( mut dest : * mut u8 , mut src : * const u8 , n : usize ) {
46
+ let dest_end = dest. wrapping_add ( n) ;
47
+ while dest < dest_end {
48
+ * dest = * src;
49
+ dest = dest. wrapping_add ( 1 ) ;
50
+ src = src. wrapping_add ( 1 ) ;
61
51
}
62
52
}
63
53
64
- /// Load `load_sz` many bytes from `src`, which must be usize-aligned. Acts as if we did a `usize`
65
- /// read with the out-of-bounds part filled with 0s.
66
- /// `load_sz` be strictly less than `WORD_SIZE`.
54
+ /// Load `load_sz` many bytes from `src`, which must be usize-aligned.
55
+ /// `load_sz` be strictly less than `WORD_SIZE`. The remaining bytes are filled non-deterministically.
67
56
#[ cfg( not( feature = "mem-unaligned" ) ) ]
68
57
#[ inline( always) ]
69
58
unsafe fn load_aligned_partial ( src : * const usize , load_sz : usize ) -> usize {
70
59
debug_assert ! ( load_sz < WORD_SIZE ) ;
71
- // We can read up to 7 bytes here, which is enough for WORD_SIZE of 8
72
- // (since `load_sz < WORD_SIZE`).
73
- const { assert ! ( WORD_SIZE <= 8 ) } ;
60
+ debug_assert ! ( src. addr( ) % WORD_SIZE == 0 ) ;
61
+
62
+ let mut out: usize ;
63
+ core:: cfg_match! {
64
+ // We don't need an x86 path here as `feature = "mem-unaligned"` is always set there.
65
+ all( not( miri) , any( target_arch = "arm" , target_arch = "aarch64" , target_arch = "arm64ec" ) ) => {
66
+ core:: arch:: asm!(
67
+ "ldr {out}, [{src}]" ,
68
+ src = in( reg) src,
69
+ out = lateout( reg) out,
70
+ options( nostack, readonly, preserves_flags) ,
71
+ ) ;
72
+ }
73
+ _ => {
74
+ out = 0 ;
75
+ copy_forward_bytes( & raw mut out as * mut u8 , src as * mut u8 , load_sz) ;
76
+ }
77
+
78
+ }
74
79
75
- let mut i = 0 ;
76
- let mut out = 0usize ;
77
- // We load in decreasing order, so the pointers remain sufficiently aligned for the next step.
78
- i = load_chunk_aligned :: < u32 > ( src, & raw mut out, load_sz, i) ;
79
- i = load_chunk_aligned :: < u16 > ( src, & raw mut out, load_sz, i) ;
80
- i = load_chunk_aligned :: < u8 > ( src, & raw mut out, load_sz, i) ;
81
- debug_assert ! ( i == load_sz) ;
82
80
out
83
81
}
84
82
@@ -90,35 +88,34 @@ unsafe fn load_aligned_partial(src: *const usize, load_sz: usize) -> usize {
90
88
#[ inline( always) ]
91
89
unsafe fn load_aligned_end_partial ( src : * const usize , load_sz : usize ) -> usize {
92
90
debug_assert ! ( load_sz < WORD_SIZE ) ;
93
- // We can read up to 7 bytes here, which is enough for WORD_SIZE of 8
94
- // (since `load_sz < WORD_SIZE`).
95
- const { assert ! ( WORD_SIZE <= 8 ) } ;
91
+ debug_assert ! ( src. addr( ) % WORD_SIZE == 0 ) ;
92
+
93
+ let mut out: usize ;
94
+ core:: cfg_match! {
95
+ // We don't need an x86 path here as `feature = "mem-unaligned"` is always set there.
96
+ all( not( miri) , any( target_arch = "arm" , target_arch = "aarch64" , target_arch = "arm64ec" ) ) => {
97
+ core:: arch:: asm!(
98
+ "ldr {out}, [{src}]" ,
99
+ src = in( reg) src,
100
+ out = lateout( reg) out,
101
+ options( nostack, readonly, preserves_flags) ,
102
+ ) ;
103
+ }
104
+ _ => {
105
+ out = 0 ;
106
+ // Obtain pointers pointing to the beginning of the range we want to load.
107
+ let src_shifted = src. wrapping_byte_add( WORD_SIZE - load_sz) ;
108
+ let out_shifted = ( & raw mut out) . wrapping_byte_add( WORD_SIZE - load_sz) ;
109
+ copy_forward_bytes( out_shifted as * mut u8 , src_shifted as * mut u8 , load_sz) ;
110
+ }
111
+
112
+ }
96
113
97
- let mut i = 0 ;
98
- let mut out = 0usize ;
99
- // Obtain pointers pointing to the beginning of the range we want to load.
100
- let src_shifted = src. wrapping_byte_add ( WORD_SIZE - load_sz) ;
101
- let out_shifted = ( & raw mut out) . wrapping_byte_add ( WORD_SIZE - load_sz) ;
102
- // We load in increasing order, so by the time we reach `u16` things are 2-aligned etc.
103
- i = load_chunk_aligned :: < u8 > ( src_shifted, out_shifted, load_sz, i) ;
104
- i = load_chunk_aligned :: < u16 > ( src_shifted, out_shifted, load_sz, i) ;
105
- i = load_chunk_aligned :: < u32 > ( src_shifted, out_shifted, load_sz, i) ;
106
- debug_assert ! ( i == load_sz) ;
107
114
out
108
115
}
109
116
110
117
#[ inline( always) ]
111
118
pub unsafe fn copy_forward ( mut dest : * mut u8 , mut src : * const u8 , mut n : usize ) {
112
- #[ inline( always) ]
113
- unsafe fn copy_forward_bytes ( mut dest : * mut u8 , mut src : * const u8 , n : usize ) {
114
- let dest_end = dest. wrapping_add ( n) ;
115
- while dest < dest_end {
116
- * dest = * src;
117
- dest = dest. wrapping_add ( 1 ) ;
118
- src = src. wrapping_add ( 1 ) ;
119
- }
120
- }
121
-
122
119
#[ inline( always) ]
123
120
unsafe fn copy_forward_aligned_words ( dest : * mut u8 , src : * const u8 , n : usize ) {
124
121
let mut dest_usize = dest as * mut usize ;
0 commit comments