1 /*************************** asmlib.h ***************************************
3 * Date created: 2003-12-12
4 * Last modified: 2011-08-21
6 * Source URL: www.agner.org/optimize
9 * Header file for the asmlib function library.
10 * This library is available in many versions for different platforms.
11 * See asmlib-instructions.pdf for details.
13 * Copyright 2003 - 2011 by Agner Fog.
14 * GNU General Public License http://www.gnu.org/licenses/gpl.html
15 *****************************************************************************/
22 /***********************************************************************
23 Define compiler-specific types and directives
24 ***********************************************************************/
27 #ifndef _SIZE_T_DEFINED
31 // Define integer types with known size: int32_t, uint32_t, int64_t, uint64_t.
32 // If this doesn't work then insert compiler-specific definitions here:
33 #if defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1600)
34 // Compilers supporting C99 or C++0x have stdint.h defining these integer types
36 #define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
37 #elif defined(_MSC_VER)
38 // Older Microsoft compilers have their own definition
39 typedef signed __int16 int16_t;
40 typedef unsigned __int16 uint16_t;
41 typedef signed __int32 int32_t;
42 typedef unsigned __int32 uint32_t;
43 typedef signed __int64 int64_t;
44 typedef unsigned __int64 uint64_t;
45 #define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
47 // This works with most compilers
48 typedef signed short int int16_t;
49 typedef unsigned short int uint16_t;
50 typedef signed int int32_t;
51 typedef unsigned int uint32_t;
52 typedef long long int64_t;
53 typedef unsigned long long uint64_t;
54 #define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
58 // Turn off name mangling
63 /***********************************************************************
64 Function prototypes, memory and string functions
65 ***********************************************************************/
66 void * A_memcpy (void * dest, const void * src, size_t count); // Copy count bytes from src to dest
67 void * A_memmove(void * dest, const void * src, size_t count); // Same as memcpy, allows overlap between src and dest
68 void * A_memset (void * dest, int c, size_t count); // Set count bytes in dest to (char)c
69 size_t GetMemcpyCacheLimit(void); // Data blocks bigger than this will be copied uncached by memcpy and memmove
70 void SetMemcpyCacheLimit(size_t); // Change limit in GetMemcpyCacheLimit
71 size_t GetMemsetCacheLimit(void); // Data blocks bigger than this will be stored uncached by memset
72 void SetMemsetCacheLimit(size_t); // Change limit in GetMemsetCacheLimit
73 char * A_strcat (char * dest, const char * src); // Concatenate strings dest and src. Store result in dest
74 char * A_strcpy (char * dest, const char * src); // Copy string src to dest
75 size_t A_strlen (const char * str); // Get length of zero-terminated string
76 int A_strcmp (const char * a, const char * b); // Compare strings. Case sensitive
77 int A_stricmp (const char *string1, const char *string2); // Compare strings. Case insensitive for A-Z only
78 char * A_strstr (char * haystack, const char * needle); // Search for substring in string
79 void A_strtolower(char * string); // Convert string to lower case for A-Z only
80 void A_strtoupper(char * string); // Convert string to upper case for a-z only
81 size_t A_substring(char * dest, const char * source, size_t pos, size_t len); // Copy a substring for source into dest
82 size_t A_strspn (const char * str, const char * set); // Find span of characters that belong to set
83 size_t A_strcspn(const char * str, const char * set); // Find span of characters that don't belong to set
84 size_t strCountInSet(const char * str, const char * set); // Count characters that belong to set
85 size_t strcount_UTF8(const char * str); // Counts the number of characters in a UTF-8 encoded string
87 /***********************************************************************
88 Function prototypes, miscellaneous functions
89 ***********************************************************************/
90 uint32_t A_popcount(uint32_t x); // Count 1-bits in 32-bit integer
91 int RoundD (double x); // Round to nearest or even
92 int RoundF (float x); // Round to nearest or even
93 int InstructionSet(void); // Tell which instruction set is supported
94 char * ProcessorName(void); // ASCIIZ text describing microprocessor
95 void CpuType(int * vendor, int * family, int * model); // Get CPU vendor, family and model
96 size_t DataCacheSize(int level); // Get size of data cache
97 void A_DebugBreak(void); // Makes a debug breakpoint
98 #ifdef INT64_SUPPORTED
99 uint64_t ReadTSC(void); // Read microprocessor internal clock (64 bits)
101 uint32_t ReadTSC(void); // Read microprocessor internal clock (only 32 bits supported by compiler)
103 void cpuid_ex (int abcd[4], int eax, int ecx); // call CPUID instruction
104 static inline void cpuid_abcd (int abcd[4], int eax) {
105 cpuid_ex(abcd, eax, 0);}
108 } // end of extern "C"
110 // Define overloaded versions if compiling as C++
112 static inline int Round (double x) { // Overload name Round
114 static inline int Round (float x) { // Overload name Round
116 static inline const char * A_strstr(const char * haystack, const char * needle) {
117 return A_strstr((char*)haystack, needle);} // Overload A_strstr with const char * version
119 #endif // __cplusplus
122 /***********************************************************************
123 Function prototypes, integer division functions
124 ***********************************************************************/
126 // Turn off name mangling
131 void setdivisori32(int buffer[2], int d); // Set divisor for repeated division
132 int dividefixedi32(const int buffer[2], int x); // Fast division with previously set divisor
133 void setdivisoru32(uint32_t buffer[2], uint32_t d); // Set divisor for repeated division
134 uint32_t dividefixedu32(const uint32_t buffer[2], uint32_t x); // Fast division with previously set divisor
136 // Test if emmintrin.h is included and __m128i defined
137 #if defined(__GNUC__) && defined(_EMMINTRIN_H_INCLUDED) && !defined(__SSE2__)
138 #error Please compile with -sse2 or higher
141 #if defined(_INCLUDED_EMM) || (defined(_EMMINTRIN_H_INCLUDED) && defined(__SSE2__))
142 #define VECTORDIVISIONDEFINED
144 // define vector division functions for 16 bit signed and unsigned integers
145 void setdivisorV8i16(__m128i buf[2], int16_t d); // Set divisor for repeated division
146 __m128i dividefixedV8i16(const __m128i buf[2], __m128i x); // Fast division with previously set divisor
147 void setdivisorV8u16(__m128i buf[2], uint16_t d); // Set divisor for repeated division
148 __m128i dividefixedV8u16(const __m128i buf[2], __m128i x); // Fast division with previously set divisor
150 // define vector division functions for 32 bit signed and unsigned integers
151 void setdivisorV4i32(__m128i buf[2], int32_t d); // Set divisor for repeated division
152 __m128i dividefixedV4i32(const __m128i buf[2], __m128i x); // Fast division with previously set divisor
153 void setdivisorV4u32(__m128i buf[2], uint32_t d); // Set divisor for repeated division
154 __m128i dividefixedV4u32(const __m128i buf[2], __m128i x); // Fast division with previously set divisor
158 } // end of extern "C"
160 // Define classes and operator '/' for fast division with fixed divisor
163 static inline int32_t operator / (int32_t x, div_i32 const &D);
164 static inline uint32_t operator / (uint32_t x, div_u32 const & D);
166 class div_i32 { // Signed 32 bit integer division
168 div_i32() {buffer[0] = buffer[1] = 0;} // Default constructor
169 div_i32(int d) {setdivisor(d);} // Constructor with divisor
170 void setdivisor(int d) {setdivisori32(buffer, d);} // Set divisor
172 int buffer[2]; // Internal memory
173 friend int32_t operator / (int32_t x, div_i32 const & D);
175 static inline int32_t operator / (int32_t x, div_i32 const &D){// Overloaded operator '/'
176 return dividefixedi32(D.buffer, x);}
178 class div_u32 { // Unsigned 32 bit integer division
180 div_u32() {buffer[0] = buffer[1] = 0;} // Default constructor
181 div_u32(uint32_t d) {setdivisor(d);} // Constructor with divisor
182 void setdivisor(uint32_t d) {setdivisoru32(buffer, d);} // Set divisor
184 uint32_t buffer[2]; // Internal memory
185 friend uint32_t operator / (uint32_t x, div_u32 const & D);
187 static inline uint32_t operator / (uint32_t x, div_u32 const & D) { // Overloaded operator '/'
188 return dividefixedu32(D.buffer, x);}
190 #ifdef VECTORDIVISIONDEFINED
191 // Define classes and operator '/' for fast division of vectors with fixed divisor
192 class div_v8i16; // vector of 8 signed integers of 16 bits
193 class div_v8u16; // vector of 8 unsigned integers of 16 bits
194 class div_v4i32; // vector of 4 signed integers of 32 bits
195 class div_v4u32; // vector of 4 unsigned integers of 32 bits
196 static inline __m128i operator / (__m128i x, div_v8i16 const & D);
197 static inline __m128i operator / (__m128i x, div_v8u16 const & D);
198 static inline __m128i operator / (__m128i x, div_v4i32 const & D);
199 static inline __m128i operator / (__m128i x, div_v4u32 const & D);
201 class div_v8i16 { // vector of 8 signed integers of 16 bits
203 div_v8i16() {buffer[0] = buffer[1] = _mm_set1_epi16(0);} // default constructor
204 div_v8i16(int16_t d) {setdivisor(d);} // constructor with divisor
205 void setdivisor(int16_t d) {setdivisorV8i16(buffer, d);} // set divisor
207 __m128i buffer[2]; // Internal memory
208 friend __m128i operator / (__m128i x, div_v8i16 const & D);
210 static inline __m128i operator / (__m128i x, div_v8i16 const &D){// Overloaded operator '/'
211 return dividefixedV8i16(D.buffer, x);}
213 class div_v8u16 { // vector of 8 unsigned integers of 16 bits
215 div_v8u16() {buffer[0] = buffer[1] = _mm_set1_epi16(0);} // default constructor
216 div_v8u16(uint16_t d) {setdivisor(d);} // constructor with divisor
217 void setdivisor(uint16_t d) {setdivisorV8u16(buffer, d);} // set divisor
219 __m128i buffer[2]; // Internal memory
220 friend __m128i operator / (__m128i x, div_v8u16 const & D);
222 static inline __m128i operator / (__m128i x, div_v8u16 const &D){// Overloaded operator '/'
223 return dividefixedV8u16(D.buffer, x);}
225 class div_v4i32 { // vector of 4 signed integers of 32 bits
227 div_v4i32() {buffer[0] = buffer[1] = _mm_set1_epi32(0);} // default constructor
228 div_v4i32(int32_t d) {setdivisor(d);} // constructor with divisor
229 void setdivisor(int32_t d) {setdivisorV4i32(buffer, d);} // set divisor
231 __m128i buffer[2]; // Internal memory
232 friend __m128i operator / (__m128i x, div_v4i32 const & D);
234 static inline __m128i operator / (__m128i x, div_v4i32 const &D){// Overloaded operator '/'
235 return dividefixedV4i32(D.buffer, x);}
237 class div_v4u32 { // vector of 4 unsigned integers of 32 bits
239 div_v4u32() {buffer[0] = buffer[1] = _mm_set1_epi32(0);} // default constructor
240 div_v4u32(uint32_t d) {setdivisor(d);} // constructor with divisor
241 void setdivisor(uint32_t d) {setdivisorV4u32(buffer, d);} // set divisor
243 __m128i buffer[2]; // Internal memory
244 friend __m128i operator / (__m128i x, div_v4u32 const & D);
246 static inline __m128i operator / (__m128i x, div_v4u32 const &D){// Overloaded operator '/'
247 return dividefixedV4u32(D.buffer, x);}
249 // Support for vector classes defined in Intel's dvec.h
250 #ifdef _DVEC_H_INCLUDED
251 static inline Is32vec4 operator / (Is32vec4 const &x, div_v4i32 const &D){
252 return (__m128i)x / D;}
253 static inline Iu32vec4 operator / (Iu32vec4 const &x, div_v4u32 const &D){
254 return (__m128i)x / D;}
255 static inline Is16vec8 operator / (Is16vec8 const &x, div_v8i16 const &D){
256 return (__m128i)x / D;}
257 static inline Iu16vec8 operator / (Iu16vec8 const &x, div_v8u16 const &D){
258 return (__m128i)x / D;}
259 #endif // _DVEC_H_INCLUDED
261 #endif // VECTORDIVISIONDEFINED
263 #endif // __cplusplus