1 /* @(#)byte_order.c	1.4 15/11/22 2015 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)byte_order.c	1.4 15/11/22 2015 J. Schilling";
6 #endif
7 /*
8  * SHA3 hash code taken from
9  * https://github.com/rhash/RHash/tree/master/librhash
10  *
11  * Portions Copyright (c) 2015 J. Schilling
12  */
13 
14 /*
15  * byte_order.c - byte order related platform dependent routines,
16  *
17  * Copyright: 2008-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
18  *
19  * Permission is hereby granted,  free of charge,  to any person  obtaining a
20  * copy of this software and associated documentation files (the "Software"),
21  * to deal in the Software without restriction,  including without limitation
22  * the rights to  use, copy, modify,  merge, publish, distribute, sublicense,
23  * and/or sell copies  of  the Software,  and to permit  persons  to whom the
24  * Software is furnished to do so.
25  *
26  * This program  is  distributed  in  the  hope  that it will be useful,  but
27  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28  * or FITNESS FOR A PARTICULAR PURPOSE.  Use this program  at  your own risk!
29  */
30 #include "byte_order.h"
31 
32 #ifdef	HAVE_LONGLONG
33 
34 #if !(__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) /* if !GCC or GCC < 4.3 */
35 
36 #if _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64) /* if MSVC++ >= 2002 on x86/x64 */
37 #include <intrin.h>
38 #pragma intrinsic(_BitScanForward)
39 
40 /*
41  * Returns index of the trailing bit of x.
42  *
43  * @param x the number to process
44  * @return zero-based index of the trailing bit
45  */
46 unsigned
rhash_ctz(unsigned x)47 rhash_ctz(unsigned x)
48 {
49 	unsigned long index;
50 	unsigned char isNonzero = _BitScanForward(&index, x); /* MSVC intrinsic */
51 	return (isNonzero ? (unsigned)index : 0);
52 }
53 #else /* _MSC_VER >= 1300... */
54 
55 /*
56  * Returns index of the trailing bit of a 32-bit number.
57  * This is a plain C equivalent for GCC __builtin_ctz() bit scan.
58  *
59  * @param x the number to process
60  * @return zero-based index of the trailing bit
61  */
62 unsigned
rhash_ctz(x)63 rhash_ctz(x)
64 	unsigned	x;
65 {
66 	/* array for conversion to bit position */
67 	static unsigned char bit_pos[32] =  {
68 		0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
69 		31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
70 	};
71 
72 	/*
73 	 * The De Bruijn bit-scan was devised in 1997, according to Donald Knuth
74 	 * by Martin Lauter. The constant 0x077CB531UL is a De Bruijn sequence,
75 	 * which produces a unique pattern of bits into the high 5 bits for each
76 	 * possible bit position that it is multiplied against.
77 	 * See http://graphics.stanford.edu/~seander/bithacks.html
78 	 * and http://chessprogramming.wikispaces.com/BitScan
79 	 */
80 	return ((unsigned)bit_pos[((UInt32_t)((x & -x) * (unsigned)0x077CB531)) >> 27]);
81 }
82 #endif /* _MSC_VER >= 1300... */
83 #endif /* !(GCC >= 4.3) */
84 
85 /*
86  * Copy a memory block with simultaneous exchanging byte order.
87  * The byte order is changed from little-endian 32-bit integers
88  * to big-endian (or vice-versa).
89  *
90  * @param to the pointer where to copy memory block
91  * @param index the index to start writing from
92  * @param from  the source block to copy
93  * @param length length of the memory block
94  */
95 void
rhash_swap_copy_str_to_u32(to,idx,from,length)96 rhash_swap_copy_str_to_u32(to, idx, from, length)
97 	void	*to;
98 	int	idx;
99 	const void	*from;
100 	size_t	length;
101 {
102 	/* if all pointers and length are 32-bits aligned */
103 	if (0 == (((int)((char *)to - (char *)0) | ((char *)from - (char *)0) | idx | length) & 3)) {
104 		/* copy memory as 32-bit words */
105 		const UInt32_t *src = (const UInt32_t *)from;
106 		const UInt32_t *end = (const UInt32_t *)((const char *)src + length);
107 		UInt32_t *dst = (UInt32_t *)((char *)to + idx);
108 		while (src < end) *(dst++) = bswap_32(*(src++));
109 	} else {
110 		const char *src = (const char *)from;
111 		for (length += idx; (size_t)idx < length; idx++) ((char *)to)[idx ^ 3] = *(src++);
112 	}
113 }
114 
115 /*
116  * Copy a memory block with changed byte order.
117  * The byte order is changed from little-endian 64-bit integers
118  * to big-endian (or vice-versa).
119  *
120  * @param to     the pointer where to copy memory block
121  * @param index  the index to start writing from
122  * @param from   the source block to copy
123  * @param length length of the memory block
124  */
125 void
rhash_swap_copy_str_to_u64(to,idx,from,length)126 rhash_swap_copy_str_to_u64(to, idx, from, length)
127 	void	*to;
128 	int	idx;
129 	const void	*from;
130 	size_t	length;
131 {
132 	/* if all pointers and length are 64-bits aligned */
133 	if (0 == (((int)((char *)to - (char *)0) | ((char *)from - (char *)0) | idx | length) & 7)) {
134 		/* copy aligned memory block as 64-bit integers */
135 		const UInt64_t *src = (const UInt64_t *)from;
136 		const UInt64_t *end = (const UInt64_t *)((const char *)src + length);
137 		UInt64_t *dst = (UInt64_t *)((char *)to + idx);
138 		while (src < end) *(dst++) = bswap_64(*(src++));
139 	} else {
140 		const char *src = (const char *)from;
141 		for (length += idx; (size_t)idx < length; idx++) ((char *)to)[idx ^ 7] = *(src++);
142 	}
143 }
144 
145 /*
146  * Copy data from a sequence of 64-bit words to a binary string of given length,
147  * while changing byte order.
148  *
149  * @param to     the binary string to receive data
150  * @param from   the source sequence of 64-bit words
151  * @param length the size in bytes of the data being copied
152  */
153 void
rhash_swap_copy_u64_to_str(to,from,length)154 rhash_swap_copy_u64_to_str(to, from, length)
155 	void	*to;
156 	const void	*from;
157 	size_t	length;
158 {
159 	/* if all pointers and length are 64-bits aligned */
160 	if (0 == (((int)((char *)to - (char *)0) | ((char *)from - (char *)0) | length) & 7)) {
161 		/* copy aligned memory block as 64-bit integers */
162 		const UInt64_t *src = (const UInt64_t *)from;
163 		const UInt64_t *end = (const UInt64_t *)((const char *)src + length);
164 		UInt64_t *dst = (UInt64_t *)to;
165 		while (src < end) *(dst++) = bswap_64(*(src++));
166 	} else {
167 		size_t idx;
168 		char *dst = (char *)to;
169 		for (idx = 0; idx < length; idx++) *(dst++) = ((char *)from)[idx ^ 7];
170 	}
171 }
172 
173 /*
174  * Exchange byte order in the given array of 32-bit integers.
175  *
176  * @param arr    the array to process
177  * @param length array length
178  */
179 void
rhash_u32_mem_swap(arr,length)180 rhash_u32_mem_swap(arr, length)
181 	unsigned	*arr;
182 	int		length;
183 {
184 	unsigned *end = arr + length;
185 	for (; arr < end; arr++) {
186 		*arr = bswap_32(*arr);
187 	}
188 }
189 
190 #endif	/* HAVE_LONGLONG */
191