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.

arm64_bigint.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C) 2016 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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <ipxe/bigint.h>
  27. /** @file
  28. *
  29. * Big integer support
  30. */
  31. /**
  32. * Multiply big integers
  33. *
  34. * @v multiplicand0 Element 0 of big integer to be multiplied
  35. * @v multiplier0 Element 0 of big integer to be multiplied
  36. * @v result0 Element 0 of big integer to hold result
  37. * @v size Number of elements
  38. */
  39. void bigint_multiply_raw ( const uint64_t *multiplicand0,
  40. const uint64_t *multiplier0,
  41. uint64_t *result0, unsigned int size ) {
  42. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplicand =
  43. ( ( const void * ) multiplicand0 );
  44. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplier =
  45. ( ( const void * ) multiplier0 );
  46. bigint_t ( size * 2 ) __attribute__ (( may_alias )) *result =
  47. ( ( void * ) result0 );
  48. unsigned int i;
  49. unsigned int j;
  50. uint64_t multiplicand_element;
  51. uint64_t multiplier_element;
  52. uint64_t *result_elements;
  53. uint64_t discard_low;
  54. uint64_t discard_high;
  55. uint64_t discard_temp_low;
  56. uint64_t discard_temp_high;
  57. /* Zero result */
  58. memset ( result, 0, sizeof ( *result ) );
  59. /* Multiply integers one element at a time */
  60. for ( i = 0 ; i < size ; i++ ) {
  61. multiplicand_element = multiplicand->element[i];
  62. for ( j = 0 ; j < size ; j++ ) {
  63. multiplier_element = multiplier->element[j];
  64. result_elements = &result->element[ i + j ];
  65. /* Perform a single multiply, and add the
  66. * resulting double-element into the result,
  67. * carrying as necessary. The carry can
  68. * never overflow beyond the end of the
  69. * result, since:
  70. *
  71. * a < 2^{n}, b < 2^{n} => ab < 2^{2n}
  72. */
  73. __asm__ __volatile__ ( "mul %1, %6, %7\n\t"
  74. "umulh %2, %6, %7\n\t"
  75. "ldp %3, %4, [%0]\n\t"
  76. "adds %3, %3, %1\n\t"
  77. "adcs %4, %4, %2\n\t"
  78. "stp %3, %4, [%0], #16\n\t"
  79. "bcc 2f\n\t"
  80. "\n1:\n\t"
  81. "ldr %3, [%0]\n\t"
  82. "adcs %3, %3, xzr\n\t"
  83. "str %3, [%0], #8\n\t"
  84. "bcs 1b\n\t"
  85. "\n2:\n\t"
  86. : "+r" ( result_elements ),
  87. "=&r" ( discard_low ),
  88. "=&r" ( discard_high ),
  89. "=r" ( discard_temp_low ),
  90. "=r" ( discard_temp_high ),
  91. "+m" ( *result )
  92. : "r" ( multiplicand_element ),
  93. "r" ( multiplier_element )
  94. : "cc" );
  95. }
  96. }
  97. }