urlyy commented on PR #1778:
URL: https://github.com/apache/fury/pull/1778#issuecomment-2257487497

   > This is too huge, do we have a better way to implement it @theweipeng 
@kitty-eu-org
   
   I also think about it. Actually I have seen the CPP version implementation: 
https://github.com/apache/fury/pull/1732/files , but the SIMD API seems only 
used at swap endian and `checking` whether all utf16 of a chunk are all can be 
converted to 1 bytes utf8, and then just use `loop` to convert the utf16 bytes 
in a chunk one by one. I don't think it has a good use of SIMD.
   ```
   // cpp version
   if (_mm256_testz_si256(mask1, mask1)) {
         // All values < 0x80, 1 byte per character
         for (int j = 0; j < 16; ++j) {
           *output++ = static_cast<char>(utf16[i + j]);
         }
       }
   ```
   rust version
   ```
    // check chunk's all u16 less than 0x80
   if _mm256_testz_si256(chunk, *M_FF80) != 0 {
       let utf8_packed = _mm_packus_epi16(
           _mm256_castsi256_si128(chunk),
           _mm256_extractf128_si256(chunk, 1),
       );
       _mm_storeu_si128(ptr_8.add(offset_8) as *mut __m128i, utf8_packed);
       offset_8 += CHUNK_UTF16_USAGE;
       offset_16 += CHUNK_UTF16_USAGE;
       continue;
   }
   ```
   But it is easy to shorten the code . Just remove some functions. And I just 
write a short-version like below. Benchmark data is also as follows.
   ```rust
    while offset_16 + CHUNK_UTF16_USAGE <= len_16 {
     let mut chunk = _mm256_loadu_si256(ptr_16.add(offset_16) as *const 
__m256i);
     chunk = if is_little_endian == super::super::IS_LITTLE_ENDIAN_LOCAL {
         chunk
     } else {
         _mm256_shuffle_epi8(chunk, *ENDIAN_SWAP_MASK)
     };
     let mask1 = _mm256_cmpgt_epi16(chunk, *limit1);
     // check chunk's all u16 less than 0x80 ,1 utf16 -> 1utf8
     if _mm256_testz_si256(mask1, mask1) != 0 {
         let utf8_packed = _mm_packus_epi16(
             _mm256_castsi256_si128(chunk),
             _mm256_extractf128_si256(chunk, 1),
         );
         _mm_storeu_si128(ptr_8.add(offset_8) as *mut __m128i, utf8_packed);
         offset_8 += CHUNK_UTF16_USAGE;
         offset_16 += CHUNK_UTF16_USAGE;
         continue;
     }
     // when has some utf16 can convert to 2/3/4 utf8 bytes 
     let res = call_fallback(
         ptr_16,
         ptr_8,
         &mut offset_16,
         &mut offset_8,
         len_16,
         is_little_endian,
     );
     if let Some(err_msg) = res {
         return Err(err_msg);
     }
   }
   ```
   
![image](https://github.com/user-attachments/assets/afbb92a8-2c05-47fd-a2e7-5103f81b3390)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to