]> git.lizzy.rs Git - rust.git/blob - src/libcore/slice/memchr.rs
Auto merge of #58406 - Disasm:rv64-support, r=nagisa
[rust.git] / src / libcore / slice / memchr.rs
1 // Original implementation taken from rust-memchr.
2 // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
3
4 use cmp;
5 use mem;
6
7 const LO_U64: u64 = 0x0101010101010101;
8 const HI_U64: u64 = 0x8080808080808080;
9
10 // Use truncation.
11 const LO_USIZE: usize = LO_U64 as usize;
12 const HI_USIZE: usize = HI_U64 as usize;
13
14 /// Returns `true` if `x` contains any zero byte.
15 ///
16 /// From *Matters Computational*, J. Arndt:
17 ///
18 /// "The idea is to subtract one from each of the bytes and then look for
19 /// bytes where the borrow propagated all the way to the most significant
20 /// bit."
21 #[inline]
22 fn contains_zero_byte(x: usize) -> bool {
23     x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
24 }
25
26 #[cfg(target_pointer_width = "16")]
27 #[inline]
28 fn repeat_byte(b: u8) -> usize {
29     (b as usize) << 8 | b as usize
30 }
31
32 #[cfg(not(target_pointer_width = "16"))]
33 #[inline]
34 fn repeat_byte(b: u8) -> usize {
35     (b as usize) * (::usize::MAX / 255)
36 }
37
38 /// Returns the first index matching the byte `x` in `text`.
39 pub fn memchr(x: u8, text: &[u8]) -> Option<usize> {
40     // Scan for a single byte value by reading two `usize` words at a time.
41     //
42     // Split `text` in three parts
43     // - unaligned initial part, before the first word aligned address in text
44     // - body, scan by 2 words at a time
45     // - the last remaining part, < 2 word size
46     let len = text.len();
47     let ptr = text.as_ptr();
48     let usize_bytes = mem::size_of::<usize>();
49
50     // search up to an aligned boundary
51     let mut offset = ptr.align_offset(usize_bytes);
52     if offset > 0 {
53         offset = cmp::min(offset, len);
54         if let Some(index) = text[..offset].iter().position(|elt| *elt == x) {
55             return Some(index);
56         }
57     }
58
59     // search the body of the text
60     let repeated_x = repeat_byte(x);
61
62     if len >= 2 * usize_bytes {
63         while offset <= len - 2 * usize_bytes {
64             unsafe {
65                 let u = *(ptr.add(offset) as *const usize);
66                 let v = *(ptr.add(offset + usize_bytes) as *const usize);
67
68                 // break if there is a matching byte
69                 let zu = contains_zero_byte(u ^ repeated_x);
70                 let zv = contains_zero_byte(v ^ repeated_x);
71                 if zu || zv {
72                     break;
73                 }
74             }
75             offset += usize_bytes * 2;
76         }
77     }
78
79     // Find the byte after the point the body loop stopped.
80     text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i)
81 }
82
83 /// Returns the last index matching the byte `x` in `text`.
84 pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
85     // Scan for a single byte value by reading two `usize` words at a time.
86     //
87     // Split `text` in three parts:
88     // - unaligned tail, after the last word aligned address in text,
89     // - body, scanned by 2 words at a time,
90     // - the first remaining bytes, < 2 word size.
91     let len = text.len();
92     let ptr = text.as_ptr();
93     type Chunk = usize;
94
95     let (min_aligned_offset, max_aligned_offset) = {
96         // We call this just to obtain the length of the prefix and suffix.
97         // In the middle we always process two chunks at once.
98         let (prefix, _, suffix) = unsafe { text.align_to::<(Chunk, Chunk)>() };
99         (prefix.len(), len - suffix.len())
100     };
101
102     let mut offset = max_aligned_offset;
103     if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
104         return Some(offset + index);
105     }
106
107     // Search the body of the text, make sure we don't cross min_aligned_offset.
108     // offset is always aligned, so just testing `>` is sufficient and avoids possible
109     // overflow.
110     let repeated_x = repeat_byte(x);
111     let chunk_bytes = mem::size_of::<Chunk>();
112
113     while offset > min_aligned_offset {
114         unsafe {
115             let u = *(ptr.offset(offset as isize - 2 * chunk_bytes as isize) as *const Chunk);
116             let v = *(ptr.offset(offset as isize - chunk_bytes as isize) as *const Chunk);
117
118             // Break if there is a matching byte.
119             let zu = contains_zero_byte(u ^ repeated_x);
120             let zv = contains_zero_byte(v ^ repeated_x);
121             if zu || zv {
122                 break;
123             }
124         }
125         offset -= 2 * chunk_bytes;
126     }
127
128     // Find the byte before the point the body loop stopped.
129     text[..offset].iter().rposition(|elt| *elt == x)
130 }