bn_sparc.c 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #include <stdlib.h>
  10. #include <openssl/bn.h>
  11. #include "internal/cryptlib.h"
  12. #include "crypto/sparc_arch.h"
  13. #include "bn_local.h" /* for definition of bn_mul_mont */
  14. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  15. const BN_ULONG *np, const BN_ULONG *n0, int num)
  16. {
  17. int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  18. const BN_ULONG *np, const BN_ULONG *n0, int num);
  19. int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  20. const BN_ULONG *np, const BN_ULONG *n0, int num);
  21. int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  22. const BN_ULONG *np, const BN_ULONG *n0, int num);
  23. if (!(num & 1) && num >= 6) {
  24. if ((num & 15) == 0 && num <= 64 &&
  25. (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
  26. (CFR_MONTMUL | CFR_MONTSQR)) {
  27. typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
  28. const BN_ULONG *bp,
  29. const BN_ULONG *np,
  30. const BN_ULONG *n0);
  31. int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
  32. const BN_ULONG *bp, const BN_ULONG *np,
  33. const BN_ULONG *n0);
  34. int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
  35. const BN_ULONG *bp, const BN_ULONG *np,
  36. const BN_ULONG *n0);
  37. int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
  38. const BN_ULONG *bp, const BN_ULONG *np,
  39. const BN_ULONG *n0);
  40. int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
  41. const BN_ULONG *bp, const BN_ULONG *np,
  42. const BN_ULONG *n0);
  43. static const bn_mul_mont_f funcs[4] = {
  44. bn_mul_mont_t4_8, bn_mul_mont_t4_16,
  45. bn_mul_mont_t4_24, bn_mul_mont_t4_32
  46. };
  47. bn_mul_mont_f worker = funcs[num / 16 - 1];
  48. if ((*worker) (rp, ap, bp, np, n0))
  49. return 1;
  50. /* retry once and fall back */
  51. if ((*worker) (rp, ap, bp, np, n0))
  52. return 1;
  53. return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
  54. }
  55. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
  56. return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
  57. else if (num >= 8 &&
  58. /*
  59. * bn_mul_mont_fpu doesn't use FMADD, we just use the
  60. * flag to detect when FPU path is preferable in cases
  61. * when current heuristics is unreliable. [it works
  62. * out because FMADD-capable processors where FPU
  63. * code path is undesirable are also VIS3-capable and
  64. * VIS3 code path takes precedence.]
  65. */
  66. ( (OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) ||
  67. (OPENSSL_sparcv9cap_P[0] &
  68. (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
  69. (SPARCV9_PREFER_FPU | SPARCV9_VIS1) ))
  70. return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
  71. }
  72. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  73. }