| 1 | package SDL2::version 0.01 { | ||
| 2 | use SDL2::Utils; | ||
| 3 | attach cpuinfo => { | ||
| 4 | SDL_GetCPUCount => [ [], 'int' ], | ||
| 5 | SDL_GetCPUCacheLineSize => [ [], 'int' ], | ||
| 6 | SDL_HasRDTSC => [ [], 'SDL_bool' ], | ||
| 7 | SDL_HasAltiVec => [ [], 'SDL_bool' ], | ||
| 8 | SDL_HasMMX => [ [], 'SDL_bool' ], | ||
| 9 | SDL_Has3DNow => [ [], 'SDL_bool' ], | ||
| 10 | SDL_HasSSE => [ [], 'SDL_bool' ], | ||
| 11 | SDL_HasSSE2 => [ [], 'SDL_bool' ], | ||
| 12 | SDL_HasSSE3 => [ [], 'SDL_bool' ], | ||
| 13 | SDL_HasSSE41 => [ [], 'SDL_bool' ], | ||
| 14 | SDL_HasSSE42 => [ [], 'SDL_bool' ], | ||
| 15 | SDL_HasAVX => [ [], 'SDL_bool' ], | ||
| 16 | SDL_HasAVX2 => [ [], 'SDL_bool' ], | ||
| 17 | SDL_HasAVX512F => [ [], 'SDL_bool' ], | ||
| 18 | SDL_HasARMSIMD => [ [], 'SDL_bool' ], | ||
| 19 | SDL_HasNEON => [ [], 'SDL_bool' ], | ||
| 20 | SDL_GetSystemRAM => [ [], 'int' ], | ||
| 21 | SDL_SIMDGetAlignment => [ [], 'int' ], | ||
| 22 | SDL_SIMDAlloc => [ ['int'], 'opaque' ], | ||
| 23 | SDL_SIMDRealloc => [ [ 'opaque', 'int' ], 'opaque' ], | ||
| 24 | SDL_SIMDFree => [ ['opaque'] ], | ||
| 25 | }; | ||
| 26 | |||
| 27 | =encoding utf-8 | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | SDL2::cpuinfo - CPU feature detection for SDL | ||
| 32 | |||
| 33 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | use SDL2 qw[:cpuinfo]; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | SDL2::version represents the library's version as three levels: major, minor, | ||
| 40 | and patch level. | ||
| 41 | |||
| 42 | =head1 Functions | ||
| 43 | |||
| 44 | These functions may be imported with the C<:cpuinfo> tag. | ||
| 45 | |||
| 46 | =head2 C<SDL_GetCPUCount( )> | ||
| 47 | |||
| 48 | Get the number of CPU cores available. | ||
| 49 | |||
| 50 | my $cores = SDL_GetCPUCount( ); | ||
| 51 | |||
| 52 | Returns the total number of logical CPU cores. On CPUs that include | ||
| 53 | technologies such as hyperthreading, the number of logical cores may be more | ||
| 54 | than the number of physical cores. | ||
| 55 | |||
| 56 | =head2 C<SDL_GetCPUCacheLineSize( )> | ||
| 57 | |||
| 58 | Determine the L1 cache line size of the CPU. | ||
| 59 | |||
| 60 | my $cache = SDL_GetCPUCacheLineSize( ); | ||
| 61 | |||
| 62 | This is useful for determining multi-threaded structure padding or SIMD | ||
| 63 | prefetch sizes. | ||
| 64 | |||
| 65 | Returns the L1 cache line size of the CPU, in bytes. | ||
| 66 | |||
| 67 | =head2 C<SDL_HasRDTSC( )> | ||
| 68 | |||
| 69 | Determine whether the CPU has the RDTSC instruction. | ||
| 70 | |||
| 71 | my $rdtsc = SDL_HasRDTSC( ); | ||
| 72 | |||
| 73 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 74 | |||
| 75 | Returns C<SDL_TRUE> if the CPU has the RDTSC instruction or C<SDL_FALSE> if | ||
| 76 | not. | ||
| 77 | |||
| 78 | =head2 C<SDL_HasAltiVec( )> | ||
| 79 | |||
| 80 | Determine whether the CPU has AltiVec features. | ||
| 81 | |||
| 82 | my $altiVec = SDL_HasAltiVec( ); | ||
| 83 | |||
| 84 | This always returns false on CPUs that aren't using PowerPC instruction sets. | ||
| 85 | |||
| 86 | Returns C<SDL_TRUE> if the CPU has AltiVec features or C<SDL_FALSE> if not. | ||
| 87 | |||
| 88 | =head2 C<SDL_HasMMX( )> | ||
| 89 | |||
| 90 | Determine whether the CPU has MMX features. | ||
| 91 | |||
| 92 | my $mmx = SDL_HasMMX( ); | ||
| 93 | |||
| 94 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 95 | |||
| 96 | Returns C<SDL_TRUE> if the CPU has MMX features or C<SDL_FALSE> if not. | ||
| 97 | |||
| 98 | =head2 C<SDL_Has3DNow( )> | ||
| 99 | |||
| 100 | Determine whether the CPU has 3DNow! features. | ||
| 101 | |||
| 102 | my $_3dnow = SDL_Has3DNow( ); | ||
| 103 | |||
| 104 | This always returns false on CPUs that aren't using AMD instruction sets. | ||
| 105 | |||
| 106 | Returns C<SDL_TRUE> if the CPU has 3DNow! features or C<SDL_FALSE> if not. | ||
| 107 | |||
| 108 | =head2 C<SDL_HasSSE( )> | ||
| 109 | |||
| 110 | Determine whether the CPU has SSE features. | ||
| 111 | |||
| 112 | my $sse = SDL_HasSSE( ); | ||
| 113 | |||
| 114 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 115 | |||
| 116 | Returns C<SDL_TRUE> if the CPU has SSE features or C<SDL_FALSE> if not. | ||
| 117 | |||
| 118 | =head2 C<SDL_HasSSE2( )> | ||
| 119 | |||
| 120 | Determine whether the CPU has SSE2 features. | ||
| 121 | |||
| 122 | my $sse2 = SDL_HasSSE2( ); | ||
| 123 | |||
| 124 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 125 | |||
| 126 | Returns C<SDL_TRUE> if the CPU has SSE2 features or C<SDL_FALSE> if not. | ||
| 127 | |||
| 128 | =head2 C<SDL_HasSSE3( )> | ||
| 129 | |||
| 130 | Determine whether the CPU has SSE3 features. | ||
| 131 | |||
| 132 | my $sse3 = SDL_HasSSE3( ); | ||
| 133 | |||
| 134 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 135 | |||
| 136 | Returns C<SDL_TRUE> if the CPU has SSE3 features or C<SDL_FALSE> if not. | ||
| 137 | |||
| 138 | =head2 C<SDL_HasSSE41( )> | ||
| 139 | |||
| 140 | Determine whether the CPU has SSE4.1 features. | ||
| 141 | |||
| 142 | my $sse41 = SDL_HasSSE41( ); | ||
| 143 | |||
| 144 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 145 | |||
| 146 | Returns C<SDL_TRUE> if the CPU has SSE4.1 features or C<SDL_FALSE> if not. | ||
| 147 | |||
| 148 | =head2 C<SDL_HasSSE42( )> | ||
| 149 | |||
| 150 | Determine whether the CPU has SSE4.2 features. | ||
| 151 | |||
| 152 | my $sse42 = SDL_HasSSE42( ); | ||
| 153 | |||
| 154 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 155 | |||
| 156 | Returns C<SDL_TRUE> if the CPU has SSE4.2 features or C<SDL_FALSE> if not. | ||
| 157 | |||
| 158 | =head2 C<SDL_HasAVX( )> | ||
| 159 | |||
| 160 | Determine whether the CPU has AVX features. | ||
| 161 | |||
| 162 | my $avx = SDL_HasAVX( ); | ||
| 163 | |||
| 164 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 165 | |||
| 166 | Returns C<SDL_TRUE> if the CPU has AVX features or C<SDL_FALSE> if not. | ||
| 167 | |||
| 168 | =head2 C<SDL_HasAVX2( )> | ||
| 169 | |||
| 170 | Determine whether the CPU has AVX2 features. | ||
| 171 | |||
| 172 | my $avx2 = SDL_HasAVX2( ); | ||
| 173 | |||
| 174 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 175 | |||
| 176 | Returns C<SDL_TRUE> if the CPU has AVX2 features or C<SDL_FALSE> if not. | ||
| 177 | |||
| 178 | =head2 C<SDL_HasAVX512F( )> | ||
| 179 | |||
| 180 | Determine whether the CPU has AVX-512F (foundation) features. | ||
| 181 | |||
| 182 | my $avx512 = SDL_HasAVX512F( ); | ||
| 183 | |||
| 184 | This always returns false on CPUs that aren't using Intel instruction sets. | ||
| 185 | |||
| 186 | Returns C<SDL_TRUE> if the CPU has AVX-512F features or C<SDL_FALSE> if not. | ||
| 187 | |||
| 188 | =head2 C<SDL_HasARMSIMD( )> | ||
| 189 | |||
| 190 | Determine whether the CPU has ARM SIMD (ARMv6) features. | ||
| 191 | |||
| 192 | my $arm6 = SDL_HasARMSIMD( ); | ||
| 193 | |||
| 194 | This is different from ARM NEON, which is a different instruction set. | ||
| 195 | |||
| 196 | This always returns false on CPUs that aren't using ARM instruction sets. | ||
| 197 | |||
| 198 | Returns C<SDL_TRUE> if the CPU has ARM SIMD features or C<SDL_FALSE> if not. | ||
| 199 | |||
| 200 | =head2 C<SDL_HasNEON( )> | ||
| 201 | |||
| 202 | Determine whether the CPU has NEON (ARM SIMD) features. | ||
| 203 | |||
| 204 | my $neon = SDL_HasNEON( ); | ||
| 205 | |||
| 206 | This always returns false on CPUs that aren't using ARM instruction sets. | ||
| 207 | |||
| 208 | Returns C<SDL_TRUE> if the CPU has ARM NEON features or C<SDL_FALSE> if not. | ||
| 209 | |||
| 210 | =head2 C<SDL_GetSystemRAM( )> | ||
| 211 | |||
| 212 | Get the amount of RAM configured in the system. | ||
| 213 | |||
| 214 | my $mb = SDL_GetSystemRAM( ); | ||
| 215 | |||
| 216 | Returns the amount of RAM configured in the system in MB. | ||
| 217 | |||
| 218 | =head2 C<SDL_SIMDGetAlignment( )> | ||
| 219 | |||
| 220 | Report the alignment this system needs for SIMD allocations. | ||
| 221 | |||
| 222 | my $size = SDL_SIMDGetAlignment( ); | ||
| 223 | |||
| 224 | This will return the minimum number of bytes to which a pointer must be aligned | ||
| 225 | to be compatible with SIMD instructions on the current machine. For example, if | ||
| 226 | the machine supports SSE only, it will return 16, but if it supports AVX-512F, | ||
| 227 | it'll return 64 (etc). This only reports values for instruction sets SDL knows | ||
| 228 | about, so if your SDL build doesn't have L<< C<SDL_HasAVX512F( | ||
| 229 | )>|/C<SDL_HasAVX512F( )> >>, then it might return 16 for the SSE support it | ||
| 230 | sees and not 64 for the AVX-512 instructions that exist but SDL doesn't know | ||
| 231 | about. Plan accordingly. | ||
| 232 | |||
| 233 | Returns alignment in bytes needed for available, known SIMD instructions. | ||
| 234 | |||
| 235 | =head2 C<SDL_SIMDAlloc( ... )> | ||
| 236 | |||
| 237 | Allocate memory in a SIMD-friendly way. | ||
| 238 | |||
| 239 | my $ptr = SDL_SIMDAlloc( 1024 * 64 ); | ||
| 240 | |||
| 241 | This will allocate a block of memory that is suitable for use with SIMD | ||
| 242 | instructions. Specifically, it will be properly aligned and padded for the | ||
| 243 | system's supported vector instructions. | ||
| 244 | |||
| 245 | The memory returned will be padded such that it is safe to read or write an | ||
| 246 | incomplete vector at the end of the memory block. This can be useful so you | ||
| 247 | don't have to drop back to a scalar fallback at the end of your SIMD processing | ||
| 248 | loop to deal with the final elements without overflowing the allocated buffer. | ||
| 249 | |||
| 250 | You must free this memory with L<< C<SDL_SIMDFree( )>|/C<SDL_SIMDFree( )> >>, | ||
| 251 | not C<SDL_free( ... )>, C<undef>, variable scope tricks, etc. | ||
| 252 | |||
| 253 | Note that SDL will only deal with SIMD instruction sets it is aware of; for | ||
| 254 | example, SDL 2.0.8 knows that SSE wants 16-byte vectors (L<< C<SDL_HasSSE( | ||
| 255 | )>|/C<SDL_HasSSE( )> >>), and AVX2 wants 32 bytes (L<< C<SDL_HasAVX2( | ||
| 256 | )>|/C<SDL_HasAVX2( )> >>), but doesn't know that AVX-512 wants 64. To be clear: | ||
| 257 | if you can't decide to use an instruction set with an C<SDL_Has*( )> function, | ||
| 258 | don't use that instruction set with memory allocated through here. | ||
| 259 | |||
| 260 | C<SDL_AllocSIMD( 0 )> will return a non-NULL pointer, assuming the system isn't | ||
| 261 | out of memory, but you are not allowed to dereference it (because you only own | ||
| 262 | zero bytes of that buffer). | ||
| 263 | |||
| 264 | Expected parameters include: | ||
| 265 | |||
| 266 | =over | ||
| 267 | |||
| 268 | =item C<len> - The length, in bytes, of the block to allocate. The actual allocated block might be larger due to padding, etc. | ||
| 269 | |||
| 270 | =back | ||
| 271 | |||
| 272 | Returns a pointer to newly-allocated block, NULL if out of memory. | ||
| 273 | |||
| 274 | =head2 C<SDL_SIMDRealloc( ... )> | ||
| 275 | |||
| 276 | Reallocate memory obtained from L<< C<SDL_SIMDAlloc( ... )>|/C<SDL_SIMDAlloc( | ||
| 277 | ... )> >>. | ||
| 278 | |||
| 279 | $ptr = SDL_SIMDRealloc( $ptr, 1024 * 32 ); | ||
| 280 | |||
| 281 | It is not valid to use this function on a pointer from anything but L<< | ||
| 282 | C<SDL_SIMDAlloc( ... )>|/C<SDL_SIMDAlloc( ... )> >>. It can't be used on | ||
| 283 | pointers from malloc, realloc, L<< C<SDL_malloc( ... | ||
| 284 | )>|SDL2::stdinc/C<SDL_malloc( ... )> >>, memalign, new, etc. | ||
| 285 | |||
| 286 | Expected parameters include: | ||
| 287 | |||
| 288 | =over | ||
| 289 | |||
| 290 | =item C<mem> - The pointer obtained from L<< C<SDL_SIMDAlloc( ... )>|/C<SDL_SIMDAlloc( ... )> >>. This function also accepts NULL, at which point this function is the same as calling L<< C<SDL_SIMDAlloc( ... )>|/C<SDL_SIMDAlloc( ... )> >> with a NULL pointer. | ||
| 291 | |||
| 292 | =item C<len> - The length, in bytes, of the block to allocated. The actual allocated block might be larger due to padding, etc. Passing C<0> will return a non-NULL pointer, assuming the system isn't out of memory. | ||
| 293 | |||
| 294 | =back | ||
| 295 | |||
| 296 | Returns a pointer to newly-reallocated block, NULL if out of memory. | ||
| 297 | |||
| 298 | =head2 C<SDL_SIMDFree( )> | ||
| 299 | |||
| 300 | Deallocate memory obtained from L<< C<SDL_SIMDAlloc( ... )>|/C<SDL_SIMDAlloc( | ||
| 301 | ... )> >>. | ||
| 302 | |||
| 303 | SDL_SIMDFree( $ptr ); | ||
| 304 | |||
| 305 | It is not valid to use this function on a pointer from anything but L<< | ||
| 306 | C<SDL_SIMDAlloc( ... )>|/C<SDL_SIMDAlloc( ... )> >> or L<< C<SDL_SIMDRealloc( | ||
| 307 | ... )>|/C<SDL_SIMDRealloc( ... )> >>. It can't be used on pointers from malloc, | ||
| 308 | realloc, SDL_malloc, memalign, new, etc. | ||
| 309 | |||
| 310 | However, C<SDL_SIMDFree( undef )> is a legal no-op. | ||
| 311 | |||
| 312 | The memory pointed to by C<ptr> is no longer valid for access upon return, and | ||
| 313 | may be returned to the system or reused by a future allocation. The pointer | ||
| 314 | passed to this function is no longer safe to dereference once this function | ||
| 315 | returns, and should be discarded. | ||
| 316 | |||
| 317 | Expected parameters include: | ||
| 318 | |||
| 319 | =over | ||
| 320 | |||
| 321 | =item C<ptr> - The pointer, returned from L<< C<SDL_SIMDAlloc( ... )>|/C<SDL_SIMDAlloc( ... )> >> or L<< C<SDL_SIMDRealloc( ... )>|/C<SDL_SIMDRealloc( ... )> >>, to deallocate. NULL is a legal no-op. | ||
| 322 | |||
| 323 | =back | ||
| 324 | |||
| 325 | =head1 LICENSE | ||
| 326 | |||
| 327 | Copyright (C) Sanko Robinson. | ||
| 328 | |||
| 329 | This library is free software; you can redistribute it and/or modify it under | ||
| 330 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 331 | conditions may apply to data transmitted through this module. | ||
| 332 | |||
| 333 | =head1 AUTHOR | ||
| 334 | |||
| 335 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 336 | |||
| 337 | =begin stopwords | ||
| 338 | |||
| 339 | hyperthreading prefetch PowerPC 3DNow ARMv6 (ARMv6) realloc memalign deallocate | ||
| 340 | |||
| 341 | =end stopwords | ||
| 342 | |||
| 343 | =cut | ||
| 344 | |||
| 345 | }; | ||
| 346 | 1; |