bn_mul.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /**
  2. * \file bn_mul.h
  3. *
  4. * \brief Multi-precision integer library
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. /*
  26. * Multiply source vector [s] with b, add result
  27. * to destination vector [d] and set carry c.
  28. *
  29. * Currently supports:
  30. *
  31. * . IA-32 (386+) . AMD64 / EM64T
  32. * . IA-32 (SSE2) . Motorola 68000
  33. * . PowerPC, 32-bit . MicroBlaze
  34. * . PowerPC, 64-bit . TriCore
  35. * . SPARC v8 . ARM v3+
  36. * . Alpha . MIPS32
  37. * . C, longlong . C, generic
  38. */
  39. #ifndef MBEDTLS_BN_MUL_H
  40. #define MBEDTLS_BN_MUL_H
  41. #include "bignum.h"
  42. #if defined(MBEDTLS_HAVE_ASM)
  43. #ifndef asm
  44. #define asm __asm
  45. #endif
  46. /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
  47. #if defined(__GNUC__) && \
  48. ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 )
  49. #if defined(__i386__)
  50. #define MULADDC_INIT \
  51. asm( \
  52. "movl %%ebx, %0 \n\t" \
  53. "movl %5, %%esi \n\t" \
  54. "movl %6, %%edi \n\t" \
  55. "movl %7, %%ecx \n\t" \
  56. "movl %8, %%ebx \n\t"
  57. #define MULADDC_CORE \
  58. "lodsl \n\t" \
  59. "mull %%ebx \n\t" \
  60. "addl %%ecx, %%eax \n\t" \
  61. "adcl $0, %%edx \n\t" \
  62. "addl (%%edi), %%eax \n\t" \
  63. "adcl $0, %%edx \n\t" \
  64. "movl %%edx, %%ecx \n\t" \
  65. "stosl \n\t"
  66. #if defined(MBEDTLS_HAVE_SSE2)
  67. #define MULADDC_HUIT \
  68. "movd %%ecx, %%mm1 \n\t" \
  69. "movd %%ebx, %%mm0 \n\t" \
  70. "movd (%%edi), %%mm3 \n\t" \
  71. "paddq %%mm3, %%mm1 \n\t" \
  72. "movd (%%esi), %%mm2 \n\t" \
  73. "pmuludq %%mm0, %%mm2 \n\t" \
  74. "movd 4(%%esi), %%mm4 \n\t" \
  75. "pmuludq %%mm0, %%mm4 \n\t" \
  76. "movd 8(%%esi), %%mm6 \n\t" \
  77. "pmuludq %%mm0, %%mm6 \n\t" \
  78. "movd 12(%%esi), %%mm7 \n\t" \
  79. "pmuludq %%mm0, %%mm7 \n\t" \
  80. "paddq %%mm2, %%mm1 \n\t" \
  81. "movd 4(%%edi), %%mm3 \n\t" \
  82. "paddq %%mm4, %%mm3 \n\t" \
  83. "movd 8(%%edi), %%mm5 \n\t" \
  84. "paddq %%mm6, %%mm5 \n\t" \
  85. "movd 12(%%edi), %%mm4 \n\t" \
  86. "paddq %%mm4, %%mm7 \n\t" \
  87. "movd %%mm1, (%%edi) \n\t" \
  88. "movd 16(%%esi), %%mm2 \n\t" \
  89. "pmuludq %%mm0, %%mm2 \n\t" \
  90. "psrlq $32, %%mm1 \n\t" \
  91. "movd 20(%%esi), %%mm4 \n\t" \
  92. "pmuludq %%mm0, %%mm4 \n\t" \
  93. "paddq %%mm3, %%mm1 \n\t" \
  94. "movd 24(%%esi), %%mm6 \n\t" \
  95. "pmuludq %%mm0, %%mm6 \n\t" \
  96. "movd %%mm1, 4(%%edi) \n\t" \
  97. "psrlq $32, %%mm1 \n\t" \
  98. "movd 28(%%esi), %%mm3 \n\t" \
  99. "pmuludq %%mm0, %%mm3 \n\t" \
  100. "paddq %%mm5, %%mm1 \n\t" \
  101. "movd 16(%%edi), %%mm5 \n\t" \
  102. "paddq %%mm5, %%mm2 \n\t" \
  103. "movd %%mm1, 8(%%edi) \n\t" \
  104. "psrlq $32, %%mm1 \n\t" \
  105. "paddq %%mm7, %%mm1 \n\t" \
  106. "movd 20(%%edi), %%mm5 \n\t" \
  107. "paddq %%mm5, %%mm4 \n\t" \
  108. "movd %%mm1, 12(%%edi) \n\t" \
  109. "psrlq $32, %%mm1 \n\t" \
  110. "paddq %%mm2, %%mm1 \n\t" \
  111. "movd 24(%%edi), %%mm5 \n\t" \
  112. "paddq %%mm5, %%mm6 \n\t" \
  113. "movd %%mm1, 16(%%edi) \n\t" \
  114. "psrlq $32, %%mm1 \n\t" \
  115. "paddq %%mm4, %%mm1 \n\t" \
  116. "movd 28(%%edi), %%mm5 \n\t" \
  117. "paddq %%mm5, %%mm3 \n\t" \
  118. "movd %%mm1, 20(%%edi) \n\t" \
  119. "psrlq $32, %%mm1 \n\t" \
  120. "paddq %%mm6, %%mm1 \n\t" \
  121. "movd %%mm1, 24(%%edi) \n\t" \
  122. "psrlq $32, %%mm1 \n\t" \
  123. "paddq %%mm3, %%mm1 \n\t" \
  124. "movd %%mm1, 28(%%edi) \n\t" \
  125. "addl $32, %%edi \n\t" \
  126. "addl $32, %%esi \n\t" \
  127. "psrlq $32, %%mm1 \n\t" \
  128. "movd %%mm1, %%ecx \n\t"
  129. #define MULADDC_STOP \
  130. "emms \n\t" \
  131. "movl %4, %%ebx \n\t" \
  132. "movl %%ecx, %1 \n\t" \
  133. "movl %%edi, %2 \n\t" \
  134. "movl %%esi, %3 \n\t" \
  135. : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
  136. : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
  137. : "eax", "ecx", "edx", "esi", "edi" \
  138. );
  139. #else
  140. #define MULADDC_STOP \
  141. "movl %4, %%ebx \n\t" \
  142. "movl %%ecx, %1 \n\t" \
  143. "movl %%edi, %2 \n\t" \
  144. "movl %%esi, %3 \n\t" \
  145. : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
  146. : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
  147. : "eax", "ecx", "edx", "esi", "edi" \
  148. );
  149. #endif /* SSE2 */
  150. #endif /* i386 */
  151. #if defined(__amd64__) || defined (__x86_64__)
  152. #define MULADDC_INIT \
  153. asm( \
  154. "xorq %%r8, %%r8 \n\t"
  155. #define MULADDC_CORE \
  156. "movq (%%rsi), %%rax \n\t" \
  157. "mulq %%rbx \n\t" \
  158. "addq $8, %%rsi \n\t" \
  159. "addq %%rcx, %%rax \n\t" \
  160. "movq %%r8, %%rcx \n\t" \
  161. "adcq $0, %%rdx \n\t" \
  162. "nop \n\t" \
  163. "addq %%rax, (%%rdi) \n\t" \
  164. "adcq %%rdx, %%rcx \n\t" \
  165. "addq $8, %%rdi \n\t"
  166. #define MULADDC_STOP \
  167. : "+c" (c), "+D" (d), "+S" (s) \
  168. : "b" (b) \
  169. : "rax", "rdx", "r8" \
  170. );
  171. #endif /* AMD64 */
  172. #if defined(__mc68020__) || defined(__mcpu32__)
  173. #define MULADDC_INIT \
  174. asm( \
  175. "movl %3, %%a2 \n\t" \
  176. "movl %4, %%a3 \n\t" \
  177. "movl %5, %%d3 \n\t" \
  178. "movl %6, %%d2 \n\t" \
  179. "moveq #0, %%d0 \n\t"
  180. #define MULADDC_CORE \
  181. "movel %%a2@+, %%d1 \n\t" \
  182. "mulul %%d2, %%d4:%%d1 \n\t" \
  183. "addl %%d3, %%d1 \n\t" \
  184. "addxl %%d0, %%d4 \n\t" \
  185. "moveq #0, %%d3 \n\t" \
  186. "addl %%d1, %%a3@+ \n\t" \
  187. "addxl %%d4, %%d3 \n\t"
  188. #define MULADDC_STOP \
  189. "movl %%d3, %0 \n\t" \
  190. "movl %%a3, %1 \n\t" \
  191. "movl %%a2, %2 \n\t" \
  192. : "=m" (c), "=m" (d), "=m" (s) \
  193. : "m" (s), "m" (d), "m" (c), "m" (b) \
  194. : "d0", "d1", "d2", "d3", "d4", "a2", "a3" \
  195. );
  196. #define MULADDC_HUIT \
  197. "movel %%a2@+, %%d1 \n\t" \
  198. "mulul %%d2, %%d4:%%d1 \n\t" \
  199. "addxl %%d3, %%d1 \n\t" \
  200. "addxl %%d0, %%d4 \n\t" \
  201. "addl %%d1, %%a3@+ \n\t" \
  202. "movel %%a2@+, %%d1 \n\t" \
  203. "mulul %%d2, %%d3:%%d1 \n\t" \
  204. "addxl %%d4, %%d1 \n\t" \
  205. "addxl %%d0, %%d3 \n\t" \
  206. "addl %%d1, %%a3@+ \n\t" \
  207. "movel %%a2@+, %%d1 \n\t" \
  208. "mulul %%d2, %%d4:%%d1 \n\t" \
  209. "addxl %%d3, %%d1 \n\t" \
  210. "addxl %%d0, %%d4 \n\t" \
  211. "addl %%d1, %%a3@+ \n\t" \
  212. "movel %%a2@+, %%d1 \n\t" \
  213. "mulul %%d2, %%d3:%%d1 \n\t" \
  214. "addxl %%d4, %%d1 \n\t" \
  215. "addxl %%d0, %%d3 \n\t" \
  216. "addl %%d1, %%a3@+ \n\t" \
  217. "movel %%a2@+, %%d1 \n\t" \
  218. "mulul %%d2, %%d4:%%d1 \n\t" \
  219. "addxl %%d3, %%d1 \n\t" \
  220. "addxl %%d0, %%d4 \n\t" \
  221. "addl %%d1, %%a3@+ \n\t" \
  222. "movel %%a2@+, %%d1 \n\t" \
  223. "mulul %%d2, %%d3:%%d1 \n\t" \
  224. "addxl %%d4, %%d1 \n\t" \
  225. "addxl %%d0, %%d3 \n\t" \
  226. "addl %%d1, %%a3@+ \n\t" \
  227. "movel %%a2@+, %%d1 \n\t" \
  228. "mulul %%d2, %%d4:%%d1 \n\t" \
  229. "addxl %%d3, %%d1 \n\t" \
  230. "addxl %%d0, %%d4 \n\t" \
  231. "addl %%d1, %%a3@+ \n\t" \
  232. "movel %%a2@+, %%d1 \n\t" \
  233. "mulul %%d2, %%d3:%%d1 \n\t" \
  234. "addxl %%d4, %%d1 \n\t" \
  235. "addxl %%d0, %%d3 \n\t" \
  236. "addl %%d1, %%a3@+ \n\t" \
  237. "addxl %%d0, %%d3 \n\t"
  238. #endif /* MC68000 */
  239. #if defined(__powerpc64__) || defined(__ppc64__)
  240. #if defined(__MACH__) && defined(__APPLE__)
  241. #define MULADDC_INIT \
  242. asm( \
  243. "ld r3, %3 \n\t" \
  244. "ld r4, %4 \n\t" \
  245. "ld r5, %5 \n\t" \
  246. "ld r6, %6 \n\t" \
  247. "addi r3, r3, -8 \n\t" \
  248. "addi r4, r4, -8 \n\t" \
  249. "addic r5, r5, 0 \n\t"
  250. #define MULADDC_CORE \
  251. "ldu r7, 8(r3) \n\t" \
  252. "mulld r8, r7, r6 \n\t" \
  253. "mulhdu r9, r7, r6 \n\t" \
  254. "adde r8, r8, r5 \n\t" \
  255. "ld r7, 8(r4) \n\t" \
  256. "addze r5, r9 \n\t" \
  257. "addc r8, r8, r7 \n\t" \
  258. "stdu r8, 8(r4) \n\t"
  259. #define MULADDC_STOP \
  260. "addze r5, r5 \n\t" \
  261. "addi r4, r4, 8 \n\t" \
  262. "addi r3, r3, 8 \n\t" \
  263. "std r5, %0 \n\t" \
  264. "std r4, %1 \n\t" \
  265. "std r3, %2 \n\t" \
  266. : "=m" (c), "=m" (d), "=m" (s) \
  267. : "m" (s), "m" (d), "m" (c), "m" (b) \
  268. : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
  269. );
  270. #else /* __MACH__ && __APPLE__ */
  271. #define MULADDC_INIT \
  272. asm( \
  273. "ld %%r3, %3 \n\t" \
  274. "ld %%r4, %4 \n\t" \
  275. "ld %%r5, %5 \n\t" \
  276. "ld %%r6, %6 \n\t" \
  277. "addi %%r3, %%r3, -8 \n\t" \
  278. "addi %%r4, %%r4, -8 \n\t" \
  279. "addic %%r5, %%r5, 0 \n\t"
  280. #define MULADDC_CORE \
  281. "ldu %%r7, 8(%%r3) \n\t" \
  282. "mulld %%r8, %%r7, %%r6 \n\t" \
  283. "mulhdu %%r9, %%r7, %%r6 \n\t" \
  284. "adde %%r8, %%r8, %%r5 \n\t" \
  285. "ld %%r7, 8(%%r4) \n\t" \
  286. "addze %%r5, %%r9 \n\t" \
  287. "addc %%r8, %%r8, %%r7 \n\t" \
  288. "stdu %%r8, 8(%%r4) \n\t"
  289. #define MULADDC_STOP \
  290. "addze %%r5, %%r5 \n\t" \
  291. "addi %%r4, %%r4, 8 \n\t" \
  292. "addi %%r3, %%r3, 8 \n\t" \
  293. "std %%r5, %0 \n\t" \
  294. "std %%r4, %1 \n\t" \
  295. "std %%r3, %2 \n\t" \
  296. : "=m" (c), "=m" (d), "=m" (s) \
  297. : "m" (s), "m" (d), "m" (c), "m" (b) \
  298. : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
  299. );
  300. #endif /* __MACH__ && __APPLE__ */
  301. #elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32 */
  302. #if defined(__MACH__) && defined(__APPLE__)
  303. #define MULADDC_INIT \
  304. asm( \
  305. "lwz r3, %3 \n\t" \
  306. "lwz r4, %4 \n\t" \
  307. "lwz r5, %5 \n\t" \
  308. "lwz r6, %6 \n\t" \
  309. "addi r3, r3, -4 \n\t" \
  310. "addi r4, r4, -4 \n\t" \
  311. "addic r5, r5, 0 \n\t"
  312. #define MULADDC_CORE \
  313. "lwzu r7, 4(r3) \n\t" \
  314. "mullw r8, r7, r6 \n\t" \
  315. "mulhwu r9, r7, r6 \n\t" \
  316. "adde r8, r8, r5 \n\t" \
  317. "lwz r7, 4(r4) \n\t" \
  318. "addze r5, r9 \n\t" \
  319. "addc r8, r8, r7 \n\t" \
  320. "stwu r8, 4(r4) \n\t"
  321. #define MULADDC_STOP \
  322. "addze r5, r5 \n\t" \
  323. "addi r4, r4, 4 \n\t" \
  324. "addi r3, r3, 4 \n\t" \
  325. "stw r5, %0 \n\t" \
  326. "stw r4, %1 \n\t" \
  327. "stw r3, %2 \n\t" \
  328. : "=m" (c), "=m" (d), "=m" (s) \
  329. : "m" (s), "m" (d), "m" (c), "m" (b) \
  330. : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
  331. );
  332. #else /* __MACH__ && __APPLE__ */
  333. #define MULADDC_INIT \
  334. asm( \
  335. "lwz %%r3, %3 \n\t" \
  336. "lwz %%r4, %4 \n\t" \
  337. "lwz %%r5, %5 \n\t" \
  338. "lwz %%r6, %6 \n\t" \
  339. "addi %%r3, %%r3, -4 \n\t" \
  340. "addi %%r4, %%r4, -4 \n\t" \
  341. "addic %%r5, %%r5, 0 \n\t"
  342. #define MULADDC_CORE \
  343. "lwzu %%r7, 4(%%r3) \n\t" \
  344. "mullw %%r8, %%r7, %%r6 \n\t" \
  345. "mulhwu %%r9, %%r7, %%r6 \n\t" \
  346. "adde %%r8, %%r8, %%r5 \n\t" \
  347. "lwz %%r7, 4(%%r4) \n\t" \
  348. "addze %%r5, %%r9 \n\t" \
  349. "addc %%r8, %%r8, %%r7 \n\t" \
  350. "stwu %%r8, 4(%%r4) \n\t"
  351. #define MULADDC_STOP \
  352. "addze %%r5, %%r5 \n\t" \
  353. "addi %%r4, %%r4, 4 \n\t" \
  354. "addi %%r3, %%r3, 4 \n\t" \
  355. "stw %%r5, %0 \n\t" \
  356. "stw %%r4, %1 \n\t" \
  357. "stw %%r3, %2 \n\t" \
  358. : "=m" (c), "=m" (d), "=m" (s) \
  359. : "m" (s), "m" (d), "m" (c), "m" (b) \
  360. : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
  361. );
  362. #endif /* __MACH__ && __APPLE__ */
  363. #endif /* PPC32 */
  364. /*
  365. * The Sparc(64) assembly is reported to be broken.
  366. * Disable it for now, until we're able to fix it.
  367. */
  368. #if 0 && defined(__sparc__)
  369. #if defined(__sparc64__)
  370. #define MULADDC_INIT \
  371. asm( \
  372. "ldx %3, %%o0 \n\t" \
  373. "ldx %4, %%o1 \n\t" \
  374. "ld %5, %%o2 \n\t" \
  375. "ld %6, %%o3 \n\t"
  376. #define MULADDC_CORE \
  377. "ld [%%o0], %%o4 \n\t" \
  378. "inc 4, %%o0 \n\t" \
  379. "ld [%%o1], %%o5 \n\t" \
  380. "umul %%o3, %%o4, %%o4 \n\t" \
  381. "addcc %%o4, %%o2, %%o4 \n\t" \
  382. "rd %%y, %%g1 \n\t" \
  383. "addx %%g1, 0, %%g1 \n\t" \
  384. "addcc %%o4, %%o5, %%o4 \n\t" \
  385. "st %%o4, [%%o1] \n\t" \
  386. "addx %%g1, 0, %%o2 \n\t" \
  387. "inc 4, %%o1 \n\t"
  388. #define MULADDC_STOP \
  389. "st %%o2, %0 \n\t" \
  390. "stx %%o1, %1 \n\t" \
  391. "stx %%o0, %2 \n\t" \
  392. : "=m" (c), "=m" (d), "=m" (s) \
  393. : "m" (s), "m" (d), "m" (c), "m" (b) \
  394. : "g1", "o0", "o1", "o2", "o3", "o4", \
  395. "o5" \
  396. );
  397. #else /* __sparc64__ */
  398. #define MULADDC_INIT \
  399. asm( \
  400. "ld %3, %%o0 \n\t" \
  401. "ld %4, %%o1 \n\t" \
  402. "ld %5, %%o2 \n\t" \
  403. "ld %6, %%o3 \n\t"
  404. #define MULADDC_CORE \
  405. "ld [%%o0], %%o4 \n\t" \
  406. "inc 4, %%o0 \n\t" \
  407. "ld [%%o1], %%o5 \n\t" \
  408. "umul %%o3, %%o4, %%o4 \n\t" \
  409. "addcc %%o4, %%o2, %%o4 \n\t" \
  410. "rd %%y, %%g1 \n\t" \
  411. "addx %%g1, 0, %%g1 \n\t" \
  412. "addcc %%o4, %%o5, %%o4 \n\t" \
  413. "st %%o4, [%%o1] \n\t" \
  414. "addx %%g1, 0, %%o2 \n\t" \
  415. "inc 4, %%o1 \n\t"
  416. #define MULADDC_STOP \
  417. "st %%o2, %0 \n\t" \
  418. "st %%o1, %1 \n\t" \
  419. "st %%o0, %2 \n\t" \
  420. : "=m" (c), "=m" (d), "=m" (s) \
  421. : "m" (s), "m" (d), "m" (c), "m" (b) \
  422. : "g1", "o0", "o1", "o2", "o3", "o4", \
  423. "o5" \
  424. );
  425. #endif /* __sparc64__ */
  426. #endif /* __sparc__ */
  427. #if defined(__microblaze__) || defined(microblaze)
  428. #define MULADDC_INIT \
  429. asm( \
  430. "lwi r3, %3 \n\t" \
  431. "lwi r4, %4 \n\t" \
  432. "lwi r5, %5 \n\t" \
  433. "lwi r6, %6 \n\t" \
  434. "andi r7, r6, 0xffff \n\t" \
  435. "bsrli r6, r6, 16 \n\t"
  436. #define MULADDC_CORE \
  437. "lhui r8, r3, 0 \n\t" \
  438. "addi r3, r3, 2 \n\t" \
  439. "lhui r9, r3, 0 \n\t" \
  440. "addi r3, r3, 2 \n\t" \
  441. "mul r10, r9, r6 \n\t" \
  442. "mul r11, r8, r7 \n\t" \
  443. "mul r12, r9, r7 \n\t" \
  444. "mul r13, r8, r6 \n\t" \
  445. "bsrli r8, r10, 16 \n\t" \
  446. "bsrli r9, r11, 16 \n\t" \
  447. "add r13, r13, r8 \n\t" \
  448. "add r13, r13, r9 \n\t" \
  449. "bslli r10, r10, 16 \n\t" \
  450. "bslli r11, r11, 16 \n\t" \
  451. "add r12, r12, r10 \n\t" \
  452. "addc r13, r13, r0 \n\t" \
  453. "add r12, r12, r11 \n\t" \
  454. "addc r13, r13, r0 \n\t" \
  455. "lwi r10, r4, 0 \n\t" \
  456. "add r12, r12, r10 \n\t" \
  457. "addc r13, r13, r0 \n\t" \
  458. "add r12, r12, r5 \n\t" \
  459. "addc r5, r13, r0 \n\t" \
  460. "swi r12, r4, 0 \n\t" \
  461. "addi r4, r4, 4 \n\t"
  462. #define MULADDC_STOP \
  463. "swi r5, %0 \n\t" \
  464. "swi r4, %1 \n\t" \
  465. "swi r3, %2 \n\t" \
  466. : "=m" (c), "=m" (d), "=m" (s) \
  467. : "m" (s), "m" (d), "m" (c), "m" (b) \
  468. : "r3", "r4" "r5", "r6", "r7", "r8", \
  469. "r9", "r10", "r11", "r12", "r13" \
  470. );
  471. #endif /* MicroBlaze */
  472. #if defined(__tricore__)
  473. #define MULADDC_INIT \
  474. asm( \
  475. "ld.a %%a2, %3 \n\t" \
  476. "ld.a %%a3, %4 \n\t" \
  477. "ld.w %%d4, %5 \n\t" \
  478. "ld.w %%d1, %6 \n\t" \
  479. "xor %%d5, %%d5 \n\t"
  480. #define MULADDC_CORE \
  481. "ld.w %%d0, [%%a2+] \n\t" \
  482. "madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \
  483. "ld.w %%d0, [%%a3] \n\t" \
  484. "addx %%d2, %%d2, %%d0 \n\t" \
  485. "addc %%d3, %%d3, 0 \n\t" \
  486. "mov %%d4, %%d3 \n\t" \
  487. "st.w [%%a3+], %%d2 \n\t"
  488. #define MULADDC_STOP \
  489. "st.w %0, %%d4 \n\t" \
  490. "st.a %1, %%a3 \n\t" \
  491. "st.a %2, %%a2 \n\t" \
  492. : "=m" (c), "=m" (d), "=m" (s) \
  493. : "m" (s), "m" (d), "m" (c), "m" (b) \
  494. : "d0", "d1", "e2", "d4", "a2", "a3" \
  495. );
  496. #endif /* TriCore */
  497. /*
  498. * gcc -O0 by default uses r7 for the frame pointer, so it complains about our
  499. * use of r7 below, unless -fomit-frame-pointer is passed. Unfortunately,
  500. * passing that option is not easy when building with yotta.
  501. *
  502. * On the other hand, -fomit-frame-pointer is implied by any -Ox options with
  503. * x !=0, which we can detect using __OPTIMIZE__ (which is also defined by
  504. * clang and armcc5 under the same conditions).
  505. *
  506. * So, only use the optimized assembly below for optimized build, which avoids
  507. * the build error and is pretty reasonable anyway.
  508. */
  509. #if defined(__GNUC__) && !defined(__OPTIMIZE__)
  510. #define MULADDC_CANNOT_USE_R7
  511. #endif
  512. #if defined(__arm__) && !defined(MULADDC_CANNOT_USE_R7)
  513. #if defined(__thumb__) && !defined(__thumb2__)
  514. #define MULADDC_INIT \
  515. asm( \
  516. "ldr r0, %3 \n\t" \
  517. "ldr r1, %4 \n\t" \
  518. "ldr r2, %5 \n\t" \
  519. "ldr r3, %6 \n\t" \
  520. "lsr r7, r3, #16 \n\t" \
  521. "mov r9, r7 \n\t" \
  522. "lsl r7, r3, #16 \n\t" \
  523. "lsr r7, r7, #16 \n\t" \
  524. "mov r8, r7 \n\t"
  525. #define MULADDC_CORE \
  526. "ldmia r0!, {r6} \n\t" \
  527. "lsr r7, r6, #16 \n\t" \
  528. "lsl r6, r6, #16 \n\t" \
  529. "lsr r6, r6, #16 \n\t" \
  530. "mov r4, r8 \n\t" \
  531. "mul r4, r6 \n\t" \
  532. "mov r3, r9 \n\t" \
  533. "mul r6, r3 \n\t" \
  534. "mov r5, r9 \n\t" \
  535. "mul r5, r7 \n\t" \
  536. "mov r3, r8 \n\t" \
  537. "mul r7, r3 \n\t" \
  538. "lsr r3, r6, #16 \n\t" \
  539. "add r5, r5, r3 \n\t" \
  540. "lsr r3, r7, #16 \n\t" \
  541. "add r5, r5, r3 \n\t" \
  542. "add r4, r4, r2 \n\t" \
  543. "mov r2, #0 \n\t" \
  544. "adc r5, r2 \n\t" \
  545. "lsl r3, r6, #16 \n\t" \
  546. "add r4, r4, r3 \n\t" \
  547. "adc r5, r2 \n\t" \
  548. "lsl r3, r7, #16 \n\t" \
  549. "add r4, r4, r3 \n\t" \
  550. "adc r5, r2 \n\t" \
  551. "ldr r3, [r1] \n\t" \
  552. "add r4, r4, r3 \n\t" \
  553. "adc r2, r5 \n\t" \
  554. "stmia r1!, {r4} \n\t"
  555. #define MULADDC_STOP \
  556. "str r2, %0 \n\t" \
  557. "str r1, %1 \n\t" \
  558. "str r0, %2 \n\t" \
  559. : "=m" (c), "=m" (d), "=m" (s) \
  560. : "m" (s), "m" (d), "m" (c), "m" (b) \
  561. : "r0", "r1", "r2", "r3", "r4", "r5", \
  562. "r6", "r7", "r8", "r9", "cc" \
  563. );
  564. #else
  565. #define MULADDC_INIT \
  566. asm( \
  567. "ldr r0, %3 \n\t" \
  568. "ldr r1, %4 \n\t" \
  569. "ldr r2, %5 \n\t" \
  570. "ldr r3, %6 \n\t"
  571. #define MULADDC_CORE \
  572. "ldr r4, [r0], #4 \n\t" \
  573. "mov r5, #0 \n\t" \
  574. "ldr r6, [r1] \n\t" \
  575. "umlal r2, r5, r3, r4 \n\t" \
  576. "adds r7, r6, r2 \n\t" \
  577. "adc r2, r5, #0 \n\t" \
  578. "str r7, [r1], #4 \n\t"
  579. #define MULADDC_STOP \
  580. "str r2, %0 \n\t" \
  581. "str r1, %1 \n\t" \
  582. "str r0, %2 \n\t" \
  583. : "=m" (c), "=m" (d), "=m" (s) \
  584. : "m" (s), "m" (d), "m" (c), "m" (b) \
  585. : "r0", "r1", "r2", "r3", "r4", "r5", \
  586. "r6", "r7", "cc" \
  587. );
  588. #endif /* Thumb */
  589. #endif /* ARMv3 */
  590. #if defined(__alpha__)
  591. #define MULADDC_INIT \
  592. asm( \
  593. "ldq $1, %3 \n\t" \
  594. "ldq $2, %4 \n\t" \
  595. "ldq $3, %5 \n\t" \
  596. "ldq $4, %6 \n\t"
  597. #define MULADDC_CORE \
  598. "ldq $6, 0($1) \n\t" \
  599. "addq $1, 8, $1 \n\t" \
  600. "mulq $6, $4, $7 \n\t" \
  601. "umulh $6, $4, $6 \n\t" \
  602. "addq $7, $3, $7 \n\t" \
  603. "cmpult $7, $3, $3 \n\t" \
  604. "ldq $5, 0($2) \n\t" \
  605. "addq $7, $5, $7 \n\t" \
  606. "cmpult $7, $5, $5 \n\t" \
  607. "stq $7, 0($2) \n\t" \
  608. "addq $2, 8, $2 \n\t" \
  609. "addq $6, $3, $3 \n\t" \
  610. "addq $5, $3, $3 \n\t"
  611. #define MULADDC_STOP \
  612. "stq $3, %0 \n\t" \
  613. "stq $2, %1 \n\t" \
  614. "stq $1, %2 \n\t" \
  615. : "=m" (c), "=m" (d), "=m" (s) \
  616. : "m" (s), "m" (d), "m" (c), "m" (b) \
  617. : "$1", "$2", "$3", "$4", "$5", "$6", "$7" \
  618. );
  619. #endif /* Alpha */
  620. #if defined(__mips__) && !defined(__mips64)
  621. #define MULADDC_INIT \
  622. asm( \
  623. "lw $10, %3 \n\t" \
  624. "lw $11, %4 \n\t" \
  625. "lw $12, %5 \n\t" \
  626. "lw $13, %6 \n\t"
  627. #define MULADDC_CORE \
  628. "lw $14, 0($10) \n\t" \
  629. "multu $13, $14 \n\t" \
  630. "addi $10, $10, 4 \n\t" \
  631. "mflo $14 \n\t" \
  632. "mfhi $9 \n\t" \
  633. "addu $14, $12, $14 \n\t" \
  634. "lw $15, 0($11) \n\t" \
  635. "sltu $12, $14, $12 \n\t" \
  636. "addu $15, $14, $15 \n\t" \
  637. "sltu $14, $15, $14 \n\t" \
  638. "addu $12, $12, $9 \n\t" \
  639. "sw $15, 0($11) \n\t" \
  640. "addu $12, $12, $14 \n\t" \
  641. "addi $11, $11, 4 \n\t"
  642. #define MULADDC_STOP \
  643. "sw $12, %0 \n\t" \
  644. "sw $11, %1 \n\t" \
  645. "sw $10, %2 \n\t" \
  646. : "=m" (c), "=m" (d), "=m" (s) \
  647. : "m" (s), "m" (d), "m" (c), "m" (b) \
  648. : "$9", "$10", "$11", "$12", "$13", "$14", "$15" \
  649. );
  650. #endif /* MIPS */
  651. #endif /* GNUC */
  652. #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  653. #define MULADDC_INIT \
  654. __asm mov esi, s \
  655. __asm mov edi, d \
  656. __asm mov ecx, c \
  657. __asm mov ebx, b
  658. #define MULADDC_CORE \
  659. __asm lodsd \
  660. __asm mul ebx \
  661. __asm add eax, ecx \
  662. __asm adc edx, 0 \
  663. __asm add eax, [edi] \
  664. __asm adc edx, 0 \
  665. __asm mov ecx, edx \
  666. __asm stosd
  667. #if defined(MBEDTLS_HAVE_SSE2)
  668. #define EMIT __asm _emit
  669. #define MULADDC_HUIT \
  670. EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
  671. EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
  672. EMIT 0x0F EMIT 0x6E EMIT 0x1F \
  673. EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
  674. EMIT 0x0F EMIT 0x6E EMIT 0x16 \
  675. EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
  676. EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
  677. EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
  678. EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
  679. EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
  680. EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
  681. EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
  682. EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
  683. EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
  684. EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
  685. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
  686. EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
  687. EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
  688. EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
  689. EMIT 0x0F EMIT 0x7E EMIT 0x0F \
  690. EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
  691. EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
  692. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  693. EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
  694. EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
  695. EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
  696. EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
  697. EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
  698. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
  699. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  700. EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
  701. EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
  702. EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
  703. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
  704. EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
  705. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
  706. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  707. EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
  708. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
  709. EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
  710. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
  711. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  712. EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
  713. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
  714. EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
  715. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
  716. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  717. EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
  718. EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
  719. EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
  720. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
  721. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  722. EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
  723. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
  724. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  725. EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
  726. EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
  727. EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
  728. EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
  729. EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
  730. EMIT 0x0F EMIT 0x7E EMIT 0xC9
  731. #define MULADDC_STOP \
  732. EMIT 0x0F EMIT 0x77 \
  733. __asm mov c, ecx \
  734. __asm mov d, edi \
  735. __asm mov s, esi \
  736. #else
  737. #define MULADDC_STOP \
  738. __asm mov c, ecx \
  739. __asm mov d, edi \
  740. __asm mov s, esi \
  741. #endif /* SSE2 */
  742. #endif /* MSVC */
  743. #endif /* MBEDTLS_HAVE_ASM */
  744. #if !defined(MULADDC_CORE)
  745. #if defined(MBEDTLS_HAVE_UDBL)
  746. #define MULADDC_INIT \
  747. { \
  748. mbedtls_t_udbl r; \
  749. mbedtls_mpi_uint r0, r1;
  750. #define MULADDC_CORE \
  751. r = *(s++) * (mbedtls_t_udbl) b; \
  752. r0 = (mbedtls_mpi_uint) r; \
  753. r1 = (mbedtls_mpi_uint)( r >> biL ); \
  754. r0 += c; r1 += (r0 < c); \
  755. r0 += *d; r1 += (r0 < *d); \
  756. c = r1; *(d++) = r0;
  757. #define MULADDC_STOP \
  758. }
  759. #else
  760. #define MULADDC_INIT \
  761. { \
  762. mbedtls_mpi_uint s0, s1, b0, b1; \
  763. mbedtls_mpi_uint r0, r1, rx, ry; \
  764. b0 = ( b << biH ) >> biH; \
  765. b1 = ( b >> biH );
  766. #define MULADDC_CORE \
  767. s0 = ( *s << biH ) >> biH; \
  768. s1 = ( *s >> biH ); s++; \
  769. rx = s0 * b1; r0 = s0 * b0; \
  770. ry = s1 * b0; r1 = s1 * b1; \
  771. r1 += ( rx >> biH ); \
  772. r1 += ( ry >> biH ); \
  773. rx <<= biH; ry <<= biH; \
  774. r0 += rx; r1 += (r0 < rx); \
  775. r0 += ry; r1 += (r0 < ry); \
  776. r0 += c; r1 += (r0 < c); \
  777. r0 += *d; r1 += (r0 < *d); \
  778. c = r1; *(d++) = r0;
  779. #define MULADDC_STOP \
  780. }
  781. #endif /* C (generic) */
  782. #endif /* C (longlong) */
  783. #endif /* bn_mul.h */