diff --git a/src/core/codestream/ojph_codeblock_fun.cpp b/src/core/codestream/ojph_codeblock_fun.cpp index 4550498..2ddb392 100644 --- a/src/core/codestream/ojph_codeblock_fun.cpp +++ b/src/core/codestream/ojph_codeblock_fun.cpp @@ -205,6 +205,7 @@ namespace ojph { #ifndef OJPH_DISABLE_AVX2 if (get_cpu_ext_level() >= X86_CPU_EXT_LEVEL_AVX2) { + decode_cb32 = ojph_decode_codeblock_avx2; find_max_val32 = avx2_find_max_val32; if (reversible) { tx_to_cb32 = avx2_rev_tx_to_cb32; @@ -215,7 +216,8 @@ namespace ojph { tx_from_cb32 = avx2_irv_tx_from_cb32; } encode_cb32 = ojph_encode_codeblock_avx2; - decode_cb32 = ojph_decode_codeblock_avx2; + bool result = initialize_block_encoder_tables_avx2(); + assert(result); find_max_val64 = avx2_find_max_val64; if (reversible) { @@ -231,8 +233,11 @@ namespace ojph { #endif // !OJPH_DISABLE_AVX2 #if (defined(OJPH_ARCH_X86_64) && !defined(OJPH_DISABLE_AVX512)) - if (get_cpu_ext_level() >= X86_CPU_EXT_LEVEL_AVX512) + if (get_cpu_ext_level() >= X86_CPU_EXT_LEVEL_AVX512) { encode_cb32 = ojph_encode_codeblock_avx512; + bool result = initialize_block_encoder_tables_avx512(); + assert(result); + } #endif // !OJPH_DISABLE_AVX512 #elif defined(OJPH_ARCH_ARM) diff --git a/src/core/coding/ojph_block_encoder.h b/src/core/coding/ojph_block_encoder.h index d2782fb..72b3c0d 100644 --- a/src/core/coding/ojph_block_encoder.h +++ b/src/core/coding/ojph_block_encoder.h @@ -78,6 +78,9 @@ namespace ojph { ui32 stride, ui32* lengths, ojph::mem_elastic_allocator *elastic, ojph::coded_lists *& coded); + + bool initialize_block_encoder_tables_avx2(); + bool initialize_block_encoder_tables_avx512(); } } diff --git a/src/core/coding/ojph_block_encoder_avx2.cpp b/src/core/coding/ojph_block_encoder_avx2.cpp index 6f3db34..7624272 100644 --- a/src/core/coding/ojph_block_encoder_avx2.cpp +++ b/src/core/coding/ojph_block_encoder_avx2.cpp @@ -218,22 +218,19 @@ namespace ojph { } ///////////////////////////////////////////////////////////////////////// - bool initialize_tables_avx2() { - if (get_cpu_ext_level() >= X86_CPU_EXT_LEVEL_AVX2) { + static bool tables_initialized = false; + + ///////////////////////////////////////////////////////////////////////// + bool initialize_block_encoder_tables_avx2() { + if (!tables_initialized) { memset(vlc_tbl0, 0, 2048 * sizeof(ui32)); memset(vlc_tbl1, 0, 2048 * sizeof(ui32)); - - bool result; - result = vlc_init_tables(); - result = result && uvlc_init_tables(); - return result; + tables_initialized = vlc_init_tables(); + tables_initialized = tables_initialized && uvlc_init_tables(); } - return false; + return tables_initialized; } - ///////////////////////////////////////////////////////////////////////// - static bool tables_initialized = initialize_tables_avx2(); - ///////////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////////// diff --git a/src/core/coding/ojph_block_encoder_avx512.cpp b/src/core/coding/ojph_block_encoder_avx512.cpp index f0c7438..b35373a 100644 --- a/src/core/coding/ojph_block_encoder_avx512.cpp +++ b/src/core/coding/ojph_block_encoder_avx512.cpp @@ -218,23 +218,19 @@ namespace ojph { } ///////////////////////////////////////////////////////////////////////// - bool initialize_tables() { - if (get_cpu_ext_level() >= X86_CPU_EXT_LEVEL_AVX512) - { + static bool tables_initialized = false; + + ///////////////////////////////////////////////////////////////////////// + bool initialize_block_encoder_tables_avx512() { + if (!tables_initialized) { memset(vlc_tbl0, 0, 2048 * sizeof(ui32)); memset(vlc_tbl1, 0, 2048 * sizeof(ui32)); - - bool result; - result = vlc_init_tables(); - result = result && uvlc_init_tables(); - return result; + tables_initialized = vlc_init_tables(); + tables_initialized = tables_initialized && uvlc_init_tables(); } - return false; + return tables_initialized; } - ///////////////////////////////////////////////////////////////////////// - static bool tables_initialized = initialize_tables(); - ///////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////