summaryrefslogtreecommitdiff
path: root/drivers/video/fbdev/atafb_utils.h
blob: 8f3396ea8ae5eee2dd8661f792f633536dc186bb (plain) (blame)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _VIDEO_ATAFB_UTILS_H
#define _VIDEO_ATAFB_UTILS_H

/* ================================================================= */
/*                      Utility Assembler Functions                  */
/* ================================================================= */

/* ====================================================================== */

/* Those of a delicate disposition might like to skip the next couple of
 * pages.
 *
 * These functions are drop in replacements for memmove and
 * memset(_, 0, _). However their five instances add at least a kilobyte
 * to the object file. You have been warned.
 *
 * Not a great fan of assembler for the sake of it, but I think
 * that these routines are at least 10 times faster than their C
 * equivalents for large blits, and that's important to the lowest level of
 * a graphics driver. Question is whether some scheme with the blitter
 * would be faster. I suspect not for simple text system - not much
 * asynchrony.
 *
 * Code is very simple, just gruesome expansion. Basic strategy is to
 * increase data moved/cleared at each step to 16 bytes to reduce
 * instruction per data move overhead. movem might be faster still
 * For more than 15 bytes, we try to align the write direction on a
 * longword boundary to get maximum speed. This is even more gruesome.
 * Unaligned read/write used requires 68020+ - think this is a problem?
 *
 * Sorry!
 */


/* ++roman: I've optimized Robert's original versions in some minor
 * aspects, e.g. moveq instead of movel, let gcc choose the registers,
 * use movem in some places...
 * For other modes than 1 plane, lots of more such assembler functions
 * were needed (e.g. the ones using movep or expanding color values).
 */

/* ++andreas: more optimizations:
   subl #65536,d0 replaced by clrw d0; subql #1,d0 for dbcc
   addal is faster than addaw
   movep is rather expensive compared to ordinary move's
   some functions rewritten in C for clarity, no speed loss */

static inline void *fb_memclear_small(void *s, size_t count)
{
	if (!count)
		return 0;

	asm volatile ("\n"
		"	lsr.l	#1,%1 ; jcc 1f ; move.b %2,-(%0)\n"
		"1:	lsr.l	#1,%1 ; jcc 1f ; move.w %2,-(%0)\n"
		"1:	lsr.l	#1,%1 ; jcc 1f ; move.l %2,-(%0)\n"
		"1:	lsr.l	#1,%1 ; jcc 1f ; move.l %2,-(%0) ; move.l %2,-(%0)\n"
		"1:"
		: "=a" (s), "=d" (count)
		: "d" (0), "0" ((char *)s + count), "1" (count));
	asm volatile ("\n"
		"	subq.l  #1,%1\n"
		"	jcs	3f\n"
		"	move.l	%2,%%d4; move.l %2,%%d5; move.l %2,%%d6\n"
		"2:	movem.l	%2/%%d4/%%d5/%%d6,-(%0)\n"
		"	dbra	%1,2b\n"
		"3:"
		: "=a" (s), "=d" (count)
		: "d" (0), "0" (s), "1" (count)
		: "d4", "d5", "d6"
		);

	return 0;
}


static inline void *fb_memclear(void *s, size_t count)
{
	if (!count)
		return 0;

	if (count < 16) {
		asm volatile ("\n"
			"	lsr.l	#1,%1 ; jcc 1f ; clr.b (%0)+\n"
			"1:	lsr.l	#1,%1 ; jcc 1f ; clr.w (%0)+\n"
			"1:	lsr.l	#1,%1 ; jcc 1f ; clr.l (%0)+\n"
			"1:	lsr.l	#1,%1 ; jcc 1f ; clr.l (%0)+ ; clr.l (%0)+\n"
			"1:"
			: "=a" (s), "=d" (count)
			: "0" (s), "1" (count));
	} else {
		long tmp;
		asm volatile ("\n"
			"	move.l	%1,%2\n"
			"	lsr.l	#1,%2 ; jcc 1f ; clr.b (%0)+ ; subq.w #1,%1\n"
			"	lsr.l	#1,%2 ; jcs 2f\n"  /* %0 increased=>bit 2 switched*/
			"	clr.w	(%0)+  ; subq.w  #2,%1 ; jra 2f\n"
			"1:	lsr.l	#1,%2 ; jcc 2f\n"
			"	clr.w	(%0)+  ; subq.w  #2,%1\n"
			"2:	move.w	%1,%2; lsr.l #2,%1 ; jeq 6f\n"
			"	lsr.l	#1,%1 ; jcc 3f ; clr.l (%0)+\n"
			"3:	lsr.l	#1,%1 ; jcc 4f ; clr.l (%0)+ ; clr.l (%0)+\n"
			"4:	subq.l	#1,%1 ; jcs 6f\n"
			"5:	clr.l	(%0)+; clr.l (%0)+ ; clr.l (%0)+ ; clr.l (%0)+\n"
			"	dbra	%1,5b ; clr.w %1; subq.l #1,%1; jcc 5b\n"
			"6:	move.w	%2,%1; btst #1,%1 ; jeq 7f ; clr.w (%0)+\n"
			"7:	btst	#0,%1 ; jeq 8f ; clr.b (%0)+\n"
			"8:"
			: "=a" (s), "=d" (count), "=d" (tmp)
			: "0" (s), "1" (count));
	}

	return 0;
}


