atafb_utils.h 11 KB

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