1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _VIDEO_ATAFB_UTILS_H
3 #define _VIDEO_ATAFB_UTILS_H
5 /* ================================================================= */
6 /* Utility Assembler Functions */
7 /* ================================================================= */
9 /* ====================================================================== */
11 /* Those of a delicate disposition might like to skip the next couple of
14 * These functions are drop in replacements for memmove and
15 * memset(_, 0, _). However their five instances add at least a kilobyte
16 * to the object file. You have been warned.
18 * Not a great fan of assembler for the sake of it, but I think
19 * that these routines are at least 10 times faster than their C
20 * equivalents for large blits, and that's important to the lowest level of
21 * a graphics driver. Question is whether some scheme with the blitter
22 * would be faster. I suspect not for simple text system - not much
25 * Code is very simple, just gruesome expansion. Basic strategy is to
26 * increase data moved/cleared at each step to 16 bytes to reduce
27 * instruction per data move overhead. movem might be faster still
28 * For more than 15 bytes, we try to align the write direction on a
29 * longword boundary to get maximum speed. This is even more gruesome.
30 * Unaligned read/write used requires 68020+ - think this is a problem?
36 /* ++roman: I've optimized Robert's original versions in some minor
37 * aspects, e.g. moveq instead of movel, let gcc choose the registers,
38 * use movem in some places...
39 * For other modes than 1 plane, lots of more such assembler functions
40 * were needed (e.g. the ones using movep or expanding color values).
43 /* ++andreas: more optimizations:
44 subl #65536,d0 replaced by clrw d0; subql #1,d0 for dbcc
45 addal is faster than addaw
46 movep is rather expensive compared to ordinary move's
47 some functions rewritten in C for clarity, no speed loss */
49 static inline void *fb_memclear_small(void *s
, size_t count
)
55 " lsr.l #1,%1 ; jcc 1f ; move.b %2,-(%0)\n"
56 "1: lsr.l #1,%1 ; jcc 1f ; move.w %2,-(%0)\n"
57 "1: lsr.l #1,%1 ; jcc 1f ; move.l %2,-(%0)\n"
58 "1: lsr.l #1,%1 ; jcc 1f ; move.l %2,-(%0) ; move.l %2,-(%0)\n"
60 : "=a" (s
), "=d" (count
)
61 : "d" (0), "0" ((char *)s
+ count
), "1" (count
));
65 " move.l %2,%%d4; move.l %2,%%d5; move.l %2,%%d6\n"
66 "2: movem.l %2/%%d4/%%d5/%%d6,-(%0)\n"
69 : "=a" (s
), "=d" (count
)
70 : "d" (0), "0" (s
), "1" (count
)
78 static inline void *fb_memclear(void *s
, size_t count
)
85 " lsr.l #1,%1 ; jcc 1f ; clr.b (%0)+\n"
86 "1: lsr.l #1,%1 ; jcc 1f ; clr.w (%0)+\n"
87 "1: lsr.l #1,%1 ; jcc 1f ; clr.l (%0)+\n"
88 "1: lsr.l #1,%1 ; jcc 1f ; clr.l (%0)+ ; clr.l (%0)+\n"
90 : "=a" (s
), "=d" (count
)
91 : "0" (s
), "1" (count
));
96 " lsr.l #1,%2 ; jcc 1f ; clr.b (%0)+ ; subq.w #1,%1\n"
97 " lsr.l #1,%2 ; jcs 2f\n" /* %0 increased=>bit 2 switched*/
98 " clr.w (%0)+ ; subq.w #2,%1 ; jra 2f\n"
99 "1: lsr.l #1,%2 ; jcc 2f\n"
100 " clr.w (%0)+ ; subq.w #2,%1\n"
101 "2: move.w %1,%2; lsr.l #2,%1 ; jeq 6f\n"
102 " lsr.l #1,%1 ; jcc 3f ; clr.l (%0)+\n"
103 "3: lsr.l #1,%1 ; jcc 4f ; clr.l (%0)+ ; clr.l (%0)+\n"
104 "4: subq.l #1,%1 ; jcs 6f\n"
105 "5: clr.l (%0)+; clr.l (%0)+ ; clr.l (%0)+ ; clr.l (%0)+\n"
106 " dbra %1,5b ; clr.w %1; subq.l #1,%1; jcc 5b\n"
107 "6: move.w %2,%1; btst #1,%1 ; jeq 7f ; clr.w (%0)+\n"
108 "7: btst #0,%1 ; jeq 8f ; clr.b (%0)+\n"
110 : "=a" (s
), "=d" (count
), "=d" (tmp
)
111 : "0" (s
), "1" (count
));
118 static inline void *fb_memset255(void *s
, size_t count
)
124 " lsr.l #1,%1 ; jcc 1f ; move.b %2,-(%0)\n"
125 "1: lsr.l #1,%1 ; jcc 1f ; move.w %2,-(%0)\n"
126 "1: lsr.l #1,%1 ; jcc 1f ; move.l %2,-(%0)\n"
127 "1: lsr.l #1,%1 ; jcc 1f ; move.l %2,-(%0) ; move.l %2,-(%0)\n"
129 : "=a" (s
), "=d" (count
)
130 : "d" (-1), "0" ((char *)s
+count
), "1" (count
));
132 " subq.l #1,%1 ; jcs 3f\n"
133 " move.l %2,%%d4; move.l %2,%%d5; move.l %2,%%d6\n"
134 "2: movem.l %2/%%d4/%%d5/%%d6,-(%0)\n"
137 : "=a" (s
), "=d" (count
)
138 : "d" (-1), "0" (s
), "1" (count
)
145 static inline void *fb_memmove(void *d
, const void *s
, size_t count
)
150 " lsr.l #1,%2 ; jcc 1f ; move.b (%1)+,(%0)+\n"
151 "1: lsr.l #1,%2 ; jcc 1f ; move.w (%1)+,(%0)+\n"
152 "1: lsr.l #1,%2 ; jcc 1f ; move.l (%1)+,(%0)+\n"
153 "1: lsr.l #1,%2 ; jcc 1f ; move.l (%1)+,(%0)+ ; move.l (%1)+,(%0)+\n"
155 : "=a" (d
), "=a" (s
), "=d" (count
)
156 : "0" (d
), "1" (s
), "2" (count
));
161 " lsr.l #1,%3 ; jcc 1f ; move.b (%1)+,(%0)+ ; subqw #1,%2\n"
162 " lsr.l #1,%3 ; jcs 2f\n" /* %0 increased=>bit 2 switched*/
163 " move.w (%1)+,(%0)+ ; subqw #2,%2 ; jra 2f\n"
164 "1: lsr.l #1,%3 ; jcc 2f\n"
165 " move.w (%1)+,(%0)+ ; subqw #2,%2\n"
166 "2: move.w %2,%-; lsr.l #2,%2 ; jeq 6f\n"
167 " lsr.l #1,%2 ; jcc 3f ; move.l (%1)+,(%0)+\n"
168 "3: lsr.l #1,%2 ; jcc 4f ; move.l (%1)+,(%0)+ ; move.l (%1)+,(%0)+\n"
169 "4: subq.l #1,%2 ; jcs 6f\n"
170 "5: move.l (%1)+,(%0)+; move.l (%1)+,(%0)+\n"
171 " move.l (%1)+,(%0)+; move.l (%1)+,(%0)+\n"
172 " dbra %2,5b ; clr.w %2; subq.l #1,%2; jcc 5b\n"
173 "6: move.w %+,%2; btst #1,%2 ; jeq 7f ; move.w (%1)+,(%0)+\n"
174 "7: btst #0,%2 ; jeq 8f ; move.b (%1)+,(%0)+\n"
176 : "=a" (d
), "=a" (s
), "=d" (count
), "=d" (tmp
)
177 : "0" (d
), "1" (s
), "2" (count
));
182 " lsr.l #1,%2 ; jcc 1f ; move.b -(%1),-(%0)\n"
183 "1: lsr.l #1,%2 ; jcc 1f ; move.w -(%1),-(%0)\n"
184 "1: lsr.l #1,%2 ; jcc 1f ; move.l -(%1),-(%0)\n"
185 "1: lsr.l #1,%2 ; jcc 1f ; move.l -(%1),-(%0) ; move.l -(%1),-(%0)\n"
187 : "=a" (d
), "=a" (s
), "=d" (count
)
188 : "0" ((char *) d
+ count
), "1" ((char *) s
+ count
), "2" (count
));
194 " lsr.l #1,%3 ; jcc 1f ; move.b -(%1),-(%0) ; subqw #1,%2\n"
195 " lsr.l #1,%3 ; jcs 2f\n" /* %0 increased=>bit 2 switched*/
196 " move.w -(%1),-(%0) ; subqw #2,%2 ; jra 2f\n"
197 "1: lsr.l #1,%3 ; jcc 2f\n"
198 " move.w -(%1),-(%0) ; subqw #2,%2\n"
199 "2: move.w %2,%-; lsr.l #2,%2 ; jeq 6f\n"
200 " lsr.l #1,%2 ; jcc 3f ; move.l -(%1),-(%0)\n"
201 "3: lsr.l #1,%2 ; jcc 4f ; move.l -(%1),-(%0) ; move.l -(%1),-(%0)\n"
202 "4: subq.l #1,%2 ; jcs 6f\n"
203 "5: move.l -(%1),-(%0); move.l -(%1),-(%0)\n"
204 " move.l -(%1),-(%0); move.l -(%1),-(%0)\n"
205 " dbra %2,5b ; clr.w %2; subq.l #1,%2; jcc 5b\n"
206 "6: move.w %+,%2; btst #1,%2 ; jeq 7f ; move.w -(%1),-(%0)\n"
207 "7: btst #0,%2 ; jeq 8f ; move.b -(%1),-(%0)\n"
209 : "=a" (d
), "=a" (s
), "=d" (count
), "=d" (tmp
)
210 : "0" ((char *) d
+ count
), "1" ((char *) s
+ count
), "2" (count
));
218 /* ++andreas: Simple and fast version of memmove, assumes size is
219 divisible by 16, suitable for moving the whole screen bitplane */
220 static inline void fast_memmove(char *dst
, const char *src
, size_t size
)
226 "1: movem.l (%0)+,%%d0/%%d1/%%a0/%%a1\n"
227 " movem.l %%d0/%%d1/%%a0/%%a1,%1@\n"
228 " addq.l #8,%1; addq.l #8,%1\n"
230 " clr.w %2; subq.l #1,%2\n"
232 : "=a" (src
), "=a" (dst
), "=d" (size
)
233 : "0" (src
), "1" (dst
), "2" (size
/ 16 - 1)
234 : "d0", "d1", "a0", "a1", "memory");
237 "1: subq.l #8,%0; subq.l #8,%0\n"
238 " movem.l %0@,%%d0/%%d1/%%a0/%%a1\n"
239 " movem.l %%d0/%%d1/%%a0/%%a1,-(%1)\n"
241 " clr.w %2; subq.l #1,%2\n"
243 : "=a" (src
), "=a" (dst
), "=d" (size
)
244 : "0" (src
+ size
), "1" (dst
+ size
), "2" (size
/ 16 - 1)
245 : "d0", "d1", "a0", "a1", "memory");
251 * This expands a up to 8 bit color into two longs
252 * for movel operations.
254 static const u32 four2long
[] = {
255 0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff,
256 0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
257 0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff,
258 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff,
261 static inline void expand8_col2mask(u8 c
, u32 m
[])
263 m
[0] = four2long
[c
& 15];
265 m
[1] = four2long
[c
>> 4];
269 static inline void expand8_2col2mask(u8 fg
, u8 bg
, u32 fgm
[], u32 bgm
[])
271 fgm
[0] = four2long
[fg
& 15] ^ (bgm
[0] = four2long
[bg
& 15]);
273 fgm
[1] = four2long
[fg
>> 4] ^ (bgm
[1] = four2long
[bg
>> 4]);
278 * set an 8bit value to a color
280 static inline void fill8_col(u8
*dst
, u32 m
[])
284 dst
[2] = (tmp
>>= 8);
286 dst
[4] = (tmp
>>= 8);
292 dst
[10] = (tmp
>>= 8);
293 dst
[12] = (tmp
>>= 8);
299 * set an 8bit value according to foreground/background color
301 static inline void fill8_2col(u8
*dst
, u8 fg
, u8 bg
, u32 mask
)
303 u32 fgm
[2], bgm
[2], tmp
;
305 expand8_2col2mask(fg
, bg
, fgm
, bgm
);
311 tmp
= (mask
& fgm
[0]) ^ bgm
[0];
313 dst
[2] = (tmp
>>= 8);
315 dst
[4] = (tmp
>>= 8);
319 tmp
= (mask
& fgm
[1]) ^ bgm
[1];
321 dst
[10] = (tmp
>>= 8);
322 dst
[12] = (tmp
>>= 8);
327 static const u32 two2word
[] = {
328 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
331 static inline void expand16_col2mask(u8 c
, u32 m
[])
333 m
[0] = two2word
[c
& 3];
335 m
[1] = two2word
[(c
>> 2) & 3];
338 m
[2] = two2word
[(c
>> 4) & 3];
339 m
[3] = two2word
[c
>> 6];
343 static inline void expand16_2col2mask(u8 fg
, u8 bg
, u32 fgm
[], u32 bgm
[])
345 bgm
[0] = two2word
[bg
& 3];
346 fgm
[0] = two2word
[fg
& 3] ^ bgm
[0];
348 bgm
[1] = two2word
[(bg
>> 2) & 3];
349 fgm
[1] = two2word
[(fg
>> 2) & 3] ^ bgm
[1];
352 bgm
[2] = two2word
[(bg
>> 4) & 3];
353 fgm
[2] = two2word
[(fg
>> 4) & 3] ^ bgm
[2];
354 bgm
[3] = two2word
[bg
>> 6];
355 fgm
[3] = two2word
[fg
>> 6] ^ bgm
[3];
359 static inline u32
*fill16_col(u32
*dst
, int rows
, u32 m
[])
375 static inline void memmove32_col(void *dst
, void *src
, u32 mask
, u32 h
, u32 bytes
)
382 v
= (*s
++ & mask
) | (*d
& ~mask
);
385 v
= (*s
++ & mask
) | (*d
& ~mask
);
389 v
= (*s
++ & mask
) | (*d
& ~mask
);
391 v
= (*s
++ & mask
) | (*d
& ~mask
);
394 d
= (u32
*)((u8
*)d
+ bytes
);
395 s
= (u32
*)((u8
*)s
+ bytes
);
401 #endif /* _VIDEO_ATAFB_UTILS_H */