Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC)
Options
Source code
use core::arch::x86_64::*; #[allow(non_camel_case_types)] type v128 = __m128i; pub struct Processed { raw_bytes: v128, high_nibbles: v128, carried_continuations: v128, } fn check_smaller_than_0xf4(current_bytes: v128, has_error: &mut v128) { *has_error = unsafe { _mm_or_si128( *has_error, _mm_subs_epu8(current_bytes, _mm_set1_epi8(0xf4u8 as _)), ) }; } fn continuation_lengths(high_nibbles: v128) -> v128 { unsafe { _mm_shuffle_epi8( _mm_setr_epi8( 1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) 0, 0, 0, 0, // 10xx (continuation) 2, 2, // 110x 3, // 1110 4, // 1111, next should be 0 (not checked here) ), high_nibbles, ) } } fn carry_continuations(initial_lengths: v128, previous_carries: v128) -> v128 { unsafe { let right1 = _mm_subs_epu8( _mm_alignr_epi8(initial_lengths, previous_carries, 16 - 1), _mm_set1_epi8(1), ); let sum = _mm_add_epi8(initial_lengths, right1); let right2 = _mm_subs_epu8( _mm_alignr_epi8(sum, previous_carries, 16 - 2), _mm_set1_epi8(2), ); _mm_add_epi8(sum, right2) } } fn check_continuations(initial_lengths: v128, carries: v128, has_error: &mut v128) { unsafe { // overlap || underlap // carry > length && length > 0 || !(carry > length) && !(length > 0) // (carries > length) == (lengths > 0) let over_under = _mm_cmpeq_epi8( _mm_cmpgt_epi8(carries, initial_lengths), _mm_cmpgt_epi8(initial_lengths, _mm_setzero_si128()), ); *has_error = _mm_or_si128(*has_error, over_under); } } fn check_first_continuation_max( current_bytes: v128, off1_current_bytes: v128, has_error: &mut v128, ) { unsafe { let mask_ed = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xEDu8 as _)); let mask_f4 = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xF4u8 as _)); let bad_follow_ed = _mm_and_si128( _mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x9Fu8 as _)), mask_ed, ); let bad_follow_f4 = _mm_and_si128( _mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x8Fu8 as _)), mask_f4, ); *has_error = _mm_or_si128(*has_error, _mm_or_si128(bad_follow_ed, bad_follow_f4)); } } fn check_overlong( current_bytes: v128, off1_current_bytes: v128, hibits: v128, previous_hibits: v128, has_error: &mut v128, ) { unsafe { let off1_hibits = _mm_alignr_epi8(hibits, previous_hibits, 16 - 1); let initial_mins = _mm_shuffle_epi8( _mm_setr_epi8( -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, // 10xx => false 0xC2u8 as _, -128, // 110x 0xE1u8 as _, // 1110 0xF1u8 as _, ), off1_hibits, ); let initial_under = _mm_cmpgt_epi8(initial_mins, off1_current_bytes); let second_mins = _mm_shuffle_epi8( _mm_setr_epi8( -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, // 10xx => false 127, 127, // 110x => true 0xA0u8 as _, // 1110 0x90u8 as _, ), off1_hibits, ); let second_under = _mm_cmpgt_epi8(second_mins, current_bytes); *has_error = _mm_or_si128(*has_error, _mm_and_si128(initial_under, second_under)); } } fn count_nibbles(bytes: v128, answer: &mut Processed) { answer.raw_bytes = bytes; answer.high_nibbles = unsafe { _mm_and_si128(_mm_srli_epi16(bytes, 4), _mm_set1_epi8(0x0F)) }; } fn check_utf8_bytes(current_bytes: v128, previous: &Processed, has_error: &mut v128) -> Processed { let mut pb = unsafe { Processed { raw_bytes: _mm_setzero_si128(), high_nibbles: _mm_setzero_si128(), carried_continuations: _mm_setzero_si128(), } }; count_nibbles(current_bytes, &mut pb); check_smaller_than_0xf4(current_bytes, has_error); let initial_lengths = continuation_lengths(pb.high_nibbles); pb.carried_continuations = carry_continuations(initial_lengths, previous.carried_continuations); check_continuations(initial_lengths, pb.carried_continuations, has_error); let off1_current_bytes = unsafe { _mm_alignr_epi8(pb.raw_bytes, previous.raw_bytes, 16 - 1) }; check_first_continuation_max(current_bytes, off1_current_bytes, has_error); check_overlong( current_bytes, off1_current_bytes, pb.high_nibbles, previous.high_nibbles, has_error, ); pb } pub fn validate(src: &[u8]) -> bool { unsafe { let len = src.len(); let src = src.as_ptr(); let mut i = 0; let mut has_error = _mm_setzero_si128(); let mut previous = Processed { raw_bytes: _mm_setzero_si128(), high_nibbles: _mm_setzero_si128(), carried_continuations: _mm_setzero_si128(), }; if len >= 16 { while i <= len - 16 { let current_bytes = _mm_loadu_si128(src.add(i).cast()); previous = check_utf8_bytes(current_bytes, &previous, &mut has_error); i += 16; } } // last part if i < len { let mut buf = [0u8; 16]; core::ptr::copy_nonoverlapping(src.add(i), buf.as_mut_ptr(), len - i); let current_bytes = _mm_loadu_si128(buf.as_ptr().cast()); check_utf8_bytes(current_bytes, &previous, &mut has_error); } else { has_error = _mm_or_si128( _mm_cmpgt_epi8( previous.carried_continuations, _mm_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1), ), has_error, ); } _mm_testz_si128(has_error, has_error) != 0 } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree