01c977993890dbd7cf8700ecc6faa61ab521ca7e
[openssl.git] / crypto / ec / curve448 / word.h
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2014 Cryptography Research, Inc.
4  *
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
9  *
10  * Originally written by Mike Hamburg
11  */
12
13 #ifndef HEADER_WORD_H
14 # define HEADER_WORD_H
15
16 # include <string.h>
17
18 # include <assert.h>
19 # include <openssl/e_os2.h>
20 # include "arch_intrinsics.h"
21
22 # include "curve448utils.h"
23 # include <stdlib.h>
24
25 # if defined(__ARM_NEON__)
26 #  include <arm_neon.h>
27 # elif defined(__SSE2__)
28 #  if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 5 \
29       || (__GNUC__==4 && __GNUC_MINOR__ >= 4)
30 #   include <immintrin.h>
31 #  else
32 #   include <emmintrin.h>
33 #  endif
34 # endif
35
36 # if (ARCH_WORD_BITS == 64)
37 typedef uint64_t word_t, mask_t;
38 typedef __uint128_t dword_t;
39 typedef int32_t hsword_t;
40 typedef int64_t sword_t;
41 typedef __int128_t dsword_t;
42 # elif (ARCH_WORD_BITS == 32)
43 typedef uint32_t word_t, mask_t;
44 typedef uint64_t dword_t;
45 typedef int16_t hsword_t;
46 typedef int32_t sword_t;
47 typedef int64_t dsword_t;
48 # else
49 #  error "For now, we only support 32- and 64-bit architectures."
50 # endif
51
52 /*
53  * Scalar limbs are keyed off of the API word size instead of the arch word
54  * size.
55  */
56 # if C448_WORD_BITS == 64
57 #  define SC_LIMB(x) (x)
58 # elif C448_WORD_BITS == 32
59 #  define SC_LIMB(x) ((uint32_t)x),(x >> 32)
60 # else
61 #  error "For now we only support 32- and 64-bit architectures."
62 # endif
63
64
65 /* PERF: vectorize vs unroll */
66 # ifdef __clang__
67 #  if 100*__clang_major__ + __clang_minor__ > 305
68 #   define UNROLL _Pragma("clang loop unroll(full)")
69 #  endif
70 # endif
71
72 # ifndef UNROLL
73 #  define UNROLL
74 # endif
75
76 /*
77  * The plan on booleans: The external interface uses c448_bool_t, but this
78  * might be a different size than our particular arch's word_t (and thus
79  * mask_t).  Also, the caller isn't guaranteed to pass it as nonzero.  So
80  * bool_to_mask converts word sizes and checks nonzero. On the flip side,
81  * mask_t is always -1 or 0, but it might be a different size than
82  * c448_bool_t. On the third hand, we have success vs boolean types, but
83  * that's handled in common.h: it converts between c448_bool_t and
84  * c448_error_t.
85  */
86 static ossl_inline c448_bool_t mask_to_bool(mask_t m)
87 {
88     return (c448_sword_t)(sword_t)m;
89 }
90
91 static ossl_inline mask_t bool_to_mask(c448_bool_t m)
92 {
93     /* On most arches this will be optimized to a simple cast. */
94     mask_t ret = 0;
95     unsigned int i;
96     unsigned int limit = sizeof(c448_bool_t) / sizeof(mask_t);
97
98     if (limit < 1)
99         limit = 1;
100     for (i = 0; i < limit; i++)
101         ret |= ~word_is_zero(m >> (i * 8 * sizeof(word_t)));
102
103     return ret;
104 }
105
106 static ossl_inline void ignore_result(c448_bool_t boo)
107 {
108     (void)boo;
109 }
110
111 #endif                          /* HEADER_WORD_H */