You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

x86_bigint.c 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <ipxe/bigint.h>
  23. /** @file
  24. *
  25. * Big integer support
  26. */
  27. /**
  28. * Multiply big integers
  29. *
  30. * @v multiplicand0 Element 0 of big integer to be multiplied
  31. * @v multiplier0 Element 0 of big integer to be multiplied
  32. * @v result0 Element 0 of big integer to hold result
  33. * @v size Number of elements
  34. */
  35. void bigint_multiply_raw ( const uint32_t *multiplicand0,
  36. const uint32_t *multiplier0,
  37. uint32_t *result0, unsigned int size ) {
  38. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplicand =
  39. ( ( const void * ) multiplicand0 );
  40. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplier =
  41. ( ( const void * ) multiplier0 );
  42. bigint_t ( size * 2 ) __attribute__ (( may_alias )) *result =
  43. ( ( void * ) result0 );
  44. unsigned int i;
  45. unsigned int j;
  46. uint32_t multiplicand_element;
  47. uint32_t multiplier_element;
  48. uint32_t *result_elements;
  49. uint32_t discard_a;
  50. uint32_t discard_d;
  51. long index;
  52. /* Zero result */
  53. memset ( result, 0, sizeof ( *result ) );
  54. /* Multiply integers one element at a time */
  55. for ( i = 0 ; i < size ; i++ ) {
  56. multiplicand_element = multiplicand->element[i];
  57. for ( j = 0 ; j < size ; j++ ) {
  58. multiplier_element = multiplier->element[j];
  59. result_elements = &result->element[ i + j ];
  60. /* Perform a single multiply, and add the
  61. * resulting double-element into the result,
  62. * carrying as necessary. The carry can
  63. * never overflow beyond the end of the
  64. * result, since:
  65. *
  66. * a < 2^{n}, b < 2^{n} => ab < 2^{2n}
  67. */
  68. __asm__ __volatile__ ( "mull %4\n\t"
  69. "addl %%eax, (%5,%2,4)\n\t"
  70. "adcl %%edx, 4(%5,%2,4)\n\t"
  71. "\n1:\n\t"
  72. "adcl $0, 8(%5,%2,4)\n\t"
  73. "inc %2\n\t"
  74. /* Does not affect CF */
  75. "jc 1b\n\t"
  76. : "=&a" ( discard_a ),
  77. "=&d" ( discard_d ),
  78. "=&r" ( index )
  79. : "0" ( multiplicand_element ),
  80. "g" ( multiplier_element ),
  81. "r" ( result_elements ),
  82. "2" ( 0 ) );
  83. }
  84. }
  85. }