aboutsummaryrefslogtreecommitdiff
path: root/src/display/display_sink/imp_x86.rs
blob: 902ea69c934cb148e7fc89158ee62b6feade6bed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! `imp_x86` has specialized copies to append short strings to strings. buffer sizing must be
//! handled by callers, in all cases.
//!
//! the structure of all implementations here is, essentially, to take the size of the data to
//! append and execute a copy for each bit set in that size, from highest to lowest. some bits are
//! simply never checked if the input is promised to never be that large - if a string to append is
//! only 0..7 bytes long, it is sufficient to only look at the low three bits to copy all bytes.
//!
//! in this way, it is slightly more efficient to right-size which append function is used, if the
//! maximum size of input strings can be bounded well. if the maximum size of input strings cannot
//! be bounded, you shouldn't be using these functions.

/// append `data` to `buf`, assuming `data` is less than 8 bytes and that `buf` has enough space
/// remaining to hold all bytes in `data`.
///
/// Safety: callers must ensure that `buf.capacity() - buf.len() >= data.len()`.
#[inline(always)]
pub unsafe fn append_string_lt_8_unchecked(buf: &mut alloc::string::String, data: &str) {
    // Safety: we are appending only valid utf8 strings to `self.buf`, as `s` is known to
    // be valid utf8
    let buf = unsafe { buf.as_mut_vec() };
    let new_bytes = data.as_bytes();

    unsafe {
        let dest = buf.as_mut_ptr().offset(buf.len() as isize);
        let src = new_bytes.as_ptr();

        let rem = new_bytes.len() as isize;

        // set_len early because there is no way to avoid the following asm!() writing that
        // same number of bytes into buf
        buf.set_len(buf.len() + new_bytes.len());

        core::arch::asm!(
            "8:",
            "cmp {rem:e}, 4",
            "jb 9f",
            "mov {buf:e}, dword ptr [{src} + {rem} - 4]",
            "mov dword ptr [{dest} + {rem} - 4], {buf:e}",
            "sub {rem:e}, 4",
            "jz 11f",
            "9:",
            "cmp {rem:e}, 2",
            "jb 10f",
            "mov {buf:x}, word ptr [{src} + {rem} - 2]",
            "mov word ptr [{dest} + {rem} - 2], {buf:x}",
            "sub {rem:e}, 2",
            "jz 11f",
            "10:",
            "cmp {rem:e}, 1",
            "jb 11f",
            "mov {buf:l}, byte ptr [{src} + {rem} - 1]",
            "mov byte ptr [{dest} + {rem} - 1], {buf:l}",
            "11:",
            src = in(reg) src,
            dest = in(reg) dest,
            rem = inout(reg) rem => _,
            buf = out(reg) _,
            options(nostack),
        );
    }
}

/// append `data` to `buf`, assuming `data` is less than 16 bytes and that `buf` has enough space
/// remaining to hold all bytes in `data`.
///
/// Safety: callers must ensure that `buf.capacity() - buf.len() >= data.len()`.
#[inline(always)]
pub unsafe fn append_string_lt_16_unchecked(buf: &mut alloc::string::String, data: &str) {
    // Safety: we are appending only valid utf8 strings to `self.buf`, as `s` is known to
    // be valid utf8
    let buf = unsafe { buf.as_mut_vec() };
    let new_bytes = data.as_bytes();

    unsafe {
        let dest = buf.as_mut_ptr().offset(buf.len() as isize);
        let src = new_bytes.as_ptr();

        let rem = new_bytes.len() as isize;

        // set_len early because there is no way to avoid the following asm!() writing that
        // same number of bytes into buf
        buf.set_len(buf.len() + new_bytes.len());

        core::arch::asm!(
            "7:",
            "cmp {rem:e}, 8",
            "jb 8f",
            "mov {buf:r}, qword ptr [{src} + {rem} - 8]",
            "mov qword ptr [{dest} + {rem} - 8], {buf:r}",
            "sub {rem:e}, 8",
            "jz 11f",
            "8:",
            "cmp {rem:e}, 4",
            "jb 9f",
            "mov {buf:e}, dword ptr [{src} + {rem} - 4]",
            "mov dword ptr [{dest} + {rem} - 4], {buf:e}",
            "sub {rem:e}, 4",
            "jz 11f",
            "9:",
            "cmp {rem:e}, 2",
            "jb 10f",
            "mov {buf:x}, word ptr [{src} + {rem} - 2]",
            "mov word ptr [{dest} + {rem} - 2], {buf:x}",
            "sub {rem:e}, 2",
            "jz 11f",
            "10:",
            "cmp {rem:e}, 1",
            "jb 11f",
            "mov {buf:l}, byte ptr [{src} + {rem} - 1]",
            "mov byte ptr [{dest} + {rem} - 1], {buf:l}",
            "11:",
            src = in(reg) src,
            dest = in(reg) dest,
            rem = inout(reg) rem => _,
            buf = out(reg) _,
            options(nostack),
        );
    }
}

/// append `data` to `buf`, assuming `data` is less than 32 bytes and that `buf` has enough space
/// remaining to hold all bytes in `data`.
///
/// Safety: callers must ensure that `buf.capacity() - buf.len() >= data.len()`.
#[inline(always)]
pub unsafe fn append_string_lt_32_unchecked(buf: &mut alloc::string::String, data: &str) {
    // Safety: we are appending only valid utf8 strings to `self.buf`, as `s` is known to
    // be valid utf8
    let buf = unsafe { buf.as_mut_vec() };
    let new_bytes = data.as_bytes();

    unsafe {
        let dest = buf.as_mut_ptr().offset(buf.len() as isize);
        let src = new_bytes.as_ptr();

        let rem = new_bytes.len() as isize;

        // set_len early because there is no way to avoid the following asm!() writing that
        // same number of bytes into buf
        buf.set_len(buf.len() + new_bytes.len());

        core::arch::asm!(
            "6:",
            "cmp {rem:e}, 16",
            "jb 7f",
            "mov {buf:r}, qword ptr [{src} + {rem} - 16]",
            "mov qword ptr [{dest} + {rem} - 16], {buf:r}",
            "mov {buf:r}, qword ptr [{src} + {rem} - 8]",
            "mov qword ptr [{dest} + {rem} - 8], {buf:r}",
            "sub {rem:e}, 16",
            "jz 11f",
            "7:",
            "cmp {rem:e}, 8",
            "jb 8f",
            "mov {buf:r}, qword ptr [{src} + {rem} - 8]",
            "mov qword ptr [{dest} + {rem} - 8], {buf:r}",
            "sub {rem:e}, 8",
            "jz 11f",
            "8:",
            "cmp {rem:e}, 4",
            "jb 9f",
            "mov {buf:e}, dword ptr [{src} + {rem} - 4]",
            "mov dword ptr [{dest} + {rem} - 4], {buf:e}",
            "sub {rem:e}, 4",
            "jz 11f",
            "9:",
            "cmp {rem:e}, 2",
            "jb 10f",
            "mov {buf:x}, word ptr [{src} + {rem} - 2]",
            "mov word ptr [{dest} + {rem} - 2], {buf:x}",
            "sub {rem:e}, 2",
            "jz 11f",
            "10:",
            "cmp {rem:e}, 1",
            "jb 11f",
            "mov {buf:l}, byte ptr [{src} + {rem} - 1]",
            "mov byte ptr [{dest} + {rem} - 1], {buf:l}",
            "11:",
            src = in(reg) src,
            dest = in(reg) dest,
            rem = inout(reg) rem => _,
            buf = out(reg) _,
            options(nostack),
        );
    }
}