2
0

ghash-sparcv9.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #! /usr/bin/env perl
  2. # Copyright 2010-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # March 2010
  15. #
  16. # The module implements "4-bit" GCM GHASH function and underlying
  17. # single multiplication operation in GF(2^128). "4-bit" means that it
  18. # uses 256 bytes per-key table [+128 bytes shared table]. Performance
  19. # results are for streamed GHASH subroutine on UltraSPARC pre-Tx CPU
  20. # and are expressed in cycles per processed byte, less is better:
  21. #
  22. # gcc 3.3.x cc 5.2 this assembler
  23. #
  24. # 32-bit build 81.4 43.3 12.6 (+546%/+244%)
  25. # 64-bit build 20.2 21.2 12.6 (+60%/+68%)
  26. #
  27. # Here is data collected on UltraSPARC T1 system running Linux:
  28. #
  29. # gcc 4.4.1 this assembler
  30. #
  31. # 32-bit build 566 50 (+1000%)
  32. # 64-bit build 56 50 (+12%)
  33. #
  34. # I don't quite understand why difference between 32-bit and 64-bit
  35. # compiler-generated code is so big. Compilers *were* instructed to
  36. # generate code for UltraSPARC and should have used 64-bit registers
  37. # for Z vector (see C code) even in 32-bit build... Oh well, it only
  38. # means more impressive improvement coefficients for this assembler
  39. # module;-) Loops are aggressively modulo-scheduled in respect to
  40. # references to input data and Z.hi updates to achieve 12 cycles
  41. # timing. To anchor to something else, sha1-sparcv9.pl spends 11.6
  42. # cycles to process one byte on UltraSPARC pre-Tx CPU and ~24 on T1.
  43. #
  44. # October 2012
  45. #
  46. # Add VIS3 lookup-table-free implementation using polynomial
  47. # multiplication xmulx[hi] and extended addition addxc[cc]
  48. # instructions. 4.52/7.63x improvement on T3/T4 or in absolute
  49. # terms 7.90/2.14 cycles per byte. On T4 multi-process benchmark
  50. # saturates at ~15.5x single-process result on 8-core processor,
  51. # or ~20.5GBps per 2.85GHz socket.
  52. $output=pop;
  53. open STDOUT,">$output";
  54. $frame="STACK_FRAME";
  55. $bias="STACK_BIAS";
  56. $Zhi="%o0"; # 64-bit values
  57. $Zlo="%o1";
  58. $Thi="%o2";
  59. $Tlo="%o3";
  60. $rem="%o4";
  61. $tmp="%o5";
  62. $nhi="%l0"; # small values and pointers
  63. $nlo="%l1";
  64. $xi0="%l2";
  65. $xi1="%l3";
  66. $rem_4bit="%l4";
  67. $remi="%l5";
  68. $Htblo="%l6";
  69. $cnt="%l7";
  70. $Xi="%i0"; # input argument block
  71. $Htbl="%i1";
  72. $inp="%i2";
  73. $len="%i3";
  74. $code.=<<___;
  75. #include "sparc_arch.h"
  76. #ifdef __arch64__
  77. .register %g2,#scratch
  78. .register %g3,#scratch
  79. #endif
  80. .section ".text",#alloc,#execinstr
  81. .align 64
  82. rem_4bit:
  83. .long `0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`,0
  84. .long `0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`,0
  85. .long `0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`,0
  86. .long `0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`,0
  87. .type rem_4bit,#object
  88. .size rem_4bit,(.-rem_4bit)
  89. .globl gcm_ghash_4bit
  90. .align 32
  91. gcm_ghash_4bit:
  92. save %sp,-$frame,%sp
  93. ldub [$inp+15],$nlo
  94. ldub [$Xi+15],$xi0
  95. ldub [$Xi+14],$xi1
  96. add $len,$inp,$len
  97. add $Htbl,8,$Htblo
  98. 1: call .+8
  99. add %o7,rem_4bit-1b,$rem_4bit
  100. .Louter:
  101. xor $xi0,$nlo,$nlo
  102. and $nlo,0xf0,$nhi
  103. and $nlo,0x0f,$nlo
  104. sll $nlo,4,$nlo
  105. ldx [$Htblo+$nlo],$Zlo
  106. ldx [$Htbl+$nlo],$Zhi
  107. ldub [$inp+14],$nlo
  108. ldx [$Htblo+$nhi],$Tlo
  109. and $Zlo,0xf,$remi
  110. ldx [$Htbl+$nhi],$Thi
  111. sll $remi,3,$remi
  112. ldx [$rem_4bit+$remi],$rem
  113. srlx $Zlo,4,$Zlo
  114. mov 13,$cnt
  115. sllx $Zhi,60,$tmp
  116. xor $Tlo,$Zlo,$Zlo
  117. srlx $Zhi,4,$Zhi
  118. xor $Zlo,$tmp,$Zlo
  119. xor $xi1,$nlo,$nlo
  120. and $Zlo,0xf,$remi
  121. and $nlo,0xf0,$nhi
  122. and $nlo,0x0f,$nlo
  123. ba .Lghash_inner
  124. sll $nlo,4,$nlo
  125. .align 32
  126. .Lghash_inner:
  127. ldx [$Htblo+$nlo],$Tlo
  128. sll $remi,3,$remi
  129. xor $Thi,$Zhi,$Zhi
  130. ldx [$Htbl+$nlo],$Thi
  131. srlx $Zlo,4,$Zlo
  132. xor $rem,$Zhi,$Zhi
  133. ldx [$rem_4bit+$remi],$rem
  134. sllx $Zhi,60,$tmp
  135. xor $Tlo,$Zlo,$Zlo
  136. ldub [$inp+$cnt],$nlo
  137. srlx $Zhi,4,$Zhi
  138. xor $Zlo,$tmp,$Zlo
  139. ldub [$Xi+$cnt],$xi1
  140. xor $Thi,$Zhi,$Zhi
  141. and $Zlo,0xf,$remi
  142. ldx [$Htblo+$nhi],$Tlo
  143. sll $remi,3,$remi
  144. xor $rem,$Zhi,$Zhi
  145. ldx [$Htbl+$nhi],$Thi
  146. srlx $Zlo,4,$Zlo
  147. ldx [$rem_4bit+$remi],$rem
  148. sllx $Zhi,60,$tmp
  149. xor $xi1,$nlo,$nlo
  150. srlx $Zhi,4,$Zhi
  151. and $nlo,0xf0,$nhi
  152. addcc $cnt,-1,$cnt
  153. xor $Zlo,$tmp,$Zlo
  154. and $nlo,0x0f,$nlo
  155. xor $Tlo,$Zlo,$Zlo
  156. sll $nlo,4,$nlo
  157. blu .Lghash_inner
  158. and $Zlo,0xf,$remi
  159. ldx [$Htblo+$nlo],$Tlo
  160. sll $remi,3,$remi
  161. xor $Thi,$Zhi,$Zhi
  162. ldx [$Htbl+$nlo],$Thi
  163. srlx $Zlo,4,$Zlo
  164. xor $rem,$Zhi,$Zhi
  165. ldx [$rem_4bit+$remi],$rem
  166. sllx $Zhi,60,$tmp
  167. xor $Tlo,$Zlo,$Zlo
  168. srlx $Zhi,4,$Zhi
  169. xor $Zlo,$tmp,$Zlo
  170. xor $Thi,$Zhi,$Zhi
  171. add $inp,16,$inp
  172. cmp $inp,$len
  173. be,pn SIZE_T_CC,.Ldone
  174. and $Zlo,0xf,$remi
  175. ldx [$Htblo+$nhi],$Tlo
  176. sll $remi,3,$remi
  177. xor $rem,$Zhi,$Zhi
  178. ldx [$Htbl+$nhi],$Thi
  179. srlx $Zlo,4,$Zlo
  180. ldx [$rem_4bit+$remi],$rem
  181. sllx $Zhi,60,$tmp
  182. xor $Tlo,$Zlo,$Zlo
  183. ldub [$inp+15],$nlo
  184. srlx $Zhi,4,$Zhi
  185. xor $Zlo,$tmp,$Zlo
  186. xor $Thi,$Zhi,$Zhi
  187. stx $Zlo,[$Xi+8]
  188. xor $rem,$Zhi,$Zhi
  189. stx $Zhi,[$Xi]
  190. srl $Zlo,8,$xi1
  191. and $Zlo,0xff,$xi0
  192. ba .Louter
  193. and $xi1,0xff,$xi1
  194. .align 32
  195. .Ldone:
  196. ldx [$Htblo+$nhi],$Tlo
  197. sll $remi,3,$remi
  198. xor $rem,$Zhi,$Zhi
  199. ldx [$Htbl+$nhi],$Thi
  200. srlx $Zlo,4,$Zlo
  201. ldx [$rem_4bit+$remi],$rem
  202. sllx $Zhi,60,$tmp
  203. xor $Tlo,$Zlo,$Zlo
  204. srlx $Zhi,4,$Zhi
  205. xor $Zlo,$tmp,$Zlo
  206. xor $Thi,$Zhi,$Zhi
  207. stx $Zlo,[$Xi+8]
  208. xor $rem,$Zhi,$Zhi
  209. stx $Zhi,[$Xi]
  210. ret
  211. restore
  212. .type gcm_ghash_4bit,#function
  213. .size gcm_ghash_4bit,(.-gcm_ghash_4bit)
  214. ___
  215. undef $inp;
  216. undef $len;
  217. $code.=<<___;
  218. .globl gcm_gmult_4bit
  219. .align 32
  220. gcm_gmult_4bit:
  221. save %sp,-$frame,%sp
  222. ldub [$Xi+15],$nlo
  223. add $Htbl,8,$Htblo
  224. 1: call .+8
  225. add %o7,rem_4bit-1b,$rem_4bit
  226. and $nlo,0xf0,$nhi
  227. and $nlo,0x0f,$nlo
  228. sll $nlo,4,$nlo
  229. ldx [$Htblo+$nlo],$Zlo
  230. ldx [$Htbl+$nlo],$Zhi
  231. ldub [$Xi+14],$nlo
  232. ldx [$Htblo+$nhi],$Tlo
  233. and $Zlo,0xf,$remi
  234. ldx [$Htbl+$nhi],$Thi
  235. sll $remi,3,$remi
  236. ldx [$rem_4bit+$remi],$rem
  237. srlx $Zlo,4,$Zlo
  238. mov 13,$cnt
  239. sllx $Zhi,60,$tmp
  240. xor $Tlo,$Zlo,$Zlo
  241. srlx $Zhi,4,$Zhi
  242. xor $Zlo,$tmp,$Zlo
  243. and $Zlo,0xf,$remi
  244. and $nlo,0xf0,$nhi
  245. and $nlo,0x0f,$nlo
  246. ba .Lgmult_inner
  247. sll $nlo,4,$nlo
  248. .align 32
  249. .Lgmult_inner:
  250. ldx [$Htblo+$nlo],$Tlo
  251. sll $remi,3,$remi
  252. xor $Thi,$Zhi,$Zhi
  253. ldx [$Htbl+$nlo],$Thi
  254. srlx $Zlo,4,$Zlo
  255. xor $rem,$Zhi,$Zhi
  256. ldx [$rem_4bit+$remi],$rem
  257. sllx $Zhi,60,$tmp
  258. xor $Tlo,$Zlo,$Zlo
  259. ldub [$Xi+$cnt],$nlo
  260. srlx $Zhi,4,$Zhi
  261. xor $Zlo,$tmp,$Zlo
  262. xor $Thi,$Zhi,$Zhi
  263. and $Zlo,0xf,$remi
  264. ldx [$Htblo+$nhi],$Tlo
  265. sll $remi,3,$remi
  266. xor $rem,$Zhi,$Zhi
  267. ldx [$Htbl+$nhi],$Thi
  268. srlx $Zlo,4,$Zlo
  269. ldx [$rem_4bit+$remi],$rem
  270. sllx $Zhi,60,$tmp
  271. srlx $Zhi,4,$Zhi
  272. and $nlo,0xf0,$nhi
  273. addcc $cnt,-1,$cnt
  274. xor $Zlo,$tmp,$Zlo
  275. and $nlo,0x0f,$nlo
  276. xor $Tlo,$Zlo,$Zlo
  277. sll $nlo,4,$nlo
  278. blu .Lgmult_inner
  279. and $Zlo,0xf,$remi
  280. ldx [$Htblo+$nlo],$Tlo
  281. sll $remi,3,$remi
  282. xor $Thi,$Zhi,$Zhi
  283. ldx [$Htbl+$nlo],$Thi
  284. srlx $Zlo,4,$Zlo
  285. xor $rem,$Zhi,$Zhi
  286. ldx [$rem_4bit+$remi],$rem
  287. sllx $Zhi,60,$tmp
  288. xor $Tlo,$Zlo,$Zlo
  289. srlx $Zhi,4,$Zhi
  290. xor $Zlo,$tmp,$Zlo
  291. xor $Thi,$Zhi,$Zhi
  292. and $Zlo,0xf,$remi
  293. ldx [$Htblo+$nhi],$Tlo
  294. sll $remi,3,$remi
  295. xor $rem,$Zhi,$Zhi
  296. ldx [$Htbl+$nhi],$Thi
  297. srlx $Zlo,4,$Zlo
  298. ldx [$rem_4bit+$remi],$rem
  299. sllx $Zhi,60,$tmp
  300. xor $Tlo,$Zlo,$Zlo
  301. srlx $Zhi,4,$Zhi
  302. xor $Zlo,$tmp,$Zlo
  303. xor $Thi,$Zhi,$Zhi
  304. stx $Zlo,[$Xi+8]
  305. xor $rem,$Zhi,$Zhi
  306. stx $Zhi,[$Xi]
  307. ret
  308. restore
  309. .type gcm_gmult_4bit,#function
  310. .size gcm_gmult_4bit,(.-gcm_gmult_4bit)
  311. ___
  312. {{{
  313. # Straightforward 128x128-bit multiplication using Karatsuba algorithm
  314. # followed by pair of 64-bit reductions [with a shortcut in first one,
  315. # which allowed to break dependency between reductions and remove one
  316. # multiplication from critical path]. While it might be suboptimal
  317. # with regard to sheer number of multiplications, other methods [such
  318. # as aggregate reduction] would require more 64-bit registers, which
  319. # we don't have in 32-bit application context.
  320. ($Xip,$Htable,$inp,$len)=map("%i$_",(0..3));
  321. ($Hhl,$Hlo,$Hhi,$Xlo,$Xhi,$xE1,$sqr, $C0,$C1,$C2,$C3,$V)=
  322. (map("%o$_",(0..5,7)),map("%g$_",(1..5)));
  323. ($shl,$shr)=map("%l$_",(0..7));
  324. # For details regarding "twisted H" see ghash-x86.pl.
  325. $code.=<<___;
  326. .globl gcm_init_vis3
  327. .align 32
  328. gcm_init_vis3:
  329. save %sp,-$frame,%sp
  330. ldx [%i1+0],$Hhi
  331. ldx [%i1+8],$Hlo
  332. mov 0xE1,$Xhi
  333. mov 1,$Xlo
  334. sllx $Xhi,57,$Xhi
  335. srax $Hhi,63,$C0 ! broadcast carry
  336. addcc $Hlo,$Hlo,$Hlo ! H<<=1
  337. addxc $Hhi,$Hhi,$Hhi
  338. and $C0,$Xlo,$Xlo
  339. and $C0,$Xhi,$Xhi
  340. xor $Xlo,$Hlo,$Hlo
  341. xor $Xhi,$Hhi,$Hhi
  342. stx $Hlo,[%i0+8] ! save twisted H
  343. stx $Hhi,[%i0+0]
  344. sethi %hi(0xA0406080),$V
  345. sethi %hi(0x20C0E000),%l0
  346. or $V,%lo(0xA0406080),$V
  347. or %l0,%lo(0x20C0E000),%l0
  348. sllx $V,32,$V
  349. or %l0,$V,$V ! (0xE0·i)&0xff=0xA040608020C0E000
  350. stx $V,[%i0+16]
  351. ret
  352. restore
  353. .type gcm_init_vis3,#function
  354. .size gcm_init_vis3,.-gcm_init_vis3
  355. .globl gcm_gmult_vis3
  356. .align 32
  357. gcm_gmult_vis3:
  358. save %sp,-$frame,%sp
  359. ldx [$Xip+8],$Xlo ! load Xi
  360. ldx [$Xip+0],$Xhi
  361. ldx [$Htable+8],$Hlo ! load twisted H
  362. ldx [$Htable+0],$Hhi
  363. mov 0xE1,%l7
  364. sllx %l7,57,$xE1 ! 57 is not a typo
  365. ldx [$Htable+16],$V ! (0xE0·i)&0xff=0xA040608020C0E000
  366. xor $Hhi,$Hlo,$Hhl ! Karatsuba pre-processing
  367. xmulx $Xlo,$Hlo,$C0
  368. xor $Xlo,$Xhi,$C2 ! Karatsuba pre-processing
  369. xmulx $C2,$Hhl,$C1
  370. xmulxhi $Xlo,$Hlo,$Xlo
  371. xmulxhi $C2,$Hhl,$C2
  372. xmulxhi $Xhi,$Hhi,$C3
  373. xmulx $Xhi,$Hhi,$Xhi
  374. sll $C0,3,$sqr
  375. srlx $V,$sqr,$sqr ! ·0xE0 [implicit &(7<<3)]
  376. xor $C0,$sqr,$sqr
  377. sllx $sqr,57,$sqr ! ($C0·0xE1)<<1<<56 [implicit &0x7f]
  378. xor $C0,$C1,$C1 ! Karatsuba post-processing
  379. xor $Xlo,$C2,$C2
  380. xor $sqr,$Xlo,$Xlo ! real destination is $C1
  381. xor $C3,$C2,$C2
  382. xor $Xlo,$C1,$C1
  383. xor $Xhi,$C2,$C2
  384. xor $Xhi,$C1,$C1
  385. xmulxhi $C0,$xE1,$Xlo ! ·0xE1<<1<<56
  386. xor $C0,$C2,$C2
  387. xmulx $C1,$xE1,$C0
  388. xor $C1,$C3,$C3
  389. xmulxhi $C1,$xE1,$C1
  390. xor $Xlo,$C2,$C2
  391. xor $C0,$C2,$C2
  392. xor $C1,$C3,$C3
  393. stx $C2,[$Xip+8] ! save Xi
  394. stx $C3,[$Xip+0]
  395. ret
  396. restore
  397. .type gcm_gmult_vis3,#function
  398. .size gcm_gmult_vis3,.-gcm_gmult_vis3
  399. .globl gcm_ghash_vis3
  400. .align 32
  401. gcm_ghash_vis3:
  402. save %sp,-$frame,%sp
  403. nop
  404. srln $len,0,$len ! needed on v8+, "nop" on v9
  405. ldx [$Xip+8],$C2 ! load Xi
  406. ldx [$Xip+0],$C3
  407. ldx [$Htable+8],$Hlo ! load twisted H
  408. ldx [$Htable+0],$Hhi
  409. mov 0xE1,%l7
  410. sllx %l7,57,$xE1 ! 57 is not a typo
  411. ldx [$Htable+16],$V ! (0xE0·i)&0xff=0xA040608020C0E000
  412. and $inp,7,$shl
  413. andn $inp,7,$inp
  414. sll $shl,3,$shl
  415. prefetch [$inp+63], 20
  416. sub %g0,$shl,$shr
  417. xor $Hhi,$Hlo,$Hhl ! Karatsuba pre-processing
  418. .Loop:
  419. ldx [$inp+8],$Xlo
  420. brz,pt $shl,1f
  421. ldx [$inp+0],$Xhi
  422. ldx [$inp+16],$C1 ! align data
  423. srlx $Xlo,$shr,$C0
  424. sllx $Xlo,$shl,$Xlo
  425. sllx $Xhi,$shl,$Xhi
  426. srlx $C1,$shr,$C1
  427. or $C0,$Xhi,$Xhi
  428. or $C1,$Xlo,$Xlo
  429. 1:
  430. add $inp,16,$inp
  431. sub $len,16,$len
  432. xor $C2,$Xlo,$Xlo
  433. xor $C3,$Xhi,$Xhi
  434. prefetch [$inp+63], 20
  435. xmulx $Xlo,$Hlo,$C0
  436. xor $Xlo,$Xhi,$C2 ! Karatsuba pre-processing
  437. xmulx $C2,$Hhl,$C1
  438. xmulxhi $Xlo,$Hlo,$Xlo
  439. xmulxhi $C2,$Hhl,$C2
  440. xmulxhi $Xhi,$Hhi,$C3
  441. xmulx $Xhi,$Hhi,$Xhi
  442. sll $C0,3,$sqr
  443. srlx $V,$sqr,$sqr ! ·0xE0 [implicit &(7<<3)]
  444. xor $C0,$sqr,$sqr
  445. sllx $sqr,57,$sqr ! ($C0·0xE1)<<1<<56 [implicit &0x7f]
  446. xor $C0,$C1,$C1 ! Karatsuba post-processing
  447. xor $Xlo,$C2,$C2
  448. xor $sqr,$Xlo,$Xlo ! real destination is $C1
  449. xor $C3,$C2,$C2
  450. xor $Xlo,$C1,$C1
  451. xor $Xhi,$C2,$C2
  452. xor $Xhi,$C1,$C1
  453. xmulxhi $C0,$xE1,$Xlo ! ·0xE1<<1<<56
  454. xor $C0,$C2,$C2
  455. xmulx $C1,$xE1,$C0
  456. xor $C1,$C3,$C3
  457. xmulxhi $C1,$xE1,$C1
  458. xor $Xlo,$C2,$C2
  459. xor $C0,$C2,$C2
  460. brnz,pt $len,.Loop
  461. xor $C1,$C3,$C3
  462. stx $C2,[$Xip+8] ! save Xi
  463. stx $C3,[$Xip+0]
  464. ret
  465. restore
  466. .type gcm_ghash_vis3,#function
  467. .size gcm_ghash_vis3,.-gcm_ghash_vis3
  468. ___
  469. }}}
  470. $code.=<<___;
  471. .asciz "GHASH for SPARCv9/VIS3, CRYPTOGAMS by <appro\@openssl.org>"
  472. .align 4
  473. ___
  474. # Purpose of these subroutines is to explicitly encode VIS instructions,
  475. # so that one can compile the module without having to specify VIS
  476. # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
  477. # Idea is to reserve for option to produce "universal" binary and let
  478. # programmer detect if current CPU is VIS capable at run-time.
  479. sub unvis3 {
  480. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  481. my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
  482. my ($ref,$opf);
  483. my %visopf = ( "addxc" => 0x011,
  484. "addxccc" => 0x013,
  485. "xmulx" => 0x115,
  486. "xmulxhi" => 0x116 );
  487. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  488. if ($opf=$visopf{$mnemonic}) {
  489. foreach ($rs1,$rs2,$rd) {
  490. return $ref if (!/%([goli])([0-9])/);
  491. $_=$bias{$1}+$2;
  492. }
  493. return sprintf ".word\t0x%08x !%s",
  494. 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
  495. $ref;
  496. } else {
  497. return $ref;
  498. }
  499. }
  500. foreach (split("\n",$code)) {
  501. s/\`([^\`]*)\`/eval $1/ge;
  502. s/\b(xmulx[hi]*|addxc[c]{0,2})\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
  503. &unvis3($1,$2,$3,$4)
  504. /ge;
  505. print $_,"\n";
  506. }
  507. close STDOUT or die "error closing STDOUT: $!";