static inline void *fb_memset255(void *s, size_t count)
{
	if (!count)
		return 0;

	asm volatile ("\n"
		"	lsr.l	#1,%1 ; jcc 1f ; move.b %2,-(%0)\n"
		"1:	lsr.l	#1,%1 ; jcc 1f ; move.w %2,-(%0)\n"
		"1:	lsr.l	#1,%1 ; jcc 1f ; move.l %2,-(%0)\n"
		"1:	lsr.l	#1,%1 ; jcc 1f ; move.l %2,-(%0) ; move.l %2,-(%0)\n"
		"1:"
		: "=a" (s), "=d" (count)
		: "d" (-1), "0" ((char *)s+count), "1" (count));
	asm volatile ("\n"
		"	subq.l	#1,%1 ; jcs 3f\n"
		"	move.l	%2,%%d4; move.l %2,%%d5; move.l %2,%%d6\n"
		"2:	movem.l	%2/%%d4/%%d5/%%d6,-(%0)\n"
		"	dbra	%1,2b\n"
		"3:"
		: "=a" (s), "=d" (count)
		: "d" (-1), "0" (s), "1" (count)
		: "d4", "d5", "d6");

	return 0;
}


static inline void *fb_memmove(void *d, const void *s, size_t count)
{
	if (d < s) {
		if (count < 16) {
			asm volatile ("\n"
				"	lsr.l	#1,%2 ; jcc 1f ; move.b (%1)+,(%0)+\n"
				"1:	lsr.l	#1,%2 ; jcc 1f ; move.w (%1)+,(%0)+\n"
				"1:	lsr.l	#1,%2 ; jcc 1f ; move.l (%1)+,(%0)+\n"
				"1:	lsr.l	#1,%2 ; jcc 1f ; move.l (%1)+,(%0)+ ; move.l (%1)+,(%0)+\n"
				"1:"
				: "=a" (d), "=a" (s), "=d" (count)
				: "0" (d), "1" (s), "2" (count));
		} else {
			long tmp;
			asm volatile ("\n"
				"	move.l	%0,%3\n"
				"	lsr.l	#1,%3 ; jcc 1f ; move.b (%1)+,(%0)+ ; subqw #1,%2\n"
				"	lsr.l	#1,%3 ; jcs 2f\n"  /* %0 increased=>bit 2 switched*/
				"	move.w	(%1)+,(%0)+  ; subqw  #2,%2 ; jra 2f\n"
				"1:	lsr.l   #1,%3 ; jcc 2f\n"
				"	move.w	(%1)+,(%0)+  ; subqw  #2,%2\n"
				"2:	move.w	%2,%-; lsr.l #2,%2 ; jeq 6f\n"
				"	lsr.l	#1,%2 ; jcc 3f ; move.l (%1)+,(%0)+\n"
				"3:	lsr.l	#1,%2 ; jcc 4f ; move.l (%1)+,(%0)+ ; move.l (%1)+,(%0)+\n"
				"4:	subq.l	#1,%2 ; jcs 6f\n"
				"5:	move.l	(%1)+,(%0)+; move.l (%1)+,(%0)+\n"
				"	move.l	(%1)+,(%0)+; move.l (%1)+,(%0)+\n"
				"	dbra	%2,5b ; clr.w %2; subq.l #1,%2; jcc 5b\n"
				"6:	move.w	%+,%2; btst #1,%2 ; jeq 7f ; move.w (%1)+,(%0)+\n"
				"7:	btst	#0,%2 ; jeq 8f ; move.b (%1)+,(%0)+\n"
				"8:"
				: "=a" (d), "=a" (s), "=d" (count), "=d" (tmp)
				: "0" (d), "1" (s), "2" (count));
		}
	} else {
		if (count < 16) {
			asm volatile ("\n"
				"	lsr.l	#1,%2 ; jcc 1f ; move.b -(%1),-(%0)\n"
				"1:	lsr.l	#1,%2 ; jcc 1f ; move.w -(%1),-(%0)\n"
				"1:	lsr.l	#1,%2 ; jcc 1f ; move.l -(%1),-(%0)\n"
				"1:	lsr.l	#1,%2 ; jcc 1f ; move.l -(%1),-(%0) ; move.l -(%1),-(%0)\n"
				"1:"
				: "=a" (d), "=a" (s), "=d" (count)
				: "0" ((char *) d + count), "1" ((char *) s + count), "2" (count));
		} else {
			long tmp;

			asm volatile ("\n"
				"	move.l	%0,%3\n"
				"	lsr.l	#1,%3 ; jcc 1f ; move.b -(%1),-(%0) ; subqw #1,%2\n"
				"	lsr.l	#1,%3 ; jcs 2f\n"  /* %0 increased=>bit 2 switched*/
				"	move.w	-(%1),-(%0) ; subqw  #2,%2 ; jra 2f\n"
				"1:	lsr.l	#1,%3 ; jcc 2f\n"
				"	move.w	-(%1),-(%0) ; subqw  #2,%2\n"
				"2:	move.w	%2,%-; lsr.l #2,%2 ; jeq 6f\n"
				"	lsr.l	#1,%2 ; jcc 3f ; move.l -(%1),-(%0)\n"
				"3:	lsr.l	#1,%2 ; jcc 4f ; move.l -(%1),-(%0) ; move.l -(%1),-(%0)\n"
				"4:	subq.l	#1,%2 ; jcs 6f\n"
				"5:	move.l	-(%1),-(%0); move.l -(%1),-(%0)\n"
				"	move.l	-(%1),-(%0); move.l -(%1),-(%0)\n"
				"	dbra	%2,5b ; clr.w %2; subq.l #1,%2; jcc 5b\n"
				"6:	move.w	%+,%2; btst #1,%2 ; jeq 7f ; move.w -(%1),-(%0)\n"
				"7:	btst	#0,%2 ; jeq 8f ; move.b -(%1),-(%0)\n"
				"8:"
				: "=a" (d), "=a" (s), "=d" (count), "=d" (tmp)
				: "0" ((char *) d + count), "1" ((char *) s + count), "2" (count));
		}
	}

	return 0;
}


