Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/crypto/skein_port.h
1201 views
1
#ifndef _SKEIN_PORT_H_
2
#define _SKEIN_PORT_H_
3
4
#include <limits.h>
5
#include <stdint.h>
6
7
#ifndef RETURN_VALUES
8
# define RETURN_VALUES
9
# if defined( DLL_EXPORT )
10
# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
11
# define VOID_RETURN __declspec( dllexport ) void __stdcall
12
# define INT_RETURN __declspec( dllexport ) int __stdcall
13
# elif defined( __GNUC__ )
14
# define VOID_RETURN __declspec( __dllexport__ ) void
15
# define INT_RETURN __declspec( __dllexport__ ) int
16
# else
17
# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
18
# endif
19
# elif defined( DLL_IMPORT )
20
# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
21
# define VOID_RETURN __declspec( dllimport ) void __stdcall
22
# define INT_RETURN __declspec( dllimport ) int __stdcall
23
# elif defined( __GNUC__ )
24
# define VOID_RETURN __declspec( __dllimport__ ) void
25
# define INT_RETURN __declspec( __dllimport__ ) int
26
# else
27
# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
28
# endif
29
# elif defined( __WATCOMC__ )
30
# define VOID_RETURN void __cdecl
31
# define INT_RETURN int __cdecl
32
# else
33
# define VOID_RETURN void
34
# define INT_RETURN int
35
# endif
36
#endif
37
38
/* These defines are used to declare buffers in a way that allows
39
faster operations on longer variables to be used. In all these
40
defines 'size' must be a power of 2 and >= 8
41
42
dec_unit_type(size,x) declares a variable 'x' of length
43
'size' bits
44
45
dec_bufr_type(size,bsize,x) declares a buffer 'x' of length 'bsize'
46
bytes defined as an array of variables
47
each of 'size' bits (bsize must be a
48
multiple of size / 8)
49
50
ptr_cast(x,size) casts a pointer to a pointer to a
51
varaiable of length 'size' bits
52
*/
53
54
#define ui_type(size) uint##size##_t
55
#define dec_unit_type(size,x) typedef ui_type(size) x
56
#define dec_bufr_type(size,bsize,x) typedef ui_type(size) x[bsize / (size >> 3)]
57
#define ptr_cast(x,size) ((ui_type(size)*)(x))
58
59
typedef unsigned int uint_t; /* native unsigned integer */
60
typedef uint8_t u08b_t; /* 8-bit unsigned integer */
61
typedef uint64_t u64b_t; /* 64-bit unsigned integer */
62
63
#ifndef RotL_64
64
#define RotL_64(x,N) (((x) << (N)) | ((x) >> (64-(N))))
65
#endif
66
67
/*
68
* Skein is "natively" little-endian (unlike SHA-xxx), for optimal
69
* performance on x86 CPUs. The Skein code requires the following
70
* definitions for dealing with endianness:
71
*
72
* SKEIN_NEED_SWAP: 0 for little-endian, 1 for big-endian
73
* Skein_Put64_LSB_First
74
* Skein_Get64_LSB_First
75
* Skein_Swap64
76
*
77
* If SKEIN_NEED_SWAP is defined at compile time, it is used here
78
* along with the portable versions of Put64/Get64/Swap64, which
79
* are slow in general.
80
*
81
* Otherwise, an "auto-detect" of endianness is attempted below.
82
* If the default handling doesn't work well, the user may insert
83
* platform-specific code instead (e.g., for big-endian CPUs).
84
*
85
*/
86
#ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */
87
88
89
#include "int-util.h"
90
91
#define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
92
#define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
93
94
#if BYTE_ORDER == LITTLE_ENDIAN
95
# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
96
#endif
97
98
#if BYTE_ORDER == BIG_ENDIAN
99
# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
100
#endif
101
102
/* special handler for IA64, which may be either endianness (?) */
103
/* here we assume little-endian, but this may need to be changed */
104
#if defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
105
# define PLATFORM_MUST_ALIGN (1)
106
#ifndef PLATFORM_BYTE_ORDER
107
# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
108
#endif
109
#endif
110
111
#ifndef PLATFORM_MUST_ALIGN
112
# define PLATFORM_MUST_ALIGN (0)
113
#endif
114
115
116
#if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
117
/* here for big-endian CPUs */
118
#define SKEIN_NEED_SWAP (1)
119
#elif PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN
120
/* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
121
#define SKEIN_NEED_SWAP (0)
122
#if PLATFORM_MUST_ALIGN == 0 /* ok to use "fast" versions? */
123
#define Skein_Put64_LSB_First(dst08,src64,bCnt) memcpy(dst08,src64,bCnt)
124
#define Skein_Get64_LSB_First(dst64,src08,wCnt) memcpy(dst64,src08,8*(wCnt))
125
#endif
126
#else
127
#error "Skein needs endianness setting!"
128
#endif
129
130
#endif /* ifndef SKEIN_NEED_SWAP */
131
132
/*
133
******************************************************************
134
* Provide any definitions still needed.
135
******************************************************************
136
*/
137
#ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */
138
#if SKEIN_NEED_SWAP
139
#define Skein_Swap64(w64) \
140
( (( ((u64b_t)(w64)) & 0xFF) << 56) | \
141
(((((u64b_t)(w64)) >> 8) & 0xFF) << 48) | \
142
(((((u64b_t)(w64)) >>16) & 0xFF) << 40) | \
143
(((((u64b_t)(w64)) >>24) & 0xFF) << 32) | \
144
(((((u64b_t)(w64)) >>32) & 0xFF) << 24) | \
145
(((((u64b_t)(w64)) >>40) & 0xFF) << 16) | \
146
(((((u64b_t)(w64)) >>48) & 0xFF) << 8) | \
147
(((((u64b_t)(w64)) >>56) & 0xFF) ) )
148
#else
149
#define Skein_Swap64(w64) (w64)
150
#endif
151
#endif /* ifndef Skein_Swap64 */
152
153
154
#ifndef Skein_Put64_LSB_First
155
void Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt)
156
#ifdef SKEIN_PORT_CODE /* instantiate the function code here? */
157
{ /* this version is fully portable (big-endian or little-endian), but slow */
158
size_t n;
159
160
for (n=0;n<bCnt;n++)
161
dst[n] = (u08b_t) (src[n>>3] >> (8*(n&7)));
162
}
163
#else
164
; /* output only the function prototype */
165
#endif
166
#endif /* ifndef Skein_Put64_LSB_First */
167
168
169
#ifndef Skein_Get64_LSB_First
170
void Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt)
171
#ifdef SKEIN_PORT_CODE /* instantiate the function code here? */
172
{ /* this version is fully portable (big-endian or little-endian), but slow */
173
size_t n;
174
175
for (n=0;n<8*wCnt;n+=8)
176
dst[n/8] = (((u64b_t) src[n ]) ) +
177
(((u64b_t) src[n+1]) << 8) +
178
(((u64b_t) src[n+2]) << 16) +
179
(((u64b_t) src[n+3]) << 24) +
180
(((u64b_t) src[n+4]) << 32) +
181
(((u64b_t) src[n+5]) << 40) +
182
(((u64b_t) src[n+6]) << 48) +
183
(((u64b_t) src[n+7]) << 56) ;
184
}
185
#else
186
; /* output only the function prototype */
187
#endif
188
#endif /* ifndef Skein_Get64_LSB_First */
189
190
#endif /* ifndef _SKEIN_PORT_H_ */
191
192