|
| 1 | +#include "simd_fill.h" |
| 2 | + |
| 3 | +#if defined(HAVE_IMMINTRIN_H) && !defined(SDL_DISABLE_IMMINTRIN_H) |
| 4 | +#include <immintrin.h> |
| 5 | +#endif /* defined(HAVE_IMMINTRIN_H) && !defined(SDL_DISABLE_IMMINTRIN_H) */ |
| 6 | + |
| 7 | +#define BAD_AVX2_FUNCTION_CALL \ |
| 8 | + printf( \ |
| 9 | + "Fatal Error: Attempted calling an AVX2 function when both compile " \ |
| 10 | + "time and runtime support is missing. If you are seeing this " \ |
| 11 | + "message, you have stumbled across a pygame bug, please report it " \ |
| 12 | + "to the devs!"); \ |
| 13 | + PG_EXIT(1) |
| 14 | + |
| 15 | +/* helper function that does a runtime check for AVX2. It has the added |
| 16 | + * functionality of also returning 0 if compile time support is missing */ |
| 17 | +int |
| 18 | +_pg_has_avx2() |
| 19 | +{ |
| 20 | +#if defined(__AVX2__) && defined(HAVE_IMMINTRIN_H) && \ |
| 21 | + !defined(SDL_DISABLE_IMMINTRIN_H) |
| 22 | + return SDL_HasAVX2(); |
| 23 | +#else |
| 24 | + return 0; |
| 25 | +#endif /* defined(__AVX2__) && defined(HAVE_IMMINTRIN_H) && \ |
| 26 | + !defined(SDL_DISABLE_IMMINTRIN_H) */ |
| 27 | +} |
| 28 | + |
| 29 | +#define SETUP_AVX2_FILLER(COLOR_PROCESS_CODE) \ |
| 30 | + /* initialize surface data */ \ |
| 31 | + int width = rect->w, height = rect->h; \ |
| 32 | + int skip = surface->pitch / 4 - width; \ |
| 33 | + /* indicates the number of pixels that can't be processed in 8-pixel \ |
| 34 | + * blocks */ \ |
| 35 | + int pxl_excess = width % 8; \ |
| 36 | + /* indicates the number of 8-pixel blocks that can be processed */ \ |
| 37 | + int n_iters_8 = width / 8; \ |
| 38 | + int i; \ |
| 39 | + /* load pixel data */ \ |
| 40 | + Uint32 *pixels = \ |
| 41 | + (Uint32 *)surface->pixels + rect->y * (surface->pitch / 4) + rect->x; \ |
| 42 | + \ |
| 43 | + __m256i mm256_dst; \ |
| 44 | + __m256i mask = \ |
| 45 | + _mm256_set_epi32(0, pxl_excess > 6 ? -1 : 0, pxl_excess > 5 ? -1 : 0, \ |
| 46 | + pxl_excess > 4 ? -1 : 0, pxl_excess > 3 ? -1 : 0, \ |
| 47 | + pxl_excess > 2 ? -1 : 0, pxl_excess > 1 ? -1 : 0, \ |
| 48 | + pxl_excess > 0 ? -1 : 0); \ |
| 49 | + /* prep and load the color */ \ |
| 50 | + Uint32 amask = surface->format->Amask; \ |
| 51 | + if (amask) { \ |
| 52 | + { \ |
| 53 | + COLOR_PROCESS_CODE \ |
| 54 | + } \ |
| 55 | + } \ |
| 56 | + __m256i mm256_color = _mm256_set1_epi32(color); |
| 57 | + |
| 58 | +#define RUN_AVX2_FILLER(FILL_CODE) \ |
| 59 | + while (height--) { \ |
| 60 | + for (i = 0; i < n_iters_8; i++) { \ |
| 61 | + /* load 8 pixels */ \ |
| 62 | + mm256_dst = _mm256_loadu_si256((__m256i *)pixels); \ |
| 63 | + \ |
| 64 | + {FILL_CODE} \ |
| 65 | + \ |
| 66 | + /* store 8 pixels */ \ |
| 67 | + _mm256_storeu_si256((__m256i *)pixels, mm256_dst); \ |
| 68 | + \ |
| 69 | + pixels += 8; \ |
| 70 | + } \ |
| 71 | + \ |
| 72 | + if (pxl_excess) { \ |
| 73 | + /* load up to 7 pixels */ \ |
| 74 | + mm256_dst = _mm256_maskload_epi32((int *)pixels, mask); \ |
| 75 | + \ |
| 76 | + {FILL_CODE} \ |
| 77 | + \ |
| 78 | + /* store up to 7 pixels */ \ |
| 79 | + _mm256_maskstore_epi32((int *)pixels, mask, mm256_dst); \ |
| 80 | + \ |
| 81 | + pixels += pxl_excess; \ |
| 82 | + } \ |
| 83 | + \ |
| 84 | + pixels += skip; \ |
| 85 | + } |
| 86 | + |
| 87 | +#if defined(__AVX2__) && defined(HAVE_IMMINTRIN_H) && \ |
| 88 | + !defined(SDL_DISABLE_IMMINTRIN_H) |
| 89 | +int |
| 90 | +surface_fill_blend_add_avx2(SDL_Surface *surface, SDL_Rect *rect, Uint32 color) |
| 91 | +{ |
| 92 | + SETUP_AVX2_FILLER({ color &= ~amask; }) |
| 93 | + RUN_AVX2_FILLER({ mm256_dst = _mm256_adds_epu8(mm256_dst, mm256_color); }); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +int |
| 98 | +surface_fill_blend_rgba_add_avx2(SDL_Surface *surface, SDL_Rect *rect, |
| 99 | + Uint32 color) |
| 100 | +{ |
| 101 | + SETUP_AVX2_FILLER({}) |
| 102 | + RUN_AVX2_FILLER({ mm256_dst = _mm256_adds_epu8(mm256_dst, mm256_color); }); |
| 103 | + return 0; |
| 104 | +} |
| 105 | +#else |
| 106 | +int |
| 107 | +surface_fill_blend_add_avx2(SDL_Surface *surface, SDL_Rect *rect, Uint32 color) |
| 108 | +{ |
| 109 | + BAD_AVX2_FUNCTION_CALL; |
| 110 | + return -1; |
| 111 | +} |
| 112 | + |
| 113 | +int |
| 114 | +surface_fill_blend_rgba_add_avx2(SDL_Surface *surface, SDL_Rect *rect, |
| 115 | + Uint32 color) |
| 116 | +{ |
| 117 | + BAD_AVX2_FUNCTION_CALL; |
| 118 | + return -1; |
| 119 | +} |
| 120 | +#endif /* defined(__AVX2__) && defined(HAVE_IMMINTRIN_H) && \ |
| 121 | + !defined(SDL_DISABLE_IMMINTRIN_H) */ |
0 commit comments