ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
video/ascii/avx2/common.c
Go to the documentation of this file.
1
5
6#include <stdint.h>
7#include <ascii-chat/common.h>
9
10#if SIMD_SUPPORT_AVX2
11#include <immintrin.h>
12
13// Thread-local storage for AVX2 working buffers
14// These stay in L1 cache and are reused across function calls
15// Non-static for shared library compatibility (still thread-local)
16THREAD_LOCAL _Alignas(32) uint8_t avx2_r_buffer[32];
17THREAD_LOCAL _Alignas(32) uint8_t avx2_g_buffer[32];
18THREAD_LOCAL _Alignas(32) uint8_t avx2_b_buffer[32];
19THREAD_LOCAL _Alignas(32) uint8_t avx2_luminance_buffer[32];
20
21// Optimized AVX2 function to load 32 RGB pixels and separate channels
22// Uses simple loop that auto-vectorizes to VMOVDQU + VPSHUFB
23void avx2_load_rgb32_optimized(const rgb_pixel_t *__restrict pixels, uint8_t *__restrict r_out, uint8_t *__restrict g_out, uint8_t *__restrict b_out) {
24 // Simple loop that compiler auto-vectorizes into efficient SIMD
25 for (int i = 0; i < 32; i++) {
26 r_out[i] = pixels[i].r;
27 g_out[i] = pixels[i].g;
28 b_out[i] = pixels[i].b;
29 }
30}
31
32// AVX2 function to compute luminance for 32 pixels
33void avx2_compute_luminance_32(const uint8_t *r_vals, const uint8_t *g_vals, const uint8_t *b_vals, uint8_t *luminance_out) {
34 // Load all 32 RGB values into AVX2 registers
35 __m256i r_all = _mm256_loadu_si256((const __m256i_u *)r_vals);
36 __m256i g_all = _mm256_loadu_si256((const __m256i_u *)g_vals);
37 __m256i b_all = _mm256_loadu_si256((const __m256i_u *)b_vals);
38
39 // Process low 16 pixels with accurate coefficients (16-bit math to prevent overflow)
40 __m256i r_lo = _mm256_unpacklo_epi8(r_all, _mm256_setzero_si256());
41 __m256i g_lo = _mm256_unpacklo_epi8(g_all, _mm256_setzero_si256());
42 __m256i b_lo = _mm256_unpacklo_epi8(b_all, _mm256_setzero_si256());
43
44 __m256i luma_16_lo = _mm256_mullo_epi16(r_lo, _mm256_set1_epi16(77));
45 luma_16_lo = _mm256_add_epi16(luma_16_lo, _mm256_mullo_epi16(g_lo, _mm256_set1_epi16(150)));
46 luma_16_lo = _mm256_add_epi16(luma_16_lo, _mm256_mullo_epi16(b_lo, _mm256_set1_epi16(29)));
47 luma_16_lo = _mm256_add_epi16(luma_16_lo, _mm256_set1_epi16(128));
48 luma_16_lo = _mm256_srli_epi16(luma_16_lo, 8);
49
50 // Process high 16 pixels with accurate coefficients
51 __m256i r_hi = _mm256_unpackhi_epi8(r_all, _mm256_setzero_si256());
52 __m256i g_hi = _mm256_unpackhi_epi8(g_all, _mm256_setzero_si256());
53 __m256i b_hi = _mm256_unpackhi_epi8(b_all, _mm256_setzero_si256());
54
55 __m256i luma_16_hi = _mm256_mullo_epi16(r_hi, _mm256_set1_epi16(77));
56 luma_16_hi = _mm256_add_epi16(luma_16_hi, _mm256_mullo_epi16(g_hi, _mm256_set1_epi16(150)));
57 luma_16_hi = _mm256_add_epi16(luma_16_hi, _mm256_mullo_epi16(b_hi, _mm256_set1_epi16(29)));
58 luma_16_hi = _mm256_add_epi16(luma_16_hi, _mm256_set1_epi16(128));
59 luma_16_hi = _mm256_srli_epi16(luma_16_hi, 8);
60
61 // Pack back to 8-bit
62 __m256i luma_packed = _mm256_packus_epi16(luma_16_lo, luma_16_hi);
63
64 // After unpack and pack operations, bytes are already in correct order [0-31]
65 // luma_16_lo contains pixels 0-7 (lower 128) and 16-23 (upper 128)
66 // luma_16_hi contains pixels 8-15 (lower 128) and 24-31 (upper 128)
67 // packus produces: [0-7, 8-15] in lower 128, [16-23, 24-31] in upper 128
68 // No permute needed - this is already the correct sequential order
69 _mm256_storeu_si256((__m256i_u *)luminance_out, luma_packed);
70}
71
72#endif // SIMD_SUPPORT_AVX2
unsigned char uint8_t
⚙️ Common definitions, error codes, macros, and types shared throughout the application
SIMD-optimized ASCII conversion interface.