xor_32.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * Optimized RAID-5 checksumming functions for MMX and SSE.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2, or (at your option)
  7. * any later version.
  8. *
  9. * You should have received a copy of the GNU General Public License
  10. * (for example /usr/src/linux/COPYING); if not, write to the Free
  11. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  12. */
  13. /*
  14. * High-speed RAID5 checksumming functions utilizing MMX instructions.
  15. * Copyright (C) 1998 Ingo Molnar.
  16. */
  17. #define LD(x, y) " movq 8*("#x")(%1), %%mm"#y" ;\n"
  18. #define ST(x, y) " movq %%mm"#y", 8*("#x")(%1) ;\n"
  19. #define XO1(x, y) " pxor 8*("#x")(%2), %%mm"#y" ;\n"
  20. #define XO2(x, y) " pxor 8*("#x")(%3), %%mm"#y" ;\n"
  21. #define XO3(x, y) " pxor 8*("#x")(%4), %%mm"#y" ;\n"
  22. #define XO4(x, y) " pxor 8*("#x")(%5), %%mm"#y" ;\n"
  23. #include <asm/i387.h>
  24. static void
  25. xor_pII_mmx_2(unsigned long bytes, unsigned long *p1, unsigned long *p2)
  26. {
  27. unsigned long lines = bytes >> 7;
  28. kernel_fpu_begin();
  29. asm volatile(
  30. #undef BLOCK
  31. #define BLOCK(i) \
  32. LD(i, 0) \
  33. LD(i + 1, 1) \
  34. LD(i + 2, 2) \
  35. LD(i + 3, 3) \
  36. XO1(i, 0) \
  37. ST(i, 0) \
  38. XO1(i+1, 1) \
  39. ST(i+1, 1) \
  40. XO1(i + 2, 2) \
  41. ST(i + 2, 2) \
  42. XO1(i + 3, 3) \
  43. ST(i + 3, 3)
  44. " .align 32 ;\n"
  45. " 1: ;\n"
  46. BLOCK(0)
  47. BLOCK(4)
  48. BLOCK(8)
  49. BLOCK(12)
  50. " addl $128, %1 ;\n"
  51. " addl $128, %2 ;\n"
  52. " decl %0 ;\n"
  53. " jnz 1b ;\n"
  54. : "+r" (lines),
  55. "+r" (p1), "+r" (p2)
  56. :
  57. : "memory");
  58. kernel_fpu_end();
  59. }
  60. static void
  61. xor_pII_mmx_3(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  62. unsigned long *p3)
  63. {
  64. unsigned long lines = bytes >> 7;
  65. kernel_fpu_begin();
  66. asm volatile(
  67. #undef BLOCK
  68. #define BLOCK(i) \
  69. LD(i, 0) \
  70. LD(i + 1, 1) \
  71. LD(i + 2, 2) \
  72. LD(i + 3, 3) \
  73. XO1(i, 0) \
  74. XO1(i + 1, 1) \
  75. XO1(i + 2, 2) \
  76. XO1(i + 3, 3) \
  77. XO2(i, 0) \
  78. ST(i, 0) \
  79. XO2(i + 1, 1) \
  80. ST(i + 1, 1) \
  81. XO2(i + 2, 2) \
  82. ST(i + 2, 2) \
  83. XO2(i + 3, 3) \
  84. ST(i + 3, 3)
  85. " .align 32 ;\n"
  86. " 1: ;\n"
  87. BLOCK(0)
  88. BLOCK(4)
  89. BLOCK(8)
  90. BLOCK(12)
  91. " addl $128, %1 ;\n"
  92. " addl $128, %2 ;\n"
  93. " addl $128, %3 ;\n"
  94. " decl %0 ;\n"
  95. " jnz 1b ;\n"
  96. : "+r" (lines),
  97. "+r" (p1), "+r" (p2), "+r" (p3)
  98. :
  99. : "memory");
  100. kernel_fpu_end();
  101. }
  102. static void
  103. xor_pII_mmx_4(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  104. unsigned long *p3, unsigned long *p4)
  105. {
  106. unsigned long lines = bytes >> 7;
  107. kernel_fpu_begin();
  108. asm volatile(
  109. #undef BLOCK
  110. #define BLOCK(i) \
  111. LD(i, 0) \
  112. LD(i + 1, 1) \
  113. LD(i + 2, 2) \
  114. LD(i + 3, 3) \
  115. XO1(i, 0) \
  116. XO1(i + 1, 1) \
  117. XO1(i + 2, 2) \
  118. XO1(i + 3, 3) \
  119. XO2(i, 0) \
  120. XO2(i + 1, 1) \
  121. XO2(i + 2, 2) \
  122. XO2(i + 3, 3) \
  123. XO3(i, 0) \
  124. ST(i, 0) \
  125. XO3(i + 1, 1) \
  126. ST(i + 1, 1) \
  127. XO3(i + 2, 2) \
  128. ST(i + 2, 2) \
  129. XO3(i + 3, 3) \
  130. ST(i + 3, 3)
  131. " .align 32 ;\n"
  132. " 1: ;\n"
  133. BLOCK(0)
  134. BLOCK(4)
  135. BLOCK(8)
  136. BLOCK(12)
  137. " addl $128, %1 ;\n"
  138. " addl $128, %2 ;\n"
  139. " addl $128, %3 ;\n"
  140. " addl $128, %4 ;\n"
  141. " decl %0 ;\n"
  142. " jnz 1b ;\n"
  143. : "+r" (lines),
  144. "+r" (p1), "+r" (p2), "+r" (p3), "+r" (p4)
  145. :
  146. : "memory");
  147. kernel_fpu_end();
  148. }
  149. static void
  150. xor_pII_mmx_5(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  151. unsigned long *p3, unsigned long *p4, unsigned long *p5)
  152. {
  153. unsigned long lines = bytes >> 7;
  154. kernel_fpu_begin();
  155. /* Make sure GCC forgets anything it knows about p4 or p5,
  156. such that it won't pass to the asm volatile below a
  157. register that is shared with any other variable. That's
  158. because we modify p4 and p5 there, but we can't mark them
  159. as read/write, otherwise we'd overflow the 10-asm-operands
  160. limit of GCC < 3.1. */
  161. asm("" : "+r" (p4), "+r" (p5));
  162. asm volatile(
  163. #undef BLOCK
  164. #define BLOCK(i) \
  165. LD(i, 0) \
  166. LD(i + 1, 1) \
  167. LD(i + 2, 2) \
  168. LD(i + 3, 3) \
  169. XO1(i, 0) \
  170. XO1(i + 1, 1) \
  171. XO1(i + 2, 2) \
  172. XO1(i + 3, 3) \
  173. XO2(i, 0) \
  174. XO2(i + 1, 1) \
  175. XO2(i + 2, 2) \
  176. XO2(i + 3, 3) \
  177. XO3(i, 0) \
  178. XO3(i + 1, 1) \
  179. XO3(i + 2, 2) \
  180. XO3(i + 3, 3) \
  181. XO4(i, 0) \
  182. ST(i, 0) \
  183. XO4(i + 1, 1) \
  184. ST(i + 1, 1) \
  185. XO4(i + 2, 2) \
  186. ST(i + 2, 2) \
  187. XO4(i + 3, 3) \
  188. ST(i + 3, 3)
  189. " .align 32 ;\n"
  190. " 1: ;\n"
  191. BLOCK(0)
  192. BLOCK(4)
  193. BLOCK(8)
  194. BLOCK(12)
  195. " addl $128, %1 ;\n"
  196. " addl $128, %2 ;\n"
  197. " addl $128, %3 ;\n"
  198. " addl $128, %4 ;\n"
  199. " addl $128, %5 ;\n"
  200. " decl %0 ;\n"
  201. " jnz 1b ;\n"
  202. : "+r" (lines),
  203. "+r" (p1), "+r" (p2), "+r" (p3)
  204. : "r" (p4), "r" (p5)
  205. : "memory");
  206. /* p4 and p5 were modified, and now the variables are dead.
  207. Clobber them just to be sure nobody does something stupid
  208. like assuming they have some legal value. */
  209. asm("" : "=r" (p4), "=r" (p5));
  210. kernel_fpu_end();
  211. }
  212. #undef LD
  213. #undef XO1
  214. #undef XO2
  215. #undef XO3
  216. #undef XO4
  217. #undef ST
  218. #undef BLOCK
  219. static void
  220. xor_p5_mmx_2(unsigned long bytes, unsigned long *p1, unsigned long *p2)
  221. {
  222. unsigned long lines = bytes >> 6;
  223. kernel_fpu_begin();
  224. asm volatile(
  225. " .align 32 ;\n"
  226. " 1: ;\n"
  227. " movq (%1), %%mm0 ;\n"
  228. " movq 8(%1), %%mm1 ;\n"
  229. " pxor (%2), %%mm0 ;\n"
  230. " movq 16(%1), %%mm2 ;\n"
  231. " movq %%mm0, (%1) ;\n"
  232. " pxor 8(%2), %%mm1 ;\n"
  233. " movq 24(%1), %%mm3 ;\n"
  234. " movq %%mm1, 8(%1) ;\n"
  235. " pxor 16(%2), %%mm2 ;\n"
  236. " movq 32(%1), %%mm4 ;\n"
  237. " movq %%mm2, 16(%1) ;\n"
  238. " pxor 24(%2), %%mm3 ;\n"
  239. " movq 40(%1), %%mm5 ;\n"
  240. " movq %%mm3, 24(%1) ;\n"
  241. " pxor 32(%2), %%mm4 ;\n"
  242. " movq 48(%1), %%mm6 ;\n"
  243. " movq %%mm4, 32(%1) ;\n"
  244. " pxor 40(%2), %%mm5 ;\n"
  245. " movq 56(%1), %%mm7 ;\n"
  246. " movq %%mm5, 40(%1) ;\n"
  247. " pxor 48(%2), %%mm6 ;\n"
  248. " pxor 56(%2), %%mm7 ;\n"
  249. " movq %%mm6, 48(%1) ;\n"
  250. " movq %%mm7, 56(%1) ;\n"
  251. " addl $64, %1 ;\n"
  252. " addl $64, %2 ;\n"
  253. " decl %0 ;\n"
  254. " jnz 1b ;\n"
  255. : "+r" (lines),
  256. "+r" (p1), "+r" (p2)
  257. :
  258. : "memory");
  259. kernel_fpu_end();
  260. }
  261. static void
  262. xor_p5_mmx_3(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  263. unsigned long *p3)
  264. {
  265. unsigned long lines = bytes >> 6;
  266. kernel_fpu_begin();
  267. asm volatile(
  268. " .align 32,0x90 ;\n"
  269. " 1: ;\n"
  270. " movq (%1), %%mm0 ;\n"
  271. " movq 8(%1), %%mm1 ;\n"
  272. " pxor (%2), %%mm0 ;\n"
  273. " movq 16(%1), %%mm2 ;\n"
  274. " pxor 8(%2), %%mm1 ;\n"
  275. " pxor (%3), %%mm0 ;\n"
  276. " pxor 16(%2), %%mm2 ;\n"
  277. " movq %%mm0, (%1) ;\n"
  278. " pxor 8(%3), %%mm1 ;\n"
  279. " pxor 16(%3), %%mm2 ;\n"
  280. " movq 24(%1), %%mm3 ;\n"
  281. " movq %%mm1, 8(%1) ;\n"
  282. " movq 32(%1), %%mm4 ;\n"
  283. " movq 40(%1), %%mm5 ;\n"
  284. " pxor 24(%2), %%mm3 ;\n"
  285. " movq %%mm2, 16(%1) ;\n"
  286. " pxor 32(%2), %%mm4 ;\n"
  287. " pxor 24(%3), %%mm3 ;\n"
  288. " pxor 40(%2), %%mm5 ;\n"
  289. " movq %%mm3, 24(%1) ;\n"
  290. " pxor 32(%3), %%mm4 ;\n"
  291. " pxor 40(%3), %%mm5 ;\n"
  292. " movq 48(%1), %%mm6 ;\n"
  293. " movq %%mm4, 32(%1) ;\n"
  294. " movq 56(%1), %%mm7 ;\n"
  295. " pxor 48(%2), %%mm6 ;\n"
  296. " movq %%mm5, 40(%1) ;\n"
  297. " pxor 56(%2), %%mm7 ;\n"
  298. " pxor 48(%3), %%mm6 ;\n"
  299. " pxor 56(%3), %%mm7 ;\n"
  300. " movq %%mm6, 48(%1) ;\n"
  301. " movq %%mm7, 56(%1) ;\n"
  302. " addl $64, %1 ;\n"
  303. " addl $64, %2 ;\n"
  304. " addl $64, %3 ;\n"
  305. " decl %0 ;\n"
  306. " jnz 1b ;\n"
  307. : "+r" (lines),
  308. "+r" (p1), "+r" (p2), "+r" (p3)
  309. :
  310. : "memory" );
  311. kernel_fpu_end();
  312. }
  313. static void
  314. xor_p5_mmx_4(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  315. unsigned long *p3, unsigned long *p4)
  316. {
  317. unsigned long lines = bytes >> 6;
  318. kernel_fpu_begin();
  319. asm volatile(
  320. " .align 32,0x90 ;\n"
  321. " 1: ;\n"
  322. " movq (%1), %%mm0 ;\n"
  323. " movq 8(%1), %%mm1 ;\n"
  324. " pxor (%2), %%mm0 ;\n"
  325. " movq 16(%1), %%mm2 ;\n"
  326. " pxor 8(%2), %%mm1 ;\n"
  327. " pxor (%3), %%mm0 ;\n"
  328. " pxor 16(%2), %%mm2 ;\n"
  329. " pxor 8(%3), %%mm1 ;\n"
  330. " pxor (%4), %%mm0 ;\n"
  331. " movq 24(%1), %%mm3 ;\n"
  332. " pxor 16(%3), %%mm2 ;\n"
  333. " pxor 8(%4), %%mm1 ;\n"
  334. " movq %%mm0, (%1) ;\n"
  335. " movq 32(%1), %%mm4 ;\n"
  336. " pxor 24(%2), %%mm3 ;\n"
  337. " pxor 16(%4), %%mm2 ;\n"
  338. " movq %%mm1, 8(%1) ;\n"
  339. " movq 40(%1), %%mm5 ;\n"
  340. " pxor 32(%2), %%mm4 ;\n"
  341. " pxor 24(%3), %%mm3 ;\n"
  342. " movq %%mm2, 16(%1) ;\n"
  343. " pxor 40(%2), %%mm5 ;\n"
  344. " pxor 32(%3), %%mm4 ;\n"
  345. " pxor 24(%4), %%mm3 ;\n"
  346. " movq %%mm3, 24(%1) ;\n"
  347. " movq 56(%1), %%mm7 ;\n"
  348. " movq 48(%1), %%mm6 ;\n"
  349. " pxor 40(%3), %%mm5 ;\n"
  350. " pxor 32(%4), %%mm4 ;\n"
  351. " pxor 48(%2), %%mm6 ;\n"
  352. " movq %%mm4, 32(%1) ;\n"
  353. " pxor 56(%2), %%mm7 ;\n"
  354. " pxor 40(%4), %%mm5 ;\n"
  355. " pxor 48(%3), %%mm6 ;\n"
  356. " pxor 56(%3), %%mm7 ;\n"
  357. " movq %%mm5, 40(%1) ;\n"
  358. " pxor 48(%4), %%mm6 ;\n"
  359. " pxor 56(%4), %%mm7 ;\n"
  360. " movq %%mm6, 48(%1) ;\n"
  361. " movq %%mm7, 56(%1) ;\n"
  362. " addl $64, %1 ;\n"
  363. " addl $64, %2 ;\n"
  364. " addl $64, %3 ;\n"
  365. " addl $64, %4 ;\n"
  366. " decl %0 ;\n"
  367. " jnz 1b ;\n"
  368. : "+r" (lines),
  369. "+r" (p1), "+r" (p2), "+r" (p3), "+r" (p4)
  370. :
  371. : "memory");
  372. kernel_fpu_end();
  373. }
  374. static void
  375. xor_p5_mmx_5(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  376. unsigned long *p3, unsigned long *p4, unsigned long *p5)
  377. {
  378. unsigned long lines = bytes >> 6;
  379. kernel_fpu_begin();
  380. /* Make sure GCC forgets anything it knows about p4 or p5,
  381. such that it won't pass to the asm volatile below a
  382. register that is shared with any other variable. That's
  383. because we modify p4 and p5 there, but we can't mark them
  384. as read/write, otherwise we'd overflow the 10-asm-operands
  385. limit of GCC < 3.1. */
  386. asm("" : "+r" (p4), "+r" (p5));
  387. asm volatile(
  388. " .align 32,0x90 ;\n"
  389. " 1: ;\n"
  390. " movq (%1), %%mm0 ;\n"
  391. " movq 8(%1), %%mm1 ;\n"
  392. " pxor (%2), %%mm0 ;\n"
  393. " pxor 8(%2), %%mm1 ;\n"
  394. " movq 16(%1), %%mm2 ;\n"
  395. " pxor (%3), %%mm0 ;\n"
  396. " pxor 8(%3), %%mm1 ;\n"
  397. " pxor 16(%2), %%mm2 ;\n"
  398. " pxor (%4), %%mm0 ;\n"
  399. " pxor 8(%4), %%mm1 ;\n"
  400. " pxor 16(%3), %%mm2 ;\n"
  401. " movq 24(%1), %%mm3 ;\n"
  402. " pxor (%5), %%mm0 ;\n"
  403. " pxor 8(%5), %%mm1 ;\n"
  404. " movq %%mm0, (%1) ;\n"
  405. " pxor 16(%4), %%mm2 ;\n"
  406. " pxor 24(%2), %%mm3 ;\n"
  407. " movq %%mm1, 8(%1) ;\n"
  408. " pxor 16(%5), %%mm2 ;\n"
  409. " pxor 24(%3), %%mm3 ;\n"
  410. " movq 32(%1), %%mm4 ;\n"
  411. " movq %%mm2, 16(%1) ;\n"
  412. " pxor 24(%4), %%mm3 ;\n"
  413. " pxor 32(%2), %%mm4 ;\n"
  414. " movq 40(%1), %%mm5 ;\n"
  415. " pxor 24(%5), %%mm3 ;\n"
  416. " pxor 32(%3), %%mm4 ;\n"
  417. " pxor 40(%2), %%mm5 ;\n"
  418. " movq %%mm3, 24(%1) ;\n"
  419. " pxor 32(%4), %%mm4 ;\n"
  420. " pxor 40(%3), %%mm5 ;\n"
  421. " movq 48(%1), %%mm6 ;\n"
  422. " movq 56(%1), %%mm7 ;\n"
  423. " pxor 32(%5), %%mm4 ;\n"
  424. " pxor 40(%4), %%mm5 ;\n"
  425. " pxor 48(%2), %%mm6 ;\n"
  426. " pxor 56(%2), %%mm7 ;\n"
  427. " movq %%mm4, 32(%1) ;\n"
  428. " pxor 48(%3), %%mm6 ;\n"
  429. " pxor 56(%3), %%mm7 ;\n"
  430. " pxor 40(%5), %%mm5 ;\n"
  431. " pxor 48(%4), %%mm6 ;\n"
  432. " pxor 56(%4), %%mm7 ;\n"
  433. " movq %%mm5, 40(%1) ;\n"
  434. " pxor 48(%5), %%mm6 ;\n"
  435. " pxor 56(%5), %%mm7 ;\n"
  436. " movq %%mm6, 48(%1) ;\n"
  437. " movq %%mm7, 56(%1) ;\n"
  438. " addl $64, %1 ;\n"
  439. " addl $64, %2 ;\n"
  440. " addl $64, %3 ;\n"
  441. " addl $64, %4 ;\n"
  442. " addl $64, %5 ;\n"
  443. " decl %0 ;\n"
  444. " jnz 1b ;\n"
  445. : "+r" (lines),
  446. "+r" (p1), "+r" (p2), "+r" (p3)
  447. : "r" (p4), "r" (p5)
  448. : "memory");
  449. /* p4 and p5 were modified, and now the variables are dead.
  450. Clobber them just to be sure nobody does something stupid
  451. like assuming they have some legal value. */
  452. asm("" : "=r" (p4), "=r" (p5));
  453. kernel_fpu_end();
  454. }
  455. static struct xor_block_template xor_block_pII_mmx = {
  456. .name = "pII_mmx",
  457. .do_2 = xor_pII_mmx_2,
  458. .do_3 = xor_pII_mmx_3,
  459. .do_4 = xor_pII_mmx_4,
  460. .do_5 = xor_pII_mmx_5,
  461. };
  462. static struct xor_block_template xor_block_p5_mmx = {
  463. .name = "p5_mmx",
  464. .do_2 = xor_p5_mmx_2,
  465. .do_3 = xor_p5_mmx_3,
  466. .do_4 = xor_p5_mmx_4,
  467. .do_5 = xor_p5_mmx_5,
  468. };
  469. /*
  470. * Cache avoiding checksumming functions utilizing KNI instructions
  471. * Copyright (C) 1999 Zach Brown (with obvious credit due Ingo)
  472. */
  473. #define XMMS_SAVE \
  474. do { \
  475. preempt_disable(); \
  476. cr0 = read_cr0(); \
  477. clts(); \
  478. asm volatile( \
  479. "movups %%xmm0,(%0) ;\n\t" \
  480. "movups %%xmm1,0x10(%0) ;\n\t" \
  481. "movups %%xmm2,0x20(%0) ;\n\t" \
  482. "movups %%xmm3,0x30(%0) ;\n\t" \
  483. : \
  484. : "r" (xmm_save) \
  485. : "memory"); \
  486. } while (0)
  487. #define XMMS_RESTORE \
  488. do { \
  489. asm volatile( \
  490. "sfence ;\n\t" \
  491. "movups (%0),%%xmm0 ;\n\t" \
  492. "movups 0x10(%0),%%xmm1 ;\n\t" \
  493. "movups 0x20(%0),%%xmm2 ;\n\t" \
  494. "movups 0x30(%0),%%xmm3 ;\n\t" \
  495. : \
  496. : "r" (xmm_save) \
  497. : "memory"); \
  498. write_cr0(cr0); \
  499. preempt_enable(); \
  500. } while (0)
  501. #define ALIGN16 __attribute__((aligned(16)))
  502. #define OFFS(x) "16*("#x")"
  503. #define PF_OFFS(x) "256+16*("#x")"
  504. #define PF0(x) " prefetchnta "PF_OFFS(x)"(%1) ;\n"
  505. #define LD(x, y) " movaps "OFFS(x)"(%1), %%xmm"#y" ;\n"
  506. #define ST(x, y) " movaps %%xmm"#y", "OFFS(x)"(%1) ;\n"
  507. #define PF1(x) " prefetchnta "PF_OFFS(x)"(%2) ;\n"
  508. #define PF2(x) " prefetchnta "PF_OFFS(x)"(%3) ;\n"
  509. #define PF3(x) " prefetchnta "PF_OFFS(x)"(%4) ;\n"
  510. #define PF4(x) " prefetchnta "PF_OFFS(x)"(%5) ;\n"
  511. #define PF5(x) " prefetchnta "PF_OFFS(x)"(%6) ;\n"
  512. #define XO1(x, y) " xorps "OFFS(x)"(%2), %%xmm"#y" ;\n"
  513. #define XO2(x, y) " xorps "OFFS(x)"(%3), %%xmm"#y" ;\n"
  514. #define XO3(x, y) " xorps "OFFS(x)"(%4), %%xmm"#y" ;\n"
  515. #define XO4(x, y) " xorps "OFFS(x)"(%5), %%xmm"#y" ;\n"
  516. #define XO5(x, y) " xorps "OFFS(x)"(%6), %%xmm"#y" ;\n"
  517. static void
  518. xor_sse_2(unsigned long bytes, unsigned long *p1, unsigned long *p2)
  519. {
  520. unsigned long lines = bytes >> 8;
  521. char xmm_save[16*4] ALIGN16;
  522. int cr0;
  523. XMMS_SAVE;
  524. asm volatile(
  525. #undef BLOCK
  526. #define BLOCK(i) \
  527. LD(i, 0) \
  528. LD(i + 1, 1) \
  529. PF1(i) \
  530. PF1(i + 2) \
  531. LD(i + 2, 2) \
  532. LD(i + 3, 3) \
  533. PF0(i + 4) \
  534. PF0(i + 6) \
  535. XO1(i, 0) \
  536. XO1(i + 1, 1) \
  537. XO1(i + 2, 2) \
  538. XO1(i + 3, 3) \
  539. ST(i, 0) \
  540. ST(i + 1, 1) \
  541. ST(i + 2, 2) \
  542. ST(i + 3, 3) \
  543. PF0(0)
  544. PF0(2)
  545. " .align 32 ;\n"
  546. " 1: ;\n"
  547. BLOCK(0)
  548. BLOCK(4)
  549. BLOCK(8)
  550. BLOCK(12)
  551. " addl $256, %1 ;\n"
  552. " addl $256, %2 ;\n"
  553. " decl %0 ;\n"
  554. " jnz 1b ;\n"
  555. : "+r" (lines),
  556. "+r" (p1), "+r" (p2)
  557. :
  558. : "memory");
  559. XMMS_RESTORE;
  560. }
  561. static void
  562. xor_sse_3(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  563. unsigned long *p3)
  564. {
  565. unsigned long lines = bytes >> 8;
  566. char xmm_save[16*4] ALIGN16;
  567. int cr0;
  568. XMMS_SAVE;
  569. asm volatile(
  570. #undef BLOCK
  571. #define BLOCK(i) \
  572. PF1(i) \
  573. PF1(i + 2) \
  574. LD(i,0) \
  575. LD(i + 1, 1) \
  576. LD(i + 2, 2) \
  577. LD(i + 3, 3) \
  578. PF2(i) \
  579. PF2(i + 2) \
  580. PF0(i + 4) \
  581. PF0(i + 6) \
  582. XO1(i,0) \
  583. XO1(i + 1, 1) \
  584. XO1(i + 2, 2) \
  585. XO1(i + 3, 3) \
  586. XO2(i,0) \
  587. XO2(i + 1, 1) \
  588. XO2(i + 2, 2) \
  589. XO2(i + 3, 3) \
  590. ST(i,0) \
  591. ST(i + 1, 1) \
  592. ST(i + 2, 2) \
  593. ST(i + 3, 3) \
  594. PF0(0)
  595. PF0(2)
  596. " .align 32 ;\n"
  597. " 1: ;\n"
  598. BLOCK(0)
  599. BLOCK(4)
  600. BLOCK(8)
  601. BLOCK(12)
  602. " addl $256, %1 ;\n"
  603. " addl $256, %2 ;\n"
  604. " addl $256, %3 ;\n"
  605. " decl %0 ;\n"
  606. " jnz 1b ;\n"
  607. : "+r" (lines),
  608. "+r" (p1), "+r"(p2), "+r"(p3)
  609. :
  610. : "memory" );
  611. XMMS_RESTORE;
  612. }
  613. static void
  614. xor_sse_4(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  615. unsigned long *p3, unsigned long *p4)
  616. {
  617. unsigned long lines = bytes >> 8;
  618. char xmm_save[16*4] ALIGN16;
  619. int cr0;
  620. XMMS_SAVE;
  621. asm volatile(
  622. #undef BLOCK
  623. #define BLOCK(i) \
  624. PF1(i) \
  625. PF1(i + 2) \
  626. LD(i,0) \
  627. LD(i + 1, 1) \
  628. LD(i + 2, 2) \
  629. LD(i + 3, 3) \
  630. PF2(i) \
  631. PF2(i + 2) \
  632. XO1(i,0) \
  633. XO1(i + 1, 1) \
  634. XO1(i + 2, 2) \
  635. XO1(i + 3, 3) \
  636. PF3(i) \
  637. PF3(i + 2) \
  638. PF0(i + 4) \
  639. PF0(i + 6) \
  640. XO2(i,0) \
  641. XO2(i + 1, 1) \
  642. XO2(i + 2, 2) \
  643. XO2(i + 3, 3) \
  644. XO3(i,0) \
  645. XO3(i + 1, 1) \
  646. XO3(i + 2, 2) \
  647. XO3(i + 3, 3) \
  648. ST(i,0) \
  649. ST(i + 1, 1) \
  650. ST(i + 2, 2) \
  651. ST(i + 3, 3) \
  652. PF0(0)
  653. PF0(2)
  654. " .align 32 ;\n"
  655. " 1: ;\n"
  656. BLOCK(0)
  657. BLOCK(4)
  658. BLOCK(8)
  659. BLOCK(12)
  660. " addl $256, %1 ;\n"
  661. " addl $256, %2 ;\n"
  662. " addl $256, %3 ;\n"
  663. " addl $256, %4 ;\n"
  664. " decl %0 ;\n"
  665. " jnz 1b ;\n"
  666. : "+r" (lines),
  667. "+r" (p1), "+r" (p2), "+r" (p3), "+r" (p4)
  668. :
  669. : "memory" );
  670. XMMS_RESTORE;
  671. }
  672. static void
  673. xor_sse_5(unsigned long bytes, unsigned long *p1, unsigned long *p2,
  674. unsigned long *p3, unsigned long *p4, unsigned long *p5)
  675. {
  676. unsigned long lines = bytes >> 8;
  677. char xmm_save[16*4] ALIGN16;
  678. int cr0;
  679. XMMS_SAVE;
  680. /* Make sure GCC forgets anything it knows about p4 or p5,
  681. such that it won't pass to the asm volatile below a
  682. register that is shared with any other variable. That's
  683. because we modify p4 and p5 there, but we can't mark them
  684. as read/write, otherwise we'd overflow the 10-asm-operands
  685. limit of GCC < 3.1. */
  686. asm("" : "+r" (p4), "+r" (p5));
  687. asm volatile(
  688. #undef BLOCK
  689. #define BLOCK(i) \
  690. PF1(i) \
  691. PF1(i + 2) \
  692. LD(i,0) \
  693. LD(i + 1, 1) \
  694. LD(i + 2, 2) \
  695. LD(i + 3, 3) \
  696. PF2(i) \
  697. PF2(i + 2) \
  698. XO1(i,0) \
  699. XO1(i + 1, 1) \
  700. XO1(i + 2, 2) \
  701. XO1(i + 3, 3) \
  702. PF3(i) \
  703. PF3(i + 2) \
  704. XO2(i,0) \
  705. XO2(i + 1, 1) \
  706. XO2(i + 2, 2) \
  707. XO2(i + 3, 3) \
  708. PF4(i) \
  709. PF4(i + 2) \
  710. PF0(i + 4) \
  711. PF0(i + 6) \
  712. XO3(i,0) \
  713. XO3(i + 1, 1) \
  714. XO3(i + 2, 2) \
  715. XO3(i + 3, 3) \
  716. XO4(i,0) \
  717. XO4(i + 1, 1) \
  718. XO4(i + 2, 2) \
  719. XO4(i + 3, 3) \
  720. ST(i,0) \
  721. ST(i + 1, 1) \
  722. ST(i + 2, 2) \
  723. ST(i + 3, 3) \
  724. PF0(0)
  725. PF0(2)
  726. " .align 32 ;\n"
  727. " 1: ;\n"
  728. BLOCK(0)
  729. BLOCK(4)
  730. BLOCK(8)
  731. BLOCK(12)
  732. " addl $256, %1 ;\n"
  733. " addl $256, %2 ;\n"
  734. " addl $256, %3 ;\n"
  735. " addl $256, %4 ;\n"
  736. " addl $256, %5 ;\n"
  737. " decl %0 ;\n"
  738. " jnz 1b ;\n"
  739. : "+r" (lines),
  740. "+r" (p1), "+r" (p2), "+r" (p3)
  741. : "r" (p4), "r" (p5)
  742. : "memory");
  743. /* p4 and p5 were modified, and now the variables are dead.
  744. Clobber them just to be sure nobody does something stupid
  745. like assuming they have some legal value. */
  746. asm("" : "=r" (p4), "=r" (p5));
  747. XMMS_RESTORE;
  748. }
  749. static struct xor_block_template xor_block_pIII_sse = {
  750. .name = "pIII_sse",
  751. .do_2 = xor_sse_2,
  752. .do_3 = xor_sse_3,
  753. .do_4 = xor_sse_4,
  754. .do_5 = xor_sse_5,
  755. };
  756. /* Also try the generic routines. */
  757. #include <asm-generic/xor.h>
  758. #undef XOR_TRY_TEMPLATES
  759. #define XOR_TRY_TEMPLATES \
  760. do { \
  761. xor_speed(&xor_block_8regs); \
  762. xor_speed(&xor_block_8regs_p); \
  763. xor_speed(&xor_block_32regs); \
  764. xor_speed(&xor_block_32regs_p); \
  765. if (cpu_has_xmm) \
  766. xor_speed(&xor_block_pIII_sse); \
  767. if (cpu_has_mmx) { \
  768. xor_speed(&xor_block_pII_mmx); \
  769. xor_speed(&xor_block_p5_mmx); \
  770. } \
  771. } while (0)
  772. /* We force the use of the SSE xor block because it can write around L2.
  773. We may also be able to load into the L1 only depending on how the cpu
  774. deals with a load to a line that is being prefetched. */
  775. #define XOR_SELECT_TEMPLATE(FASTEST) \
  776. (cpu_has_xmm ? &xor_block_pIII_sse : FASTEST)