]> git.sesse.net Git - casparcg/blob - dependencies64/asmlib/asmlib.h
[2.1.0 Beta 2] Prepared for release of Beta 2.
[casparcg] / dependencies64 / asmlib / asmlib.h
1 /*************************** asmlib.h ***************************************
2 * Author:        Agner Fog
3 * Date created:  2003-12-12
4 * Last modified: 2011-08-21
5 * Project:       asmlib.zip
6 * Source URL:    www.agner.org/optimize
7 *
8 * Description:
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.
12 *
13 * Copyright 2003 - 2011 by Agner Fog. 
14 * GNU General Public License http://www.gnu.org/licenses/gpl.html
15 *****************************************************************************/
16
17
18 #ifndef ASMLIB_H
19 #define ASMLIB_H
20
21
22 /***********************************************************************
23 Define compiler-specific types and directives
24 ***********************************************************************/
25
26 // Define type size_t
27 #ifndef _SIZE_T_DEFINED
28 #include "stddef.h"
29 #endif
30
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
35   #include <stdint.h>
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
46 #else
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
55 #endif
56
57
58 // Turn off name mangling
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62
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
86
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)
100 #else
101    uint32_t ReadTSC(void);                                     // Read microprocessor internal clock (only 32 bits supported by compiler)
102 #endif
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);}
106
107 #ifdef __cplusplus
108 }  // end of extern "C"
109
110 // Define overloaded versions if compiling as C++
111
112 static inline int Round (double x) {   // Overload name Round
113    return RoundD(x);}
114 static inline int Round (float  x) {   // Overload name Round
115    return RoundF(x);}
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
118
119 #endif // __cplusplus
120
121
122 /***********************************************************************
123 Function prototypes, integer division functions
124 ***********************************************************************/
125
126 // Turn off name mangling
127 #ifdef __cplusplus
128 extern "C" {
129 #endif
130
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
135
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 
139 #endif
140
141 #if defined(_INCLUDED_EMM) || (defined(_EMMINTRIN_H_INCLUDED) && defined(__SSE2__))
142 #define VECTORDIVISIONDEFINED
143
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
149
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
155 #endif
156
157 #ifdef __cplusplus
158 }  // end of extern "C"
159
160 // Define classes and operator '/' for fast division with fixed divisor
161 class div_i32;
162 class div_u32;
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);
165
166 class div_i32 {                                                // Signed 32 bit integer division
167 public:
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
171 protected:
172    int buffer[2];                                              // Internal memory
173 friend int32_t operator / (int32_t x, div_i32 const & D);
174 };
175 static inline int32_t operator / (int32_t x, div_i32 const &D){// Overloaded operator '/'
176    return dividefixedi32(D.buffer, x);}
177
178 class div_u32 {                                                // Unsigned 32 bit integer division
179 public:
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
183 protected:
184    uint32_t buffer[2];                                         // Internal memory
185 friend uint32_t operator / (uint32_t x, div_u32 const & D);
186 };
187 static inline uint32_t operator / (uint32_t x, div_u32 const & D) { // Overloaded operator '/'
188    return dividefixedu32(D.buffer, x);}
189
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);
200
201 class div_v8i16 {                                              // vector of 8 signed integers of 16 bits
202 public:
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
206 protected:
207    __m128i buffer[2];                                          // Internal memory
208 friend __m128i operator / (__m128i x, div_v8i16 const & D);
209 };
210 static inline __m128i operator / (__m128i x, div_v8i16 const &D){// Overloaded operator '/'
211    return dividefixedV8i16(D.buffer, x);}
212
213 class div_v8u16 {                                              // vector of 8 unsigned integers of 16 bits
214 public:
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
218 protected:
219    __m128i buffer[2];                                          // Internal memory
220 friend __m128i operator / (__m128i x, div_v8u16 const & D);
221 };
222 static inline __m128i operator / (__m128i x, div_v8u16 const &D){// Overloaded operator '/'
223    return dividefixedV8u16(D.buffer, x);}
224
225 class div_v4i32 {                                              // vector of 4 signed integers of 32 bits
226 public:
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
230 protected:
231    __m128i buffer[2];                                          // Internal memory
232 friend __m128i operator / (__m128i x, div_v4i32 const & D);
233 };
234 static inline __m128i operator / (__m128i x, div_v4i32 const &D){// Overloaded operator '/'
235    return dividefixedV4i32(D.buffer, x);}
236
237 class div_v4u32 {                                              // vector of 4 unsigned integers of 32 bits
238 public:
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
242 protected:
243    __m128i buffer[2];                                          // Internal memory
244 friend __m128i operator / (__m128i x, div_v4u32 const & D);
245 };
246 static inline __m128i operator / (__m128i x, div_v4u32 const &D){// Overloaded operator '/'
247    return dividefixedV4u32(D.buffer, x);}
248
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
260
261 #endif // VECTORDIVISIONDEFINED
262
263 #endif // __cplusplus
264
265 #endif // ASMLIB_H