2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2014 Cryptography Research, Inc.
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
10 * Originally written by Mike Hamburg
19 # include <openssl/e_os2.h>
20 # include "arch_intrinsics.h"
22 # include "curve448utils.h"
25 # define _BSD_SOURCE 1
28 # ifndef _DEFAULT_SOURCE
29 # define _DEFAULT_SOURCE 1
34 # if defined(__ARM_NEON__)
35 # include <arm_neon.h>
36 # elif defined(__SSE2__)
37 # if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 5 \
38 || (__GNUC__==4 && __GNUC_MINOR__ >= 4)
39 # include <immintrin.h>
41 # include <emmintrin.h>
45 # if (ARCH_WORD_BITS == 64)
46 typedef uint64_t word_t, mask_t;
47 typedef __uint128_t dword_t;
48 typedef int32_t hsword_t;
49 typedef int64_t sword_t;
50 typedef __int128_t dsword_t;
51 # elif (ARCH_WORD_BITS == 32)
52 typedef uint32_t word_t, mask_t;
53 typedef uint64_t dword_t;
54 typedef int16_t hsword_t;
55 typedef int32_t sword_t;
56 typedef int64_t dsword_t;
58 # error "For now, we only support 32- and 64-bit architectures."
62 * Scalar limbs are keyed off of the API word size instead of the arch word
65 # if C448_WORD_BITS == 64
66 # define SC_LIMB(x) (x)
67 # elif C448_WORD_BITS == 32
68 # define SC_LIMB(x) ((uint32_t)x),(x>>32)
70 # error "For now we only support 32- and 64-bit architectures."
74 typedef uint32x4_t vecmask_t;
75 # elif defined(__clang__)
76 typedef uint64_t uint64x2_t __attribute__ ((ext_vector_type(2)));
77 typedef int64_t int64x2_t __attribute__ ((ext_vector_type(2)));
78 typedef uint64_t uint64x4_t __attribute__ ((ext_vector_type(4)));
79 typedef int64_t int64x4_t __attribute__ ((ext_vector_type(4)));
80 typedef uint32_t uint32x4_t __attribute__ ((ext_vector_type(4)));
81 typedef int32_t int32x4_t __attribute__ ((ext_vector_type(4)));
82 typedef uint32_t uint32x2_t __attribute__ ((ext_vector_type(2)));
83 typedef int32_t int32x2_t __attribute__ ((ext_vector_type(2)));
84 typedef uint32_t uint32x8_t __attribute__ ((ext_vector_type(8)));
85 typedef int32_t int32x8_t __attribute__ ((ext_vector_type(8)));
86 typedef word_t vecmask_t __attribute__ ((ext_vector_type(4)));
87 # elif defined(__GNUC__) \
88 && (__GNUC__ >= 4 || (__GNUC__== 3 && __GNUC_MINOR__ >= 1))
89 typedef uint64_t uint64x2_t __attribute__ ((vector_size(16)));
90 typedef int64_t int64x2_t __attribute__ ((vector_size(16)));
91 typedef uint64_t uint64x4_t __attribute__ ((vector_size(32)));
92 typedef int64_t int64x4_t __attribute__ ((vector_size(32)));
93 typedef uint32_t uint32x4_t __attribute__ ((vector_size(16)));
94 typedef int32_t int32x4_t __attribute__ ((vector_size(16)));
95 typedef uint32_t uint32x2_t __attribute__ ((vector_size(8)));
96 typedef int32_t int32x2_t __attribute__ ((vector_size(8)));
97 typedef uint32_t uint32x8_t __attribute__ ((vector_size(32)));
98 typedef int32_t int32x8_t __attribute__ ((vector_size(32)));
99 typedef word_t vecmask_t __attribute__ ((vector_size(32)));
102 # if defined(__AVX2__)
103 # define VECTOR_ALIGNED __attribute__((aligned(32)))
104 typedef uint32x8_t big_register_t;
105 typedef uint64x4_t uint64xn_t;
106 typedef uint32x8_t uint32xn_t;
108 static ossl_inline big_register_t br_set_to_mask(mask_t x)
110 uint32_t y = (uint32_t)x;
111 big_register_t ret = { y, y, y, y, y, y, y, y };
114 # elif defined(__SSE2__)
115 # define VECTOR_ALIGNED __attribute__((aligned(16)))
116 typedef uint32x4_t big_register_t;
117 typedef uint64x2_t uint64xn_t;
118 typedef uint32x4_t uint32xn_t;
120 static ossl_inline big_register_t br_set_to_mask(mask_t x)
123 big_register_t ret = { y, y, y, y };
126 # elif defined(__ARM_NEON__)
127 # define VECTOR_ALIGNED __attribute__((aligned(16)))
128 typedef uint32x4_t big_register_t;
129 typedef uint64x2_t uint64xn_t;
130 typedef uint32x4_t uint32xn_t;
132 static ossl_inline big_register_t br_set_to_mask(mask_t x)
134 return vdupq_n_u32(x);
136 # elif !defined(_MSC_VER) \
137 && (defined(_WIN64) || defined(__amd64__) || defined(__X86_64__) \
138 || defined(__aarch64__))
139 # define VECTOR_ALIGNED __attribute__((aligned(8)))
140 typedef uint64_t big_register_t, uint64xn_t;
142 typedef uint32_t uint32xn_t;
143 static ossl_inline big_register_t br_set_to_mask(mask_t x)
145 return (big_register_t) x;
149 # define VECTOR_ALIGNED __attribute__((aligned(4)))
152 * This shouldn't be a problem because a big_register_t isn't actually a vector
153 * type anyway in this case.
155 # define VECTOR_ALIGNED
157 typedef uint64_t uint64xn_t;
158 typedef uint32_t uint32xn_t;
159 typedef uint32_t big_register_t;
161 static ossl_inline big_register_t br_set_to_mask(mask_t x)
163 return (big_register_t) x;
167 # if defined(__AVX2__)
168 static ossl_inline big_register_t br_is_zero(big_register_t x)
170 return (big_register_t) (x == br_set_to_mask(0));
172 # elif defined(__SSE2__)
173 static ossl_inline big_register_t br_is_zero(big_register_t x)
175 return (big_register_t) _mm_cmpeq_epi32((__m128i) x, _mm_setzero_si128());
177 # elif defined(__ARM_NEON__)
178 static ossl_inline big_register_t br_is_zero(big_register_t x)
180 return vceqq_u32(x, x ^ x);
183 # define br_is_zero word_is_zero
186 /* PERF: vectorize vs unroll */
188 # if 100*__clang_major__ + __clang_minor__ > 305
189 # define UNROLL _Pragma("clang loop unroll(full)")
198 * The plan on booleans: The external interface uses c448_bool_t, but this
199 * might be a different size than our particular arch's word_t (and thus
200 * mask_t). Also, the caller isn't guaranteed to pass it as nonzero. So
201 * bool_to_mask converts word sizes and checks nonzero. On the flip side,
202 * mask_t is always -1 or 0, but it might be a different size than
203 * c448_bool_t. On the third hand, we have success vs boolean types, but
204 * that's handled in common.h: it converts between c448_bool_t and
207 static ossl_inline c448_bool_t mask_to_bool(mask_t m)
209 return (c448_sword_t)(sword_t)m;
212 static ossl_inline mask_t bool_to_mask(c448_bool_t m)
214 /* On most arches this will be optimized to a simple cast. */
217 unsigned int limit = sizeof(c448_bool_t) / sizeof(mask_t);
221 for (i = 0; i < limit; i++)
222 ret |= ~word_is_zero(m >> (i * 8 * sizeof(word_t)));
227 static ossl_inline void ignore_result(c448_bool_t boo)
232 #endif /* __WORD_H__ */