Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SSE2 in the x86_64 C version of decodeLatin1 #297

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/haskell/Benchmarks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ main = do
, env (DecodeUtf8.initEnv (tf "ascii.txt")) (DecodeUtf8.benchmark "ascii")
, env (DecodeUtf8.initEnv (tf "russian.txt")) (DecodeUtf8.benchmark "russian")
, env (DecodeUtf8.initEnv (tf "japanese.txt")) (DecodeUtf8.benchmark "japanese")
, env (DecodeUtf8.initEnv (tf "ascii.txt")) (DecodeUtf8.benchmarkASCII)
, EncodeUtf8.benchmark "επανάληψη 竺法蘭共譯"
, env (Equality.initEnv (tf "japanese.txt")) Equality.benchmark
, FileRead.benchmark (tf "russian.txt")
Expand Down
12 changes: 12 additions & 0 deletions benchmarks/haskell/Benchmarks/DecodeUtf8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
module Benchmarks.DecodeUtf8
( initEnv
, benchmark
, benchmarkASCII
) where

import Foreign.C.Types
Expand Down Expand Up @@ -67,6 +68,17 @@ benchmark kind ~(bs, lbs) =
, bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
]

benchmarkASCII :: Env -> Benchmark
benchmarkASCII ~(bs, lbs) =
bgroup "DecodeASCII"
[ C.bench "strict decodeUtf8" $ nf T.decodeUtf8 bs
, C.bench "strict decodeLatin1" $ nf T.decodeLatin1 bs
, C.bench "strict decodeASCII" $ nf T.decodeASCII bs
, C.bench "lazy decodeUtf8" $ nf TL.decodeUtf8 lbs
, C.bench "lazy decodeLatin1" $ nf TL.decodeLatin1 lbs
, C.bench "lazy decodeASCII" $ nf TL.decodeASCII lbs
]

iconv :: B.ByteString -> IO CInt
iconv (PS fp off len) = withForeignPtr fp $ \ptr ->
time_iconv (ptr `plusPtr` off) (fromIntegral len)
Expand Down
23 changes: 23 additions & 0 deletions cbits/cbits.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#include <stdio.h>
#include "text_cbits.h"

#if defined(__x86_64__)
#include <xmmintrin.h>
#include <emmintrin.h>
#endif

void _hs_text_memcpy(void *dest, size_t doff, const void *src, size_t soff,
size_t n)
{
Expand Down Expand Up @@ -82,6 +87,23 @@ _hs_text_decode_latin1(uint16_t *dest, const uint8_t *src,
while (p != srcend && (uintptr_t)p & 0x3)
*dest++ = *p++;

#if defined(__x86_64__)
/* All the intrinsics used here are from SSE2,
* so every x86_64 CPU supports them.
*/
const __m128i zeros = _mm_set1_epi32(0);
while (p < srcend - 3) {
/* Load 4 bytes of ASCII data */
const __m128i ascii = _mm_cvtsi32_si128(*((const uint32_t *)p));
/* Interleave with zeros */
const __m128i utf16 = _mm_unpacklo_epi8(ascii, zeros);
/* Store the resulting 8 bytes into destination */
_mm_storel_epi64((__m128i *)dest, utf16);

dest += 4;
p += 4;
}
#else
/* iterate over 32-bit aligned loads */
while (p < srcend - 3) {
const uint32_t w = *((const uint32_t *)p);
Expand All @@ -93,6 +115,7 @@ _hs_text_decode_latin1(uint16_t *dest, const uint8_t *src,

p += 4;
}
#endif
#endif

/* handle unaligned suffix */
Expand Down