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