atafb_utils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _VIDEO_ATAFB_UTILS_H
  3. #define _VIDEO_ATAFB_UTILS_H
  4. /* ================================================================= */
  5. /* Utility Assembler Functions */
  6. /* ================================================================= */
  7. /* ====================================================================== */
  8. /* Those of a delicate disposition might like to skip the next couple of
  9. * pages.
  10. *
  11. * These functions are drop in replacements for memmove and
  12. * memset(_, 0, _). However their five instances add at least a kilobyte
  13. * to the object file. You have been warned.
  14. *
  15. * Not a great fan of assembler for the sake of it, but I think
  16. * that these routines are at least 10 times faster than their C
  17. * equivalents for large blits, and that's important to the lowest level of
  18. * a graphics driver. Question is whether some scheme with the blitter
  19. * would be faster. I suspect not for simple text system - not much
  20. * asynchrony.
  21. *
  22. * Code is very simple, just gruesome expansion. Basic strategy is to
  23. * increase data moved/cleared at each step to 16 bytes to reduce
  24. * instruction per data move overhead. movem might be faster still
  25. * For more than 15 bytes, we try to align the write direction on a
  26. * longword boundary to get maximum speed. This is even more gruesome.
  27. * Unaligned read/write used requires 68020+ - think this is a problem?
  28. *
  29. * Sorry!
  30. */
  31. /* ++roman: I've optimized Robert's original versions in some minor
  32. * aspects, e.g. moveq instead of movel, let gcc choose the registers,
  33. * use movem in some places...
  34. * For other modes than 1 plane, lots of more such assembler functions
  35. * were needed (e.g. the ones using movep or expanding color values).
  36. */
  37. /* ++andreas: more optimizations:
  38. subl #65536,d0 replaced by clrw d0; subql #1,d0 for dbcc
  39. addal is faster than addaw
  40. movep is rather expensive compared to ordinary move's
  41. some functions rewritten in C for clarity, no speed loss */
  42. static inline void *fb_memclear_small(void *s, size_t count)
  43. {
  44. if (!count)
  45. return 0;
  46. asm volatile ("\n"
  47. " lsr.l #1,%1 ; jcc 1f ; move.b %2,-(%0)\n"
  48. "1: lsr.l #1,%1 ; jcc 1f ; move.w %2,-(%0)\n"
  49. "1: lsr.l #1,%1 ; jcc 1f ; move.l %2,-(%0)\n"
  50. "1: lsr.l #1,%1 ; jcc 1f ; move.l %2,-(%0) ; move.l %2,-(%0)\n"
  51. "1:"
  52. : "=a" (s), "=d" (count)
  53. : "d" (0), "0" ((char *)s + count), "1" (count));
  54. asm volatile ("\n"
  55. " subq.l #1,%1\n"
  56. " jcs 3f\n"
  57. " move.l %2,%%d4; move.l %2,%%d5; move.l %2,%%d6\n"
  58. "2: movem.l %2/%%d4/%%d5/%%d6,-(%0)\n"
  59. " dbra %1,2b\n"
  60. "3:"
  61. : "=a" (s), "=d" (count)
  62. : "d" (0), "0" (s), "1" (count)
  63. : "d4", "d5", "d6"
  64. );
  65. return 0;
  66. }
  67. static inline void *fb_memclear(void *s, size_t count)
  68. {
  69. if (!count)
  70. return 0;
  71. if (count < 16) {
  72. asm volatile ("\n"
  73. " lsr.l #1,%1 ; jcc 1f ; clr.b (%0)+\n"
  74. "1: lsr.l #1,%1 ; jcc 1f ; clr.w (%0)+\n"
  75. "1: lsr.l #1,%1 ; jcc 1f ; clr.l (%0)+\n"
  76. "1: lsr.l #1,%1 ; jcc 1f ; clr.l (%0)+ ; clr.l (%0)+\n"
  77. "1:"
  78. : "=a" (s), "=d" (count)
  79. : "0" (s), "1" (count));
  80. } else {
  81. long tmp;
  82. asm volatile ("\n"
  83. " move.l %1,%2\n"
  84. " lsr.l #1,%2 ; jcc 1f ; clr.b (%0)+ ; subq.w #1,%1\n"
  85. " lsr.l #1,%2 ; jcs 2f\n" /* %0 increased=>bit 2 switched*/
  86. " clr.w (%0)+ ; subq.w #2,%1 ; jra 2f\n"
  87. "1: lsr.l #1,%2 ; jcc 2f\n"
  88. " clr.w (%0)+ ; subq.w #2,%1\n"
  89. "2: move.w %1,%2; lsr.l #2,%1 ; jeq 6f\n"
  90. " lsr.l #1,%1 ; jcc 3f ; clr.l (%0)+\n"
  91. "3: lsr.l #1,%1 ; jcc 4f ; clr.l (%0)+ ; clr.l (%0)+\n"
  92. "4: subq.l #1,%1 ; jcs 6f\n"
  93. "5: clr.l (%0)+; clr.l (%0)+ ; clr.l (%0)+ ; clr.l (%0)+\n"
  94. " dbra %1,5b ; clr.w %1; subq.l #1,%1; jcc 5b\n"
  95. "6: move.w %2,%1; btst #1,%1 ; jeq 7f ; clr.w (%0)+\n"
  96. "7: btst #0,%1 ; jeq 8f ; clr.b (%0)+\n"
  97. "8:"
  98. : "=a" (s), "=d" (count), "=d" (tmp)
  99. : "0" (s), "1" (count));
  100. }
  101. return 0;
  102. }
  103. static inline void *fb_memset255(void *s, size_t count)
  104. {
  105. if (!count)
  106. return 0;
  107. asm volatile ("\n"
  108. " lsr.l #1,%1 ; jcc 1f ; move.b %2,-(%0)\n"
  109. "1: lsr.l #1,%1 ; jcc 1f ; move.w %2,-(%0)\n"
  110. "1: lsr.l #1,%1 ; jcc 1f ; move.l %2,-(%0)\n"
  111. "1: lsr.l #1,%1 ; jcc 1f ; move.l %2,-(%0) ; move.l %2,-(%0)\n"
  112. "1:"
  113. : "=a" (s), "=d" (count)
  114. : "d" (-1), "0" ((char *)s+count), "1" (count));
  115. asm volatile ("\n"
  116. " subq.l #1,%1 ; jcs 3f\n"
  117. " move.l %2,%%d4; move.l %2,%%d5; move.l %2,%%d6\n"
  118. "2: movem.l %2/%%d4/%%d5/%%d6,-(%0)\n"
  119. " dbra %1,2b\n"
  120. "3:"
  121. : "=a" (s), "=d" (count)
  122. : "d" (-1), "0" (s), "1" (count)
  123. : "d4", "d5", "d6");
  124. return 0;
  125. }
  126. static inline void *fb_memmove(void *d, const void *s, size_t count)
  127. {
  128. if (d < s) {
  129. if (count < 16) {
  130. asm volatile ("\n"
  131. " lsr.l #1,%2 ; jcc 1f ; move.b (%1)+,(%0)+\n"
  132. "1: lsr.l #1,%2 ; jcc 1f ; move.w (%1)+,(%0)+\n"
  133. "1: lsr.l #1,%2 ; jcc 1f ; move.l (%1)+,(%0)+\n"
  134. "1: lsr.l #1,%2 ; jcc 1f ; move.l (%1)+,(%0)+ ; move.l (%1)+,(%0)+\n"
  135. "1:"
  136. : "=a" (d), "=a" (s), "=d" (count)
  137. : "0" (d), "1" (s), "2" (count));
  138. } else {
  139. long tmp;
  140. asm volatile ("\n"
  141. " move.l %0,%3\n"
  142. " lsr.l #1,%3 ; jcc 1f ; move.b (%1)+,(%0)+ ; subqw #1,%2\n"
  143. " lsr.l #1,%3 ; jcs 2f\n" /* %0 increased=>bit 2 switched*/
  144. " move.w (%1)+,(%0)+ ; subqw #2,%2 ; jra 2f\n"
  145. "1: lsr.l #1,%3 ; jcc 2f\n"
  146. " move.w (%1)+,(%0)+ ; subqw #2,%2\n"
  147. "2: move.w %2,%-; lsr.l #2,%2 ; jeq 6f\n"
  148. " lsr.l #1,%2 ; jcc 3f ; move.l (%1)+,(%0)+\n"
  149. "3: lsr.l #1,%2 ; jcc 4f ; move.l (%1)+,(%0)+ ; move.l (%1)+,(%0)+\n"
  150. "4: subq.l #1,%2 ; jcs 6f\n"
  151. "5: move.l (%1)+,(%0)+; move.l (%1)+,(%0)+\n"
  152. " move.l (%1)+,(%0)+; move.l (%1)+,(%0)+\n"
  153. " dbra %2,5b ; clr.w %2; subq.l #1,%2; jcc 5b\n"
  154. "6: move.w %+,%2; btst #1,%2 ; jeq 7f ; move.w (%1)+,(%0)+\n"
  155. "7: btst #0,%2 ; jeq 8f ; move.b (%1)+,(%0)+\n"
  156. "8:"
  157. : "=a" (d), "=a" (s), "=d" (count), "=d" (tmp)
  158. : "0" (d), "1" (s), "2" (count));
  159. }
  160. } else {
  161. if (count < 16) {
  162. asm volatile ("\n"
  163. " lsr.l #1,%2 ; jcc 1f ; move.b -(%1),-(%0)\n"
  164. "1: lsr.l #1,%2 ; jcc 1f ; move.w -(%1),-(%0)\n"
  165. "1: lsr.l #1,%2 ; jcc 1f ; move.l -(%1),-(%0)\n"
  166. "1: lsr.l #1,%2 ; jcc 1f ; move.l -(%1),-(%0) ; move.l -(%1),-(%0)\n"
  167. "1:"
  168. : "=a" (d), "=a" (s), "=d" (count)
  169. : "0" ((char *) d + count), "1" ((char *) s + count), "2" (count));
  170. } else {
  171. long tmp;
  172. asm volatile ("\n"
  173. " move.l %0,%3\n"
  174. " lsr.l #1,%3 ; jcc 1f ; move.b -(%1),-(%0) ; subqw #1,%2\n"
  175. " lsr.l #1,%3 ; jcs 2f\n" /* %0 increased=>bit 2 switched*/
  176. " move.w -(%1),-(%0) ; subqw #2,%2 ; jra 2f\n"
  177. "1: lsr.l #1,%3 ; jcc 2f\n"
  178. " move.w -(%1),-(%0) ; subqw #2,%2\n"
  179. "2: move.w %2,%-; lsr.l #2,%2 ; jeq 6f\n"
  180. " lsr.l #1,%2 ; jcc 3f ; move.l -(%1),-(%0)\n"
  181. "3: lsr.l #1,%2 ; jcc 4f ; move.l -(%1),-(%0) ; move.l -(%1),-(%0)\n"
  182. "4: subq.l #1,%2 ; jcs 6f\n"
  183. "5: move.l -(%1),-(%0); move.l -(%1),-(%0)\n"
  184. " move.l -(%1),-(%0); move.l -(%1),-(%0)\n"
  185. " dbra %2,5b ; clr.w %2; subq.l #1,%2; jcc 5b\n"
  186. "6: move.w %+,%2; btst #1,%2 ; jeq 7f ; move.w -(%1),-(%0)\n"
  187. "7: btst #0,%2 ; jeq 8f ; move.b -(%1),-(%0)\n"
  188. "8:"
  189. : "=a" (d), "=a" (s), "=d" (count), "=d" (tmp)
  190. : "0" ((char *) d + count), "1" ((char *) s + count), "2" (count));
  191. }
  192. }
  193. return 0;
  194. }
  195. /* ++andreas: Simple and fast version of memmove, assumes size is
  196. divisible by 16, suitable for moving the whole screen bitplane */
  197. static inline void fast_memmove(char *dst, const char *src, size_t size)
  198. {
  199. if (!size)
  200. return;
  201. if (dst < src)
  202. asm volatile ("\n"
  203. "1: movem.l (%0)+,%%d0/%%d1/%%a0/%%a1\n"
  204. " movem.l %%d0/%%d1/%%a0/%%a1,%1@\n"
  205. " addq.l #8,%1; addq.l #8,%1\n"
  206. " dbra %2,1b\n"
  207. " clr.w %2; subq.l #1,%2\n"
  208. " jcc 1b"
  209. : "=a" (src), "=a" (dst), "=d" (size)
  210. : "0" (src), "1" (dst), "2" (size / 16 - 1)
  211. : "d0", "d1", "a0", "a1", "memory");
  212. else
  213. asm volatile ("\n"
  214. "1: subq.l #8,%0; subq.l #8,%0\n"
  215. " movem.l %0@,%%d0/%%d1/%%a0/%%a1\n"
  216. " movem.l %%d0/%%d1/%%a0/%%a1,-(%1)\n"
  217. " dbra %2,1b\n"
  218. " clr.w %2; subq.l #1,%2\n"
  219. " jcc 1b"
  220. : "=a" (src), "=a" (dst), "=d" (size)
  221. : "0" (src + size), "1" (dst + size), "2" (size / 16 - 1)
  222. : "d0", "d1", "a0", "a1", "memory");
  223. }
  224. #ifdef BPL
  225. /*
  226. * This expands a up to 8 bit color into two longs
  227. * for movel operations.
  228. */
  229. static const u32 four2long[] = {
  230. 0x00000000, 0x000000ff, 0x0000ff00, 0x0000ffff,
  231. 0x00ff0000, 0x00ff00ff, 0x00ffff00, 0x00ffffff,
  232. 0xff000000, 0xff0000ff, 0xff00ff00, 0xff00ffff,
  233. 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff,
  234. };
  235. static inline void expand8_col2mask(u8 c, u32 m[])
  236. {
  237. m[0] = four2long[c & 15];
  238. #if BPL > 4
  239. m[1] = four2long[c >> 4];
  240. #endif
  241. }
  242. static inline void expand8_2col2mask(u8 fg, u8 bg, u32 fgm[], u32 bgm[])
  243. {
  244. fgm[0] = four2long[fg & 15] ^ (bgm[0] = four2long[bg & 15]);
  245. #if BPL > 4
  246. fgm[1] = four2long[fg >> 4] ^ (bgm[1] = four2long[bg >> 4]);
  247. #endif
  248. }
  249. /*
  250. * set an 8bit value to a color
  251. */
  252. static inline void fill8_col(u8 *dst, u32 m[])
  253. {
  254. u32 tmp = m[0];
  255. dst[0] = tmp;
  256. dst[2] = (tmp >>= 8);
  257. #if BPL > 2
  258. dst[4] = (tmp >>= 8);
  259. dst[6] = tmp >> 8;
  260. #endif
  261. #if BPL > 4
  262. tmp = m[1];
  263. dst[8] = tmp;
  264. dst[10] = (tmp >>= 8);
  265. dst[12] = (tmp >>= 8);
  266. dst[14] = tmp >> 8;
  267. #endif
  268. }
  269. /*
  270. * set an 8bit value according to foreground/background color
  271. */
  272. static inline void fill8_2col(u8 *dst, u8 fg, u8 bg, u32 mask)
  273. {
  274. u32 fgm[2], bgm[2], tmp;
  275. expand8_2col2mask(fg, bg, fgm, bgm);
  276. mask |= mask << 8;
  277. #if BPL > 2
  278. mask |= mask << 16;
  279. #endif
  280. tmp = (mask & fgm[0]) ^ bgm[0];
  281. dst[0] = tmp;
  282. dst[2] = (tmp >>= 8);
  283. #if BPL > 2
  284. dst[4] = (tmp >>= 8);
  285. dst[6] = tmp >> 8;
  286. #endif
  287. #if BPL > 4
  288. tmp = (mask & fgm[1]) ^ bgm[1];
  289. dst[8] = tmp;
  290. dst[10] = (tmp >>= 8);
  291. dst[12] = (tmp >>= 8);
  292. dst[14] = tmp >> 8;
  293. #endif
  294. }
  295. static const u32 two2word[] = {
  296. 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
  297. };
  298. static inline void expand16_col2mask(u8 c, u32 m[])
  299. {
  300. m[0] = two2word[c & 3];
  301. #if BPL > 2
  302. m[1] = two2word[(c >> 2) & 3];
  303. #endif
  304. #if BPL > 4
  305. m[2] = two2word[(c >> 4) & 3];
  306. m[3] = two2word[c >> 6];
  307. #endif
  308. }
  309. static inline void expand16_2col2mask(u8 fg, u8 bg, u32 fgm[], u32 bgm[])
  310. {
  311. bgm[0] = two2word[bg & 3];
  312. fgm[0] = two2word[fg & 3] ^ bgm[0];
  313. #if BPL > 2
  314. bgm[1] = two2word[(bg >> 2) & 3];
  315. fgm[1] = two2word[(fg >> 2) & 3] ^ bgm[1];
  316. #endif
  317. #if BPL > 4
  318. bgm[2] = two2word[(bg >> 4) & 3];
  319. fgm[2] = two2word[(fg >> 4) & 3] ^ bgm[2];
  320. bgm[3] = two2word[bg >> 6];
  321. fgm[3] = two2word[fg >> 6] ^ bgm[3];
  322. #endif
  323. }
  324. static inline u32 *fill16_col(u32 *dst, int rows, u32 m[])
  325. {
  326. while (rows) {
  327. *dst++ = m[0];
  328. #if BPL > 2
  329. *dst++ = m[1];
  330. #endif
  331. #if BPL > 4
  332. *dst++ = m[2];
  333. *dst++ = m[3];
  334. #endif
  335. rows--;
  336. }
  337. return dst;
  338. }
  339. static inline void memmove32_col(void *dst, void *src, u32 mask, u32 h, u32 bytes)
  340. {
  341. u32 *s, *d, v;
  342. s = src;
  343. d = dst;
  344. do {
  345. v = (*s++ & mask) | (*d & ~mask);
  346. *d++ = v;
  347. #if BPL > 2
  348. v = (*s++ & mask) | (*d & ~mask);
  349. *d++ = v;
  350. #endif
  351. #if BPL > 4
  352. v = (*s++ & mask) | (*d & ~mask);
  353. *d++ = v;
  354. v = (*s++ & mask) | (*d & ~mask);
  355. *d++ = v;
  356. #endif
  357. d = (u32 *)((u8 *)d + bytes);
  358. s = (u32 *)((u8 *)s + bytes);
  359. } while (--h);
  360. }
  361. #endif
  362. #endif /* _VIDEO_ATAFB_UTILS_H */