/* ++andreas: Simple and fast version of memmove, assumes size is
   divisible by 16, suitable for moving the whole screen bitplane */
static inline void fast_memmove(char *dst, const char *src, size_t size)
{
	if (!size)
		return;
	if (dst < src)
		asm volatile ("\n"
			"1:	movem.l	(%0)+,%%d0/%%d1/%%a0/%%a1\n"
			"	movem.l	%%d0/%%d1/%%a0/%%a1,%1@\n"
			"	addq.l	#8,%1; addq.l #8,%1\n"
			"	dbra	%2,1b\n"
			"	clr.w	%2; subq.l #1,%2\n"
			"	jcc	1b"
			: "=a" (src), "=a" (dst), "=d" (size)
			: "0" (src), "1" (dst), "2" (size / 16 - 1)
			: "d0", "d1", "a0", "a1", "memory");
	else
		asm volatile ("\n"
			"1:	subq.l	#8,%0; subq.l #8,%0\n"
			"	movem.l	%0@,%%d0/%%d1/%%a0/%%a1\n"
			"	movem.l	%%d0/%%d1/%%a0/%%a1,-(%1)\n"
			"	dbra	%2,1b\n"
			"	clr.w	%2; subq.l #1,%2\n"
			"	jcc 1b"
			: "=a" (src), "=a" (dst), "=d" (size)
			: "0" (src + size), "1" (dst + size), "2" (size / 16 - 1)
			: "d0", "d1", "a0", "a1", "memory");
}

#ifdef BPL

/*
 * This expands a up to 8 bit color into two longs
 * for movel operations.
 */
