SDL_blit_A.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. #if SDL_HAVE_BLIT_A
  20. #include "SDL_video.h"
  21. #include "SDL_blit.h"
  22. /* Functions to perform alpha blended blitting */
  23. /* N->1 blending with per-surface alpha */
  24. static void
  25. BlitNto1SurfaceAlpha(SDL_BlitInfo * info)
  26. {
  27. int width = info->dst_w;
  28. int height = info->dst_h;
  29. Uint8 *src = info->src;
  30. int srcskip = info->src_skip;
  31. Uint8 *dst = info->dst;
  32. int dstskip = info->dst_skip;
  33. Uint8 *palmap = info->table;
  34. SDL_PixelFormat *srcfmt = info->src_fmt;
  35. SDL_PixelFormat *dstfmt = info->dst_fmt;
  36. int srcbpp = srcfmt->BytesPerPixel;
  37. Uint32 Pixel;
  38. unsigned sR, sG, sB;
  39. unsigned dR, dG, dB;
  40. const unsigned A = info->a;
  41. while (height--) {
  42. /* *INDENT-OFF* */ /* clang-format off */
  43. DUFFS_LOOP4(
  44. {
  45. DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
  46. dR = dstfmt->palette->colors[*dst].r;
  47. dG = dstfmt->palette->colors[*dst].g;
  48. dB = dstfmt->palette->colors[*dst].b;
  49. ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB);
  50. dR &= 0xff;
  51. dG &= 0xff;
  52. dB &= 0xff;
  53. /* Pack RGB into 8bit pixel */
  54. if ( palmap == NULL ) {
  55. *dst =((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0));
  56. } else {
  57. *dst = palmap[((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0))];
  58. }
  59. dst++;
  60. src += srcbpp;
  61. },
  62. width);
  63. /* *INDENT-ON* */ /* clang-format on */
  64. src += srcskip;
  65. dst += dstskip;
  66. }
  67. }
  68. /* N->1 blending with pixel alpha */
  69. static void
  70. BlitNto1PixelAlpha(SDL_BlitInfo * info)
  71. {
  72. int width = info->dst_w;
  73. int height = info->dst_h;
  74. Uint8 *src = info->src;
  75. int srcskip = info->src_skip;
  76. Uint8 *dst = info->dst;
  77. int dstskip = info->dst_skip;
  78. Uint8 *palmap = info->table;
  79. SDL_PixelFormat *srcfmt = info->src_fmt;
  80. SDL_PixelFormat *dstfmt = info->dst_fmt;
  81. int srcbpp = srcfmt->BytesPerPixel;
  82. Uint32 Pixel;
  83. unsigned sR, sG, sB, sA;
  84. unsigned dR, dG, dB;
  85. while (height--) {
  86. /* *INDENT-OFF* */ /* clang-format off */
  87. DUFFS_LOOP4(
  88. {
  89. DISEMBLE_RGBA(src,srcbpp,srcfmt,Pixel,sR,sG,sB,sA);
  90. dR = dstfmt->palette->colors[*dst].r;
  91. dG = dstfmt->palette->colors[*dst].g;
  92. dB = dstfmt->palette->colors[*dst].b;
  93. ALPHA_BLEND_RGB(sR, sG, sB, sA, dR, dG, dB);
  94. dR &= 0xff;
  95. dG &= 0xff;
  96. dB &= 0xff;
  97. /* Pack RGB into 8bit pixel */
  98. if ( palmap == NULL ) {
  99. *dst =((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0));
  100. } else {
  101. *dst = palmap[((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0))];
  102. }
  103. dst++;
  104. src += srcbpp;
  105. },
  106. width);
  107. /* *INDENT-ON* */ /* clang-format on */
  108. src += srcskip;
  109. dst += dstskip;
  110. }
  111. }
  112. /* colorkeyed N->1 blending with per-surface alpha */
  113. static void
  114. BlitNto1SurfaceAlphaKey(SDL_BlitInfo * info)
  115. {
  116. int width = info->dst_w;
  117. int height = info->dst_h;
  118. Uint8 *src = info->src;
  119. int srcskip = info->src_skip;
  120. Uint8 *dst = info->dst;
  121. int dstskip = info->dst_skip;
  122. Uint8 *palmap = info->table;
  123. SDL_PixelFormat *srcfmt = info->src_fmt;
  124. SDL_PixelFormat *dstfmt = info->dst_fmt;
  125. int srcbpp = srcfmt->BytesPerPixel;
  126. Uint32 ckey = info->colorkey;
  127. Uint32 Pixel;
  128. unsigned sR, sG, sB;
  129. unsigned dR, dG, dB;
  130. const unsigned A = info->a;
  131. while (height--) {
  132. /* *INDENT-OFF* */ /* clang-format off */
  133. DUFFS_LOOP(
  134. {
  135. DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
  136. if ( Pixel != ckey ) {
  137. dR = dstfmt->palette->colors[*dst].r;
  138. dG = dstfmt->palette->colors[*dst].g;
  139. dB = dstfmt->palette->colors[*dst].b;
  140. ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB);
  141. dR &= 0xff;
  142. dG &= 0xff;
  143. dB &= 0xff;
  144. /* Pack RGB into 8bit pixel */
  145. if ( palmap == NULL ) {
  146. *dst =((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0));
  147. } else {
  148. *dst = palmap[((dR>>5)<<(3+2))|((dG>>5)<<(2))|((dB>>6)<<(0))];
  149. }
  150. }
  151. dst++;
  152. src += srcbpp;
  153. },
  154. width);
  155. /* *INDENT-ON* */ /* clang-format on */
  156. src += srcskip;
  157. dst += dstskip;
  158. }
  159. }
  160. #ifdef __MMX__
  161. /* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
  162. static void
  163. BlitRGBtoRGBSurfaceAlpha128MMX(SDL_BlitInfo * info)
  164. {
  165. int width = info->dst_w;
  166. int height = info->dst_h;
  167. Uint32 *srcp = (Uint32 *) info->src;
  168. int srcskip = info->src_skip >> 2;
  169. Uint32 *dstp = (Uint32 *) info->dst;
  170. int dstskip = info->dst_skip >> 2;
  171. Uint32 dalpha = info->dst_fmt->Amask;
  172. __m64 src1, src2, dst1, dst2, lmask, hmask, dsta;
  173. hmask = _mm_set_pi32(0x00fefefe, 0x00fefefe); /* alpha128 mask -> hmask */
  174. lmask = _mm_set_pi32(0x00010101, 0x00010101); /* !alpha128 mask -> lmask */
  175. dsta = _mm_set_pi32(dalpha, dalpha); /* dst alpha mask -> dsta */
  176. while (height--) {
  177. int n = width;
  178. if (n & 1) {
  179. Uint32 s = *srcp++;
  180. Uint32 d = *dstp;
  181. *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
  182. + (s & d & 0x00010101)) | dalpha;
  183. n--;
  184. }
  185. for (n >>= 1; n > 0; --n) {
  186. dst1 = *(__m64 *) dstp; /* 2 x dst -> dst1(ARGBARGB) */
  187. dst2 = dst1; /* 2 x dst -> dst2(ARGBARGB) */
  188. src1 = *(__m64 *) srcp; /* 2 x src -> src1(ARGBARGB) */
  189. src2 = src1; /* 2 x src -> src2(ARGBARGB) */
  190. dst2 = _mm_and_si64(dst2, hmask); /* dst & mask -> dst2 */
  191. src2 = _mm_and_si64(src2, hmask); /* src & mask -> src2 */
  192. src2 = _mm_add_pi32(src2, dst2); /* dst2 + src2 -> src2 */
  193. src2 = _mm_srli_pi32(src2, 1); /* src2 >> 1 -> src2 */
  194. dst1 = _mm_and_si64(dst1, src1); /* src & dst -> dst1 */
  195. dst1 = _mm_and_si64(dst1, lmask); /* dst1 & !mask -> dst1 */
  196. dst1 = _mm_add_pi32(dst1, src2); /* src2 + dst1 -> dst1 */
  197. dst1 = _mm_or_si64(dst1, dsta); /* dsta(full alpha) | dst1 -> dst1 */
  198. *(__m64 *) dstp = dst1; /* dst1 -> 2 x dst pixels */
  199. dstp += 2;
  200. srcp += 2;
  201. }
  202. srcp += srcskip;
  203. dstp += dstskip;
  204. }
  205. _mm_empty();
  206. }
  207. /* fast RGB888->(A)RGB888 blending with surface alpha */
  208. static void
  209. BlitRGBtoRGBSurfaceAlphaMMX(SDL_BlitInfo * info)
  210. {
  211. SDL_PixelFormat *df = info->dst_fmt;
  212. Uint32 chanmask;
  213. unsigned alpha = info->a;
  214. if (alpha == 128 && (df->Rmask | df->Gmask | df->Bmask) == 0x00FFFFFF) {
  215. /* only call a128 version when R,G,B occupy lower bits */
  216. BlitRGBtoRGBSurfaceAlpha128MMX(info);
  217. } else {
  218. int width = info->dst_w;
  219. int height = info->dst_h;
  220. Uint32 *srcp = (Uint32 *) info->src;
  221. int srcskip = info->src_skip >> 2;
  222. Uint32 *dstp = (Uint32 *) info->dst;
  223. int dstskip = info->dst_skip >> 2;
  224. Uint32 dalpha = df->Amask;
  225. Uint32 amult;
  226. __m64 src1, src2, dst1, dst2, mm_alpha, mm_zero, dsta;
  227. mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
  228. /* form the alpha mult */
  229. amult = alpha | (alpha << 8);
  230. amult = amult | (amult << 16);
  231. chanmask =
  232. (0xff << df->Rshift) | (0xff << df->
  233. Gshift) | (0xff << df->Bshift);
  234. mm_alpha = _mm_set_pi32(0, amult & chanmask); /* 0000AAAA -> mm_alpha, minus 1 chan */
  235. mm_alpha = _mm_unpacklo_pi8(mm_alpha, mm_zero); /* 0A0A0A0A -> mm_alpha, minus 1 chan */
  236. /* at this point mm_alpha can be 000A0A0A or 0A0A0A00 or another combo */
  237. dsta = _mm_set_pi32(dalpha, dalpha); /* dst alpha mask -> dsta */
  238. while (height--) {
  239. int n = width;
  240. if (n & 1) {
  241. /* One Pixel Blend */
  242. src2 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src2 (0000ARGB) */
  243. src2 = _mm_unpacklo_pi8(src2, mm_zero); /* 0A0R0G0B -> src2 */
  244. dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB) */
  245. dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
  246. src2 = _mm_sub_pi16(src2, dst1); /* src2 - dst2 -> src2 */
  247. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  248. src2 = _mm_srli_pi16(src2, 8); /* src2 >> 8 -> src2 */
  249. dst1 = _mm_add_pi8(src2, dst1); /* src2 + dst1 -> dst1 */
  250. dst1 = _mm_packs_pu16(dst1, mm_zero); /* 0000ARGB -> dst1 */
  251. dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
  252. *dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
  253. ++srcp;
  254. ++dstp;
  255. n--;
  256. }
  257. for (n >>= 1; n > 0; --n) {
  258. /* Two Pixels Blend */
  259. src1 = *(__m64 *) srcp; /* 2 x src -> src1(ARGBARGB) */
  260. src2 = src1; /* 2 x src -> src2(ARGBARGB) */
  261. src1 = _mm_unpacklo_pi8(src1, mm_zero); /* low - 0A0R0G0B -> src1 */
  262. src2 = _mm_unpackhi_pi8(src2, mm_zero); /* high - 0A0R0G0B -> src2 */
  263. dst1 = *(__m64 *) dstp; /* 2 x dst -> dst1(ARGBARGB) */
  264. dst2 = dst1; /* 2 x dst -> dst2(ARGBARGB) */
  265. dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* low - 0A0R0G0B -> dst1 */
  266. dst2 = _mm_unpackhi_pi8(dst2, mm_zero); /* high - 0A0R0G0B -> dst2 */
  267. src1 = _mm_sub_pi16(src1, dst1); /* src1 - dst1 -> src1 */
  268. src1 = _mm_mullo_pi16(src1, mm_alpha); /* src1 * alpha -> src1 */
  269. src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1 */
  270. dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst1) -> dst1 */
  271. src2 = _mm_sub_pi16(src2, dst2); /* src2 - dst2 -> src2 */
  272. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  273. src2 = _mm_srli_pi16(src2, 8); /* src2 >> 8 -> src2 */
  274. dst2 = _mm_add_pi8(src2, dst2); /* src2 + dst2(dst2) -> dst2 */
  275. dst1 = _mm_packs_pu16(dst1, dst2); /* 0A0R0G0B(res1), 0A0R0G0B(res2) -> dst1(ARGBARGB) */
  276. dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
  277. *(__m64 *) dstp = dst1; /* dst1 -> 2 x pixel */
  278. srcp += 2;
  279. dstp += 2;
  280. }
  281. srcp += srcskip;
  282. dstp += dstskip;
  283. }
  284. _mm_empty();
  285. }
  286. }
  287. /* fast ARGB888->(A)RGB888 blending with pixel alpha */
  288. static void
  289. BlitRGBtoRGBPixelAlphaMMX(SDL_BlitInfo * info)
  290. {
  291. int width = info->dst_w;
  292. int height = info->dst_h;
  293. Uint32 *srcp = (Uint32 *) info->src;
  294. int srcskip = info->src_skip >> 2;
  295. Uint32 *dstp = (Uint32 *) info->dst;
  296. int dstskip = info->dst_skip >> 2;
  297. SDL_PixelFormat *sf = info->src_fmt;
  298. Uint32 amask = sf->Amask;
  299. Uint32 ashift = sf->Ashift;
  300. Uint64 multmask, multmask2;
  301. __m64 src1, dst1, mm_alpha, mm_zero, mm_alpha2;
  302. mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
  303. multmask = 0x00FF;
  304. multmask <<= (ashift * 2);
  305. multmask2 = 0x00FF00FF00FF00FFULL;
  306. while (height--) {
  307. /* *INDENT-OFF* */ /* clang-format off */
  308. DUFFS_LOOP4({
  309. Uint32 alpha = *srcp & amask;
  310. if (alpha == 0) {
  311. /* do nothing */
  312. } else if (alpha == amask) {
  313. *dstp = *srcp;
  314. } else {
  315. src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB) */
  316. src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
  317. dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB) */
  318. dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
  319. mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
  320. mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
  321. mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
  322. mm_alpha2 = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha2 */
  323. mm_alpha = _mm_or_si64(mm_alpha2, *(__m64 *) & multmask); /* 0F0A0A0A -> mm_alpha */
  324. mm_alpha2 = _mm_xor_si64(mm_alpha2, *(__m64 *) & multmask2); /* 255 - mm_alpha -> mm_alpha */
  325. /* blend */
  326. src1 = _mm_mullo_pi16(src1, mm_alpha);
  327. src1 = _mm_srli_pi16(src1, 8);
  328. dst1 = _mm_mullo_pi16(dst1, mm_alpha2);
  329. dst1 = _mm_srli_pi16(dst1, 8);
  330. dst1 = _mm_add_pi16(src1, dst1);
  331. dst1 = _mm_packs_pu16(dst1, mm_zero);
  332. *dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
  333. }
  334. ++srcp;
  335. ++dstp;
  336. }, width);
  337. /* *INDENT-ON* */ /* clang-format on */
  338. srcp += srcskip;
  339. dstp += dstskip;
  340. }
  341. _mm_empty();
  342. }
  343. #endif /* __MMX__ */
  344. #if SDL_ARM_SIMD_BLITTERS
  345. void BlitARGBto565PixelAlphaARMSIMDAsm(int32_t w, int32_t h, uint16_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
  346. static void
  347. BlitARGBto565PixelAlphaARMSIMD(SDL_BlitInfo * info)
  348. {
  349. int32_t width = info->dst_w;
  350. int32_t height = info->dst_h;
  351. uint16_t *dstp = (uint16_t *)info->dst;
  352. int32_t dststride = width + (info->dst_skip >> 1);
  353. uint32_t *srcp = (uint32_t *)info->src;
  354. int32_t srcstride = width + (info->src_skip >> 2);
  355. BlitARGBto565PixelAlphaARMSIMDAsm(width, height, dstp, dststride, srcp, srcstride);
  356. }
  357. void BlitRGBtoRGBPixelAlphaARMSIMDAsm(int32_t w, int32_t h, uint32_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
  358. static void
  359. BlitRGBtoRGBPixelAlphaARMSIMD(SDL_BlitInfo * info)
  360. {
  361. int32_t width = info->dst_w;
  362. int32_t height = info->dst_h;
  363. uint32_t *dstp = (uint32_t *)info->dst;
  364. int32_t dststride = width + (info->dst_skip >> 2);
  365. uint32_t *srcp = (uint32_t *)info->src;
  366. int32_t srcstride = width + (info->src_skip >> 2);
  367. BlitRGBtoRGBPixelAlphaARMSIMDAsm(width, height, dstp, dststride, srcp, srcstride);
  368. }
  369. #endif
  370. #if SDL_ARM_NEON_BLITTERS
  371. void BlitARGBto565PixelAlphaARMNEONAsm(int32_t w, int32_t h, uint16_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
  372. static void
  373. BlitARGBto565PixelAlphaARMNEON(SDL_BlitInfo * info)
  374. {
  375. int32_t width = info->dst_w;
  376. int32_t height = info->dst_h;
  377. uint16_t *dstp = (uint16_t *)info->dst;
  378. int32_t dststride = width + (info->dst_skip >> 1);
  379. uint32_t *srcp = (uint32_t *)info->src;
  380. int32_t srcstride = width + (info->src_skip >> 2);
  381. BlitARGBto565PixelAlphaARMNEONAsm(width, height, dstp, dststride, srcp, srcstride);
  382. }
  383. void BlitRGBtoRGBPixelAlphaARMNEONAsm(int32_t w, int32_t h, uint32_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
  384. static void
  385. BlitRGBtoRGBPixelAlphaARMNEON(SDL_BlitInfo * info)
  386. {
  387. int32_t width = info->dst_w;
  388. int32_t height = info->dst_h;
  389. uint32_t *dstp = (uint32_t *)info->dst;
  390. int32_t dststride = width + (info->dst_skip >> 2);
  391. uint32_t *srcp = (uint32_t *)info->src;
  392. int32_t srcstride = width + (info->src_skip >> 2);
  393. BlitRGBtoRGBPixelAlphaARMNEONAsm(width, height, dstp, dststride, srcp, srcstride);
  394. }
  395. #endif
  396. /* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
  397. static void
  398. BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo * info)
  399. {
  400. int width = info->dst_w;
  401. int height = info->dst_h;
  402. Uint32 *srcp = (Uint32 *) info->src;
  403. int srcskip = info->src_skip >> 2;
  404. Uint32 *dstp = (Uint32 *) info->dst;
  405. int dstskip = info->dst_skip >> 2;
  406. while (height--) {
  407. /* *INDENT-OFF* */ /* clang-format off */
  408. DUFFS_LOOP4({
  409. Uint32 s = *srcp++;
  410. Uint32 d = *dstp;
  411. *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
  412. + (s & d & 0x00010101)) | 0xff000000;
  413. }, width);
  414. /* *INDENT-ON* */ /* clang-format on */
  415. srcp += srcskip;
  416. dstp += dstskip;
  417. }
  418. }
  419. /* fast RGB888->(A)RGB888 blending with surface alpha */
  420. static void
  421. BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo * info)
  422. {
  423. unsigned alpha = info->a;
  424. if (alpha == 128) {
  425. BlitRGBtoRGBSurfaceAlpha128(info);
  426. } else {
  427. int width = info->dst_w;
  428. int height = info->dst_h;
  429. Uint32 *srcp = (Uint32 *) info->src;
  430. int srcskip = info->src_skip >> 2;
  431. Uint32 *dstp = (Uint32 *) info->dst;
  432. int dstskip = info->dst_skip >> 2;
  433. Uint32 s;
  434. Uint32 d;
  435. Uint32 s1;
  436. Uint32 d1;
  437. while (height--) {
  438. /* *INDENT-OFF* */ /* clang-format off */
  439. DUFFS_LOOP4({
  440. s = *srcp;
  441. d = *dstp;
  442. s1 = s & 0xff00ff;
  443. d1 = d & 0xff00ff;
  444. d1 = (d1 + ((s1 - d1) * alpha >> 8))
  445. & 0xff00ff;
  446. s &= 0xff00;
  447. d &= 0xff00;
  448. d = (d + ((s - d) * alpha >> 8)) & 0xff00;
  449. *dstp = d1 | d | 0xff000000;
  450. ++srcp;
  451. ++dstp;
  452. }, width);
  453. /* *INDENT-ON* */ /* clang-format on */
  454. srcp += srcskip;
  455. dstp += dstskip;
  456. }
  457. }
  458. }
  459. /* fast ARGB888->(A)RGB888 blending with pixel alpha */
  460. static void
  461. BlitRGBtoRGBPixelAlpha(SDL_BlitInfo * info)
  462. {
  463. int width = info->dst_w;
  464. int height = info->dst_h;
  465. Uint32 *srcp = (Uint32 *) info->src;
  466. int srcskip = info->src_skip >> 2;
  467. Uint32 *dstp = (Uint32 *) info->dst;
  468. int dstskip = info->dst_skip >> 2;
  469. while (height--) {
  470. /* *INDENT-OFF* */ /* clang-format off */
  471. DUFFS_LOOP4({
  472. Uint32 dalpha;
  473. Uint32 d;
  474. Uint32 s1;
  475. Uint32 d1;
  476. Uint32 s = *srcp;
  477. Uint32 alpha = s >> 24;
  478. /* FIXME: Here we special-case opaque alpha since the
  479. compositioning used (>>8 instead of /255) doesn't handle
  480. it correctly. Also special-case alpha=0 for speed?
  481. Benchmark this! */
  482. if (alpha) {
  483. if (alpha == SDL_ALPHA_OPAQUE) {
  484. *dstp = *srcp;
  485. } else {
  486. /*
  487. * take out the middle component (green), and process
  488. * the other two in parallel. One multiply less.
  489. */
  490. d = *dstp;
  491. dalpha = d >> 24;
  492. s1 = s & 0xff00ff;
  493. d1 = d & 0xff00ff;
  494. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
  495. s &= 0xff00;
  496. d &= 0xff00;
  497. d = (d + ((s - d) * alpha >> 8)) & 0xff00;
  498. dalpha = alpha + (dalpha * (alpha ^ 0xFF) >> 8);
  499. *dstp = d1 | d | (dalpha << 24);
  500. }
  501. }
  502. ++srcp;
  503. ++dstp;
  504. }, width);
  505. /* *INDENT-ON* */ /* clang-format on */
  506. srcp += srcskip;
  507. dstp += dstskip;
  508. }
  509. }
  510. /* fast ARGB888->(A)BGR888 blending with pixel alpha */
  511. static void
  512. BlitRGBtoBGRPixelAlpha(SDL_BlitInfo * info)
  513. {
  514. int width = info->dst_w;
  515. int height = info->dst_h;
  516. Uint32 *srcp = (Uint32 *) info->src;
  517. int srcskip = info->src_skip >> 2;
  518. Uint32 *dstp = (Uint32 *) info->dst;
  519. int dstskip = info->dst_skip >> 2;
  520. while (height--) {
  521. /* *INDENT-OFF* */ /* clang-format off */
  522. DUFFS_LOOP4({
  523. Uint32 dalpha;
  524. Uint32 d;
  525. Uint32 s1;
  526. Uint32 d1;
  527. Uint32 s = *srcp;
  528. Uint32 alpha = s >> 24;
  529. /* FIXME: Here we special-case opaque alpha since the
  530. compositioning used (>>8 instead of /255) doesn't handle
  531. it correctly. Also special-case alpha=0 for speed?
  532. Benchmark this! */
  533. if (alpha) {
  534. /*
  535. * take out the middle component (green), and process
  536. * the other two in parallel. One multiply less.
  537. */
  538. s1 = s & 0xff00ff;
  539. s1 = (s1 >> 16) | (s1 << 16);
  540. s &= 0xff00;
  541. if (alpha == SDL_ALPHA_OPAQUE) {
  542. *dstp = 0xff000000 | s | s1;
  543. } else {
  544. d = *dstp;
  545. dalpha = d >> 24;
  546. d1 = d & 0xff00ff;
  547. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
  548. d &= 0xff00;
  549. d = (d + ((s - d) * alpha >> 8)) & 0xff00;
  550. dalpha = alpha + (dalpha * (alpha ^ 0xFF) >> 8);
  551. *dstp = d1 | d | (dalpha << 24);
  552. }
  553. }
  554. ++srcp;
  555. ++dstp;
  556. }, width);
  557. /* *INDENT-ON* */ /* clang-format on */
  558. srcp += srcskip;
  559. dstp += dstskip;
  560. }
  561. }
  562. #ifdef __3dNOW__
  563. /* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
  564. static void
  565. BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo * info)
  566. {
  567. int width = info->dst_w;
  568. int height = info->dst_h;
  569. Uint32 *srcp = (Uint32 *) info->src;
  570. int srcskip = info->src_skip >> 2;
  571. Uint32 *dstp = (Uint32 *) info->dst;
  572. int dstskip = info->dst_skip >> 2;
  573. SDL_PixelFormat *sf = info->src_fmt;
  574. Uint32 amask = sf->Amask;
  575. Uint32 ashift = sf->Ashift;
  576. Uint64 multmask, multmask2;
  577. __m64 src1, dst1, mm_alpha, mm_zero, mm_alpha2;
  578. mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
  579. multmask = 0x00FF;
  580. multmask <<= (ashift * 2);
  581. multmask2 = 0x00FF00FF00FF00FFULL;
  582. while (height--) {
  583. /* *INDENT-OFF* */ /* clang-format off */
  584. DUFFS_LOOP4({
  585. Uint32 alpha;
  586. _m_prefetch(srcp + 16);
  587. _m_prefetch(dstp + 16);
  588. alpha = *srcp & amask;
  589. if (alpha == 0) {
  590. /* do nothing */
  591. } else if (alpha == amask) {
  592. *dstp = *srcp;
  593. } else {
  594. src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB) */
  595. src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
  596. dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB) */
  597. dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
  598. mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
  599. mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
  600. mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
  601. mm_alpha2 = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha2 */
  602. mm_alpha = _mm_or_si64(mm_alpha2, *(__m64 *) & multmask); /* 0F0A0A0A -> mm_alpha */
  603. mm_alpha2 = _mm_xor_si64(mm_alpha2, *(__m64 *) & multmask2); /* 255 - mm_alpha -> mm_alpha */
  604. /* blend */
  605. src1 = _mm_mullo_pi16(src1, mm_alpha);
  606. src1 = _mm_srli_pi16(src1, 8);
  607. dst1 = _mm_mullo_pi16(dst1, mm_alpha2);
  608. dst1 = _mm_srli_pi16(dst1, 8);
  609. dst1 = _mm_add_pi16(src1, dst1);
  610. dst1 = _mm_packs_pu16(dst1, mm_zero);
  611. *dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
  612. }
  613. ++srcp;
  614. ++dstp;
  615. }, width);
  616. /* *INDENT-ON* */ /* clang-format on */
  617. srcp += srcskip;
  618. dstp += dstskip;
  619. }
  620. _mm_empty();
  621. }
  622. #endif /* __3dNOW__ */
  623. /* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
  624. /* blend a single 16 bit pixel at 50% */
  625. #define BLEND16_50(d, s, mask) \
  626. ((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
  627. /* blend two 16 bit pixels at 50% */
  628. #define BLEND2x16_50(d, s, mask) \
  629. (((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
  630. + (s & d & (~(mask | mask << 16))))
  631. static void
  632. Blit16to16SurfaceAlpha128(SDL_BlitInfo * info, Uint16 mask)
  633. {
  634. int width = info->dst_w;
  635. int height = info->dst_h;
  636. Uint16 *srcp = (Uint16 *) info->src;
  637. int srcskip = info->src_skip >> 1;
  638. Uint16 *dstp = (Uint16 *) info->dst;
  639. int dstskip = info->dst_skip >> 1;
  640. while (height--) {
  641. if (((uintptr_t) srcp ^ (uintptr_t) dstp) & 2) {
  642. /*
  643. * Source and destination not aligned, pipeline it.
  644. * This is mostly a win for big blits but no loss for
  645. * small ones
  646. */
  647. Uint32 prev_sw;
  648. int w = width;
  649. /* handle odd destination */
  650. if ((uintptr_t) dstp & 2) {
  651. Uint16 d = *dstp, s = *srcp;
  652. *dstp = BLEND16_50(d, s, mask);
  653. dstp++;
  654. srcp++;
  655. w--;
  656. }
  657. srcp++; /* srcp is now 32-bit aligned */
  658. /* bootstrap pipeline with first halfword */
  659. prev_sw = ((Uint32 *) srcp)[-1];
  660. while (w > 1) {
  661. Uint32 sw, dw, s;
  662. sw = *(Uint32 *) srcp;
  663. dw = *(Uint32 *) dstp;
  664. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  665. s = (prev_sw << 16) + (sw >> 16);
  666. #else
  667. s = (prev_sw >> 16) + (sw << 16);
  668. #endif
  669. prev_sw = sw;
  670. *(Uint32 *) dstp = BLEND2x16_50(dw, s, mask);
  671. dstp += 2;
  672. srcp += 2;
  673. w -= 2;
  674. }
  675. /* final pixel if any */
  676. if (w) {
  677. Uint16 d = *dstp, s;
  678. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  679. s = (Uint16) prev_sw;
  680. #else
  681. s = (Uint16) (prev_sw >> 16);
  682. #endif
  683. *dstp = BLEND16_50(d, s, mask);
  684. srcp++;
  685. dstp++;
  686. }
  687. srcp += srcskip - 1;
  688. dstp += dstskip;
  689. } else {
  690. /* source and destination are aligned */
  691. int w = width;
  692. /* first odd pixel? */
  693. if ((uintptr_t) srcp & 2) {
  694. Uint16 d = *dstp, s = *srcp;
  695. *dstp = BLEND16_50(d, s, mask);
  696. srcp++;
  697. dstp++;
  698. w--;
  699. }
  700. /* srcp and dstp are now 32-bit aligned */
  701. while (w > 1) {
  702. Uint32 sw = *(Uint32 *) srcp;
  703. Uint32 dw = *(Uint32 *) dstp;
  704. *(Uint32 *) dstp = BLEND2x16_50(dw, sw, mask);
  705. srcp += 2;
  706. dstp += 2;
  707. w -= 2;
  708. }
  709. /* last odd pixel? */
  710. if (w) {
  711. Uint16 d = *dstp, s = *srcp;
  712. *dstp = BLEND16_50(d, s, mask);
  713. srcp++;
  714. dstp++;
  715. }
  716. srcp += srcskip;
  717. dstp += dstskip;
  718. }
  719. }
  720. }
  721. #ifdef __MMX__
  722. /* fast RGB565->RGB565 blending with surface alpha */
  723. static void
  724. Blit565to565SurfaceAlphaMMX(SDL_BlitInfo * info)
  725. {
  726. unsigned alpha = info->a;
  727. if (alpha == 128) {
  728. Blit16to16SurfaceAlpha128(info, 0xf7de);
  729. } else {
  730. int width = info->dst_w;
  731. int height = info->dst_h;
  732. Uint16 *srcp = (Uint16 *) info->src;
  733. int srcskip = info->src_skip >> 1;
  734. Uint16 *dstp = (Uint16 *) info->dst;
  735. int dstskip = info->dst_skip >> 1;
  736. Uint32 s, d;
  737. __m64 src1, dst1, src2, dst2, gmask, bmask, mm_res, mm_alpha;
  738. alpha &= ~(1 + 2 + 4); /* cut alpha to get the exact same behaviour */
  739. mm_alpha = _mm_set_pi32(0, alpha); /* 0000000A -> mm_alpha */
  740. alpha >>= 3; /* downscale alpha to 5 bits */
  741. mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
  742. mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
  743. /* position alpha to allow for mullo and mulhi on diff channels
  744. to reduce the number of operations */
  745. mm_alpha = _mm_slli_si64(mm_alpha, 3);
  746. /* Setup the 565 color channel masks */
  747. gmask = _mm_set_pi32(0x07E007E0, 0x07E007E0); /* MASKGREEN -> gmask */
  748. bmask = _mm_set_pi32(0x001F001F, 0x001F001F); /* MASKBLUE -> bmask */
  749. while (height--) {
  750. /* *INDENT-OFF* */ /* clang-format off */
  751. DUFFS_LOOP_124(
  752. {
  753. s = *srcp++;
  754. d = *dstp;
  755. /*
  756. * shift out the middle component (green) to
  757. * the high 16 bits, and process all three RGB
  758. * components at the same time.
  759. */
  760. s = (s | s << 16) & 0x07e0f81f;
  761. d = (d | d << 16) & 0x07e0f81f;
  762. d += (s - d) * alpha >> 5;
  763. d &= 0x07e0f81f;
  764. *dstp++ = (Uint16)(d | d >> 16);
  765. },{
  766. s = *srcp++;
  767. d = *dstp;
  768. /*
  769. * shift out the middle component (green) to
  770. * the high 16 bits, and process all three RGB
  771. * components at the same time.
  772. */
  773. s = (s | s << 16) & 0x07e0f81f;
  774. d = (d | d << 16) & 0x07e0f81f;
  775. d += (s - d) * alpha >> 5;
  776. d &= 0x07e0f81f;
  777. *dstp++ = (Uint16)(d | d >> 16);
  778. s = *srcp++;
  779. d = *dstp;
  780. /*
  781. * shift out the middle component (green) to
  782. * the high 16 bits, and process all three RGB
  783. * components at the same time.
  784. */
  785. s = (s | s << 16) & 0x07e0f81f;
  786. d = (d | d << 16) & 0x07e0f81f;
  787. d += (s - d) * alpha >> 5;
  788. d &= 0x07e0f81f;
  789. *dstp++ = (Uint16)(d | d >> 16);
  790. },{
  791. src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
  792. dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
  793. /* red */
  794. src2 = src1;
  795. src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 [000r 000r 000r 000r] */
  796. dst2 = dst1;
  797. dst2 = _mm_srli_pi16(dst2, 11); /* dst2 >> 11 -> dst2 [000r 000r 000r 000r] */
  798. /* blend */
  799. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  800. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  801. src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
  802. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  803. dst2 = _mm_slli_pi16(dst2, 11); /* dst2 << 11 -> dst2 */
  804. mm_res = dst2; /* RED -> mm_res */
  805. /* green -- process the bits in place */
  806. src2 = src1;
  807. src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
  808. dst2 = dst1;
  809. dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
  810. /* blend */
  811. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  812. src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  813. src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
  814. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  815. mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
  816. /* blue */
  817. src2 = src1;
  818. src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
  819. dst2 = dst1;
  820. dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
  821. /* blend */
  822. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  823. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  824. src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
  825. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  826. dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
  827. mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
  828. *(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
  829. srcp += 4;
  830. dstp += 4;
  831. }, width);
  832. /* *INDENT-ON* */ /* clang-format on */
  833. srcp += srcskip;
  834. dstp += dstskip;
  835. }
  836. _mm_empty();
  837. }
  838. }
  839. /* fast RGB555->RGB555 blending with surface alpha */
  840. static void
  841. Blit555to555SurfaceAlphaMMX(SDL_BlitInfo * info)
  842. {
  843. unsigned alpha = info->a;
  844. if (alpha == 128) {
  845. Blit16to16SurfaceAlpha128(info, 0xfbde);
  846. } else {
  847. int width = info->dst_w;
  848. int height = info->dst_h;
  849. Uint16 *srcp = (Uint16 *) info->src;
  850. int srcskip = info->src_skip >> 1;
  851. Uint16 *dstp = (Uint16 *) info->dst;
  852. int dstskip = info->dst_skip >> 1;
  853. Uint32 s, d;
  854. __m64 src1, dst1, src2, dst2, rmask, gmask, bmask, mm_res, mm_alpha;
  855. alpha &= ~(1 + 2 + 4); /* cut alpha to get the exact same behaviour */
  856. mm_alpha = _mm_set_pi32(0, alpha); /* 0000000A -> mm_alpha */
  857. alpha >>= 3; /* downscale alpha to 5 bits */
  858. mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
  859. mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
  860. /* position alpha to allow for mullo and mulhi on diff channels
  861. to reduce the number of operations */
  862. mm_alpha = _mm_slli_si64(mm_alpha, 3);
  863. /* Setup the 555 color channel masks */
  864. rmask = _mm_set_pi32(0x7C007C00, 0x7C007C00); /* MASKRED -> rmask */
  865. gmask = _mm_set_pi32(0x03E003E0, 0x03E003E0); /* MASKGREEN -> gmask */
  866. bmask = _mm_set_pi32(0x001F001F, 0x001F001F); /* MASKBLUE -> bmask */
  867. while (height--) {
  868. /* *INDENT-OFF* */ /* clang-format off */
  869. DUFFS_LOOP_124(
  870. {
  871. s = *srcp++;
  872. d = *dstp;
  873. /*
  874. * shift out the middle component (green) to
  875. * the high 16 bits, and process all three RGB
  876. * components at the same time.
  877. */
  878. s = (s | s << 16) & 0x03e07c1f;
  879. d = (d | d << 16) & 0x03e07c1f;
  880. d += (s - d) * alpha >> 5;
  881. d &= 0x03e07c1f;
  882. *dstp++ = (Uint16)(d | d >> 16);
  883. },{
  884. s = *srcp++;
  885. d = *dstp;
  886. /*
  887. * shift out the middle component (green) to
  888. * the high 16 bits, and process all three RGB
  889. * components at the same time.
  890. */
  891. s = (s | s << 16) & 0x03e07c1f;
  892. d = (d | d << 16) & 0x03e07c1f;
  893. d += (s - d) * alpha >> 5;
  894. d &= 0x03e07c1f;
  895. *dstp++ = (Uint16)(d | d >> 16);
  896. s = *srcp++;
  897. d = *dstp;
  898. /*
  899. * shift out the middle component (green) to
  900. * the high 16 bits, and process all three RGB
  901. * components at the same time.
  902. */
  903. s = (s | s << 16) & 0x03e07c1f;
  904. d = (d | d << 16) & 0x03e07c1f;
  905. d += (s - d) * alpha >> 5;
  906. d &= 0x03e07c1f;
  907. *dstp++ = (Uint16)(d | d >> 16);
  908. },{
  909. src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
  910. dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
  911. /* red -- process the bits in place */
  912. src2 = src1;
  913. src2 = _mm_and_si64(src2, rmask); /* src & MASKRED -> src2 */
  914. dst2 = dst1;
  915. dst2 = _mm_and_si64(dst2, rmask); /* dst & MASKRED -> dst2 */
  916. /* blend */
  917. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  918. src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  919. src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
  920. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  921. dst2 = _mm_and_si64(dst2, rmask); /* dst2 & MASKRED -> dst2 */
  922. mm_res = dst2; /* RED -> mm_res */
  923. /* green -- process the bits in place */
  924. src2 = src1;
  925. src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
  926. dst2 = dst1;
  927. dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
  928. /* blend */
  929. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  930. src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  931. src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
  932. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  933. mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
  934. /* blue */
  935. src2 = src1; /* src -> src2 */
  936. src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
  937. dst2 = dst1; /* dst -> dst2 */
  938. dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
  939. /* blend */
  940. src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
  941. src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
  942. src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
  943. dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
  944. dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
  945. mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
  946. *(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
  947. srcp += 4;
  948. dstp += 4;
  949. }, width);
  950. /* *INDENT-ON* */ /* clang-format on */
  951. srcp += srcskip;
  952. dstp += dstskip;
  953. }
  954. _mm_empty();
  955. }
  956. }
  957. #endif /* __MMX__ */
  958. /* fast RGB565->RGB565 blending with surface alpha */
  959. static void
  960. Blit565to565SurfaceAlpha(SDL_BlitInfo * info)
  961. {
  962. unsigned alpha = info->a;
  963. if (alpha == 128) {
  964. Blit16to16SurfaceAlpha128(info, 0xf7de);
  965. } else {
  966. int width = info->dst_w;
  967. int height = info->dst_h;
  968. Uint16 *srcp = (Uint16 *) info->src;
  969. int srcskip = info->src_skip >> 1;
  970. Uint16 *dstp = (Uint16 *) info->dst;
  971. int dstskip = info->dst_skip >> 1;
  972. alpha >>= 3; /* downscale alpha to 5 bits */
  973. while (height--) {
  974. /* *INDENT-OFF* */ /* clang-format off */
  975. DUFFS_LOOP4({
  976. Uint32 s = *srcp++;
  977. Uint32 d = *dstp;
  978. /*
  979. * shift out the middle component (green) to
  980. * the high 16 bits, and process all three RGB
  981. * components at the same time.
  982. */
  983. s = (s | s << 16) & 0x07e0f81f;
  984. d = (d | d << 16) & 0x07e0f81f;
  985. d += (s - d) * alpha >> 5;
  986. d &= 0x07e0f81f;
  987. *dstp++ = (Uint16)(d | d >> 16);
  988. }, width);
  989. /* *INDENT-ON* */ /* clang-format on */
  990. srcp += srcskip;
  991. dstp += dstskip;
  992. }
  993. }
  994. }
  995. /* fast RGB555->RGB555 blending with surface alpha */
  996. static void
  997. Blit555to555SurfaceAlpha(SDL_BlitInfo * info)
  998. {
  999. unsigned alpha = info->a; /* downscale alpha to 5 bits */
  1000. if (alpha == 128) {
  1001. Blit16to16SurfaceAlpha128(info, 0xfbde);
  1002. } else {
  1003. int width = info->dst_w;
  1004. int height = info->dst_h;
  1005. Uint16 *srcp = (Uint16 *) info->src;
  1006. int srcskip = info->src_skip >> 1;
  1007. Uint16 *dstp = (Uint16 *) info->dst;
  1008. int dstskip = info->dst_skip >> 1;
  1009. alpha >>= 3; /* downscale alpha to 5 bits */
  1010. while (height--) {
  1011. /* *INDENT-OFF* */ /* clang-format off */
  1012. DUFFS_LOOP4({
  1013. Uint32 s = *srcp++;
  1014. Uint32 d = *dstp;
  1015. /*
  1016. * shift out the middle component (green) to
  1017. * the high 16 bits, and process all three RGB
  1018. * components at the same time.
  1019. */
  1020. s = (s | s << 16) & 0x03e07c1f;
  1021. d = (d | d << 16) & 0x03e07c1f;
  1022. d += (s - d) * alpha >> 5;
  1023. d &= 0x03e07c1f;
  1024. *dstp++ = (Uint16)(d | d >> 16);
  1025. }, width);
  1026. /* *INDENT-ON* */ /* clang-format on */
  1027. srcp += srcskip;
  1028. dstp += dstskip;
  1029. }
  1030. }
  1031. }
  1032. /* fast ARGB8888->RGB565 blending with pixel alpha */
  1033. static void
  1034. BlitARGBto565PixelAlpha(SDL_BlitInfo * info)
  1035. {
  1036. int width = info->dst_w;
  1037. int height = info->dst_h;
  1038. Uint32 *srcp = (Uint32 *) info->src;
  1039. int srcskip = info->src_skip >> 2;
  1040. Uint16 *dstp = (Uint16 *) info->dst;
  1041. int dstskip = info->dst_skip >> 1;
  1042. while (height--) {
  1043. /* *INDENT-OFF* */ /* clang-format off */
  1044. DUFFS_LOOP4({
  1045. Uint32 s = *srcp;
  1046. unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
  1047. /* FIXME: Here we special-case opaque alpha since the
  1048. compositioning used (>>8 instead of /255) doesn't handle
  1049. it correctly. Also special-case alpha=0 for speed?
  1050. Benchmark this! */
  1051. if(alpha) {
  1052. if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
  1053. *dstp = (Uint16)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3 & 0x1f));
  1054. } else {
  1055. Uint32 d = *dstp;
  1056. /*
  1057. * convert source and destination to G0RAB65565
  1058. * and blend all components at the same time
  1059. */
  1060. s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
  1061. + (s >> 3 & 0x1f);
  1062. d = (d | d << 16) & 0x07e0f81f;
  1063. d += (s - d) * alpha >> 5;
  1064. d &= 0x07e0f81f;
  1065. *dstp = (Uint16)(d | d >> 16);
  1066. }
  1067. }
  1068. srcp++;
  1069. dstp++;
  1070. }, width);
  1071. /* *INDENT-ON* */ /* clang-format on */
  1072. srcp += srcskip;
  1073. dstp += dstskip;
  1074. }
  1075. }
  1076. /* fast ARGB8888->RGB555 blending with pixel alpha */
  1077. static void
  1078. BlitARGBto555PixelAlpha(SDL_BlitInfo * info)
  1079. {
  1080. int width = info->dst_w;
  1081. int height = info->dst_h;
  1082. Uint32 *srcp = (Uint32 *) info->src;
  1083. int srcskip = info->src_skip >> 2;
  1084. Uint16 *dstp = (Uint16 *) info->dst;
  1085. int dstskip = info->dst_skip >> 1;
  1086. while (height--) {
  1087. /* *INDENT-OFF* */ /* clang-format off */
  1088. DUFFS_LOOP4({
  1089. unsigned alpha;
  1090. Uint32 s = *srcp;
  1091. alpha = s >> 27; /* downscale alpha to 5 bits */
  1092. /* FIXME: Here we special-case opaque alpha since the
  1093. compositioning used (>>8 instead of /255) doesn't handle
  1094. it correctly. Also special-case alpha=0 for speed?
  1095. Benchmark this! */
  1096. if(alpha) {
  1097. if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
  1098. *dstp = (Uint16)((s >> 9 & 0x7c00) + (s >> 6 & 0x3e0) + (s >> 3 & 0x1f));
  1099. } else {
  1100. Uint32 d = *dstp;
  1101. /*
  1102. * convert source and destination to G0RAB65565
  1103. * and blend all components at the same time
  1104. */
  1105. s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
  1106. + (s >> 3 & 0x1f);
  1107. d = (d | d << 16) & 0x03e07c1f;
  1108. d += (s - d) * alpha >> 5;
  1109. d &= 0x03e07c1f;
  1110. *dstp = (Uint16)(d | d >> 16);
  1111. }
  1112. }
  1113. srcp++;
  1114. dstp++;
  1115. }, width);
  1116. /* *INDENT-ON* */ /* clang-format on */
  1117. srcp += srcskip;
  1118. dstp += dstskip;
  1119. }
  1120. }
  1121. /* General (slow) N->N blending with per-surface alpha */
  1122. static void
  1123. BlitNtoNSurfaceAlpha(SDL_BlitInfo * info)
  1124. {
  1125. int width = info->dst_w;
  1126. int height = info->dst_h;
  1127. Uint8 *src = info->src;
  1128. int srcskip = info->src_skip;
  1129. Uint8 *dst = info->dst;
  1130. int dstskip = info->dst_skip;
  1131. SDL_PixelFormat *srcfmt = info->src_fmt;
  1132. SDL_PixelFormat *dstfmt = info->dst_fmt;
  1133. int srcbpp = srcfmt->BytesPerPixel;
  1134. int dstbpp = dstfmt->BytesPerPixel;
  1135. Uint32 Pixel;
  1136. unsigned sR, sG, sB;
  1137. unsigned dR, dG, dB, dA;
  1138. const unsigned sA = info->a;
  1139. if (sA) {
  1140. while (height--) {
  1141. /* *INDENT-OFF* */ /* clang-format off */
  1142. DUFFS_LOOP4(
  1143. {
  1144. DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
  1145. DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
  1146. ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA);
  1147. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  1148. src += srcbpp;
  1149. dst += dstbpp;
  1150. },
  1151. width);
  1152. /* *INDENT-ON* */ /* clang-format on */
  1153. src += srcskip;
  1154. dst += dstskip;
  1155. }
  1156. }
  1157. }
  1158. /* General (slow) colorkeyed N->N blending with per-surface alpha */
  1159. static void
  1160. BlitNtoNSurfaceAlphaKey(SDL_BlitInfo * info)
  1161. {
  1162. int width = info->dst_w;
  1163. int height = info->dst_h;
  1164. Uint8 *src = info->src;
  1165. int srcskip = info->src_skip;
  1166. Uint8 *dst = info->dst;
  1167. int dstskip = info->dst_skip;
  1168. SDL_PixelFormat *srcfmt = info->src_fmt;
  1169. SDL_PixelFormat *dstfmt = info->dst_fmt;
  1170. Uint32 ckey = info->colorkey;
  1171. int srcbpp = srcfmt->BytesPerPixel;
  1172. int dstbpp = dstfmt->BytesPerPixel;
  1173. Uint32 Pixel;
  1174. unsigned sR, sG, sB;
  1175. unsigned dR, dG, dB, dA;
  1176. const unsigned sA = info->a;
  1177. while (height--) {
  1178. /* *INDENT-OFF* */ /* clang-format off */
  1179. DUFFS_LOOP4(
  1180. {
  1181. RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
  1182. if(sA && Pixel != ckey) {
  1183. RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB);
  1184. DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
  1185. ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA);
  1186. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  1187. }
  1188. src += srcbpp;
  1189. dst += dstbpp;
  1190. },
  1191. width);
  1192. /* *INDENT-ON* */ /* clang-format on */
  1193. src += srcskip;
  1194. dst += dstskip;
  1195. }
  1196. }
  1197. /* General (slow) N->N blending with pixel alpha */
  1198. static void
  1199. BlitNtoNPixelAlpha(SDL_BlitInfo * info)
  1200. {
  1201. int width = info->dst_w;
  1202. int height = info->dst_h;
  1203. Uint8 *src = info->src;
  1204. int srcskip = info->src_skip;
  1205. Uint8 *dst = info->dst;
  1206. int dstskip = info->dst_skip;
  1207. SDL_PixelFormat *srcfmt = info->src_fmt;
  1208. SDL_PixelFormat *dstfmt = info->dst_fmt;
  1209. int srcbpp;
  1210. int dstbpp;
  1211. Uint32 Pixel;
  1212. unsigned sR, sG, sB, sA;
  1213. unsigned dR, dG, dB, dA;
  1214. /* Set up some basic variables */
  1215. srcbpp = srcfmt->BytesPerPixel;
  1216. dstbpp = dstfmt->BytesPerPixel;
  1217. while (height--) {
  1218. /* *INDENT-OFF* */ /* clang-format off */
  1219. DUFFS_LOOP4(
  1220. {
  1221. DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
  1222. if(sA) {
  1223. DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
  1224. ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA);
  1225. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  1226. }
  1227. src += srcbpp;
  1228. dst += dstbpp;
  1229. },
  1230. width);
  1231. /* *INDENT-ON* */ /* clang-format on */
  1232. src += srcskip;
  1233. dst += dstskip;
  1234. }
  1235. }
  1236. SDL_BlitFunc
  1237. SDL_CalculateBlitA(SDL_Surface * surface)
  1238. {
  1239. SDL_PixelFormat *sf = surface->format;
  1240. SDL_PixelFormat *df = surface->map->dst->format;
  1241. switch (surface->map->info.flags & ~SDL_COPY_RLE_MASK) {
  1242. case SDL_COPY_BLEND:
  1243. /* Per-pixel alpha blits */
  1244. switch (df->BytesPerPixel) {
  1245. case 1:
  1246. if (df->palette != NULL) {
  1247. return BlitNto1PixelAlpha;
  1248. } else {
  1249. /* RGB332 has no palette ! */
  1250. return BlitNtoNPixelAlpha;
  1251. }
  1252. case 2:
  1253. #if SDL_ARM_NEON_BLITTERS || SDL_ARM_SIMD_BLITTERS
  1254. if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
  1255. && sf->Gmask == 0xff00 && df->Gmask == 0x7e0
  1256. && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
  1257. || (sf->Bmask == 0xff && df->Bmask == 0x1f)))
  1258. {
  1259. #if SDL_ARM_NEON_BLITTERS
  1260. if (SDL_HasNEON())
  1261. return BlitARGBto565PixelAlphaARMNEON;
  1262. #endif
  1263. #if SDL_ARM_SIMD_BLITTERS
  1264. if (SDL_HasARMSIMD())
  1265. return BlitARGBto565PixelAlphaARMSIMD;
  1266. #endif
  1267. }
  1268. #endif
  1269. if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
  1270. && sf->Gmask == 0xff00
  1271. && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
  1272. || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
  1273. if (df->Gmask == 0x7e0)
  1274. return BlitARGBto565PixelAlpha;
  1275. else if (df->Gmask == 0x3e0)
  1276. return BlitARGBto555PixelAlpha;
  1277. }
  1278. return BlitNtoNPixelAlpha;
  1279. case 4:
  1280. if (sf->Rmask == df->Rmask
  1281. && sf->Gmask == df->Gmask
  1282. && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
  1283. #if defined(__MMX__) || defined(__3dNOW__)
  1284. if (sf->Rshift % 8 == 0
  1285. && sf->Gshift % 8 == 0
  1286. && sf->Bshift % 8 == 0
  1287. && sf->Ashift % 8 == 0 && sf->Aloss == 0) {
  1288. #ifdef __3dNOW__
  1289. if (SDL_Has3DNow())
  1290. return BlitRGBtoRGBPixelAlphaMMX3DNOW;
  1291. #endif
  1292. #ifdef __MMX__
  1293. if (SDL_HasMMX())
  1294. return BlitRGBtoRGBPixelAlphaMMX;
  1295. #endif
  1296. }
  1297. #endif /* __MMX__ || __3dNOW__ */
  1298. if (sf->Amask == 0xff000000) {
  1299. #if SDL_ARM_NEON_BLITTERS
  1300. if (SDL_HasNEON())
  1301. return BlitRGBtoRGBPixelAlphaARMNEON;
  1302. #endif
  1303. #if SDL_ARM_SIMD_BLITTERS
  1304. if (SDL_HasARMSIMD())
  1305. return BlitRGBtoRGBPixelAlphaARMSIMD;
  1306. #endif
  1307. return BlitRGBtoRGBPixelAlpha;
  1308. }
  1309. } else if (sf->Rmask == df->Bmask
  1310. && sf->Gmask == df->Gmask
  1311. && sf->Bmask == df->Rmask && sf->BytesPerPixel == 4) {
  1312. if (sf->Amask == 0xff000000) {
  1313. return BlitRGBtoBGRPixelAlpha;
  1314. }
  1315. }
  1316. return BlitNtoNPixelAlpha;
  1317. case 3:
  1318. default:
  1319. break;
  1320. }
  1321. return BlitNtoNPixelAlpha;
  1322. case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  1323. if (sf->Amask == 0) {
  1324. /* Per-surface alpha blits */
  1325. switch (df->BytesPerPixel) {
  1326. case 1:
  1327. if (df->palette != NULL) {
  1328. return BlitNto1SurfaceAlpha;
  1329. } else {
  1330. /* RGB332 has no palette ! */
  1331. return BlitNtoNSurfaceAlpha;
  1332. }
  1333. case 2:
  1334. if (surface->map->identity) {
  1335. if (df->Gmask == 0x7e0) {
  1336. #ifdef __MMX__
  1337. if (SDL_HasMMX())
  1338. return Blit565to565SurfaceAlphaMMX;
  1339. else
  1340. #endif
  1341. return Blit565to565SurfaceAlpha;
  1342. } else if (df->Gmask == 0x3e0) {
  1343. #ifdef __MMX__
  1344. if (SDL_HasMMX())
  1345. return Blit555to555SurfaceAlphaMMX;
  1346. else
  1347. #endif
  1348. return Blit555to555SurfaceAlpha;
  1349. }
  1350. }
  1351. return BlitNtoNSurfaceAlpha;
  1352. case 4:
  1353. if (sf->Rmask == df->Rmask
  1354. && sf->Gmask == df->Gmask
  1355. && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
  1356. #ifdef __MMX__
  1357. if (sf->Rshift % 8 == 0
  1358. && sf->Gshift % 8 == 0
  1359. && sf->Bshift % 8 == 0 && SDL_HasMMX())
  1360. return BlitRGBtoRGBSurfaceAlphaMMX;
  1361. #endif
  1362. if ((sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff) {
  1363. return BlitRGBtoRGBSurfaceAlpha;
  1364. }
  1365. }
  1366. return BlitNtoNSurfaceAlpha;
  1367. case 3:
  1368. default:
  1369. return BlitNtoNSurfaceAlpha;
  1370. }
  1371. }
  1372. break;
  1373. case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  1374. if (sf->Amask == 0) {
  1375. if (df->BytesPerPixel == 1) {
  1376. if (df->palette != NULL) {
  1377. return BlitNto1SurfaceAlphaKey;
  1378. } else {
  1379. /* RGB332 has no palette ! */
  1380. return BlitNtoNSurfaceAlphaKey;
  1381. }
  1382. } else {
  1383. return BlitNtoNSurfaceAlphaKey;
  1384. }
  1385. }
  1386. break;
  1387. }
  1388. return NULL;
  1389. }
  1390. #endif /* SDL_HAVE_BLIT_A */
  1391. /* vi: set ts=4 sw=4 expandtab: */