static const u32 four2long[] = {
	0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff,
	0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
	0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff,
	0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff,
};

static inline void expand8_col2mask(u8 c, u32 m[])
{
	m[0] = four2long[c & 15];
#if BPL > 4
	m[1] = four2long[c >> 4];
#endif
}

static inline void expand8_2col2mask(u8 fg, u8 bg, u32 fgm[], u32 bgm[])
{
	fgm[0] = four2long[fg & 15] ^ (bgm[0] = four2long[bg & 15]);
#if BPL > 4
	fgm[1] = four2long[fg >> 4] ^ (bgm[1] = four2long[bg >> 4]);
#endif
}

/*
 * set an 8bit value to a color
 */
static inline void fill8_col(u8 *dst, u32 m[])
{
	u32 tmp = m[0];
	dst[0] = tmp;
	dst[2] = (tmp >>= 8);
#if BPL > 2
	dst[4] = (tmp >>= 8);
	dst[6] = tmp >> 8;
#endif
#if BPL > 4
	tmp = m[1];
	dst[8] = tmp;
	dst[10] = (tmp >>= 8);
	dst[12] = (tmp >>= 8);
	dst[14] = tmp >> 8;
#endif
}

/*
 * set an 8bit value according to foreground/background color
 */
static inline void fill8_2col(u8 *dst, u8 fg, u8 bg, u32 mask)
{
	u32 fgm[2], bgm[2], tmp;

	expand8_2col2mask(fg, bg, fgm, bgm);

	mask |= mask << 8;
#if BPL > 2
	mask |= mask << 16;
#endif
	tmp = (mask & fgm[0]) ^ bgm[0];
	dst[0] = tmp;
	dst[2] = (tmp >>= 8);
#if BPL > 2
	dst[4] = (tmp >>= 8);
	dst[6] = tmp >> 8;
#endif
#if BPL > 4
	tmp = (mask & fgm[1]) ^ bgm[1];
	dst[8] = tmp;
	dst[10] = (tmp >>= 8);
	dst[12] = (tmp >>= 8);
	dst[14] = tmp >> 8;
#endif
}

static const u32 two2word[] = {
	0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
};

static inline void expand16_col2mask(u8 c, u32 m[])
{
	m[0] = two2word[c & 3];
#if BPL > 2
	m[1] = two2word[(c >> 2) & 3];
#endif
#if BPL > 4
	m[2] = two2word[(c >> 4) & 3];
	m[3] = two2word[c >> 6];
#endif
}

static inline void expand16_2col2mask(u8 fg, u8 bg, u32 fgm[], u32 bgm[])
{
	bgm[0] = two2word[bg & 3];
	fgm[0] = two2word[fg & 3] ^ bgm[0];
#if BPL > 2
	bgm[1] = two2word[(bg >> 2) & 3];
	fgm[1] = two2word[(fg >> 2) & 3] ^ bgm[1];
#endif
#if BPL > 4
	bgm[2] = two2word[(bg >> 4) & 3];
	fgm[2] = two2word[(fg >> 4) & 3] ^ bgm[2];
	bgm[3] = two2word[bg >> 6];
	fgm[3] = two2word[fg >> 6] ^ bgm[3];
#endif
}

static inline u32 *fill16_col(u32 *dst, int rows, u32 m[])
{
	while (rows) {
		*dst++ = m[0];
#if BPL > 2
		*dst++ = m[1];
#endif
#if BPL > 4
		*dst++ = m[2];
		*dst++ = m[3];
#endif
		rows--;
	}
	return dst;
}

static inline void memmove32_col(void *dst, void *src, u32 mask, u32 h, u32 bytes)
{
	u32 *s, *d, v;

        s = src;
        d = dst;
        do {
                v = (*s++ & mask) | (*d  & ~mask);
                *d++ = v;
#if BPL > 2
                v = (*s++ & mask) | (*d  & ~mask);
                *d++ = v;
#endif
#if BPL > 4
                v = (*s++ & mask) | (*d  & ~mask);
                *d++ = v;
                v = (*s++ & mask) | (*d  & ~mask);
                *d++ = v;
#endif
                d = (u32 *)((u8 *)d + bytes);
                s = (u32 *)((u8 *)s + bytes);
        } while (--h);
}

#endif

#endif /* _VIDEO_ATAFB_UTILS_H */