Skip to content

Commit eb2e835

Browse files
authored
feat: remove portability.rs and use stdlib idioms for maximal rustiness (#38)
* feat: remove portability.rs and use stdlib idioms for maximal rustiness * fix: remove extraneous casts/converts
1 parent 72aaee3 commit eb2e835

File tree

7 files changed

+26
-65
lines changed

7 files changed

+26
-65
lines changed

src/avx2/deser.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ pub use crate::Result;
1111
pub use crate::avx2::utf8check::*;
1212
pub use crate::stringparse::*;
1313

14-
use crate::portability::trailingzeroes;
15-
16-
1714
impl<'de> Deserializer<'de> {
1815
#[cfg_attr(not(feature = "no-inline"), inline(always))]
1916
pub fn parse_str_(&mut self) -> Result<&'de str> {
@@ -60,7 +57,7 @@ impl<'de> Deserializer<'de> {
6057
if (bs_bits.wrapping_sub(1) & quote_bits) != 0 {
6158
// we encountered quotes first. Move dst to point to quotes and exit
6259
// find out where the quote is...
63-
let quote_dist: u32 = trailingzeroes(u64::from(quote_bits)) as u32;
60+
let quote_dist: u32 = quote_bits.trailing_zeros();
6461

6562
///////////////////////
6663
// Above, check for overflow in case someone has a crazy string (>=4GB?)
@@ -81,7 +78,7 @@ impl<'de> Deserializer<'de> {
8178
}
8279
if (quote_bits.wrapping_sub(1) & bs_bits) != 0 {
8380
// Move to the 'bad' character
84-
let bs_dist: u32 = trailingzeroes(u64::from(bs_bits));
81+
let bs_dist: u32 = bs_bits.trailing_zeros();
8582
len += bs_dist as usize;
8683
src_i += bs_dist as usize;
8784
break;
@@ -132,7 +129,7 @@ impl<'de> Deserializer<'de> {
132129
if (bs_bits.wrapping_sub(1) & quote_bits) != 0 {
133130
// we encountered quotes first. Move dst to point to quotes and exit
134131
// find out where the quote is...
135-
let quote_dist: u32 = trailingzeroes(u64::from(quote_bits)) as u32;
132+
let quote_dist: u32 = quote_bits.trailing_zeros();
136133

137134
///////////////////////
138135
// Above, check for overflow in case someone has a crazy string (>=4GB?)
@@ -158,7 +155,7 @@ impl<'de> Deserializer<'de> {
158155
}
159156
if (quote_bits.wrapping_sub(1) & bs_bits) != 0 {
160157
// find out where the backspace is
161-
let bs_dist: u32 = trailingzeroes(u64::from(bs_bits));
158+
let bs_dist: u32 = bs_bits.trailing_zeros();
162159
let escape_char: u8 = unsafe { *src.get_unchecked(src_i + bs_dist as usize + 1) };
163160
// we encountered backslash first. Handle backslash
164161
if escape_char == b'u' {

src/avx2/stage1.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![allow(dead_code)]
2-
use crate::portability::*;
32
use crate::avx2::utf8check::*;
43
use crate::*;
54
#[cfg(target_arch = "x86")]
@@ -103,11 +102,10 @@ fn find_odd_backslash_sequences(input: &SimdInput, prev_iter_ends_odd_backslash:
103102
let odd_starts: u64 = start_edges & !even_start_mask;
104103
let even_carries: u64 = bs_bits.wrapping_add(even_starts);
105104

106-
let mut odd_carries: u64 = 0;
107105
// must record the carry-out of our odd-carries out of bit 63; this
108106
// indicates whether the sense of any edge going to the next iteration
109107
// should be flipped
110-
let iter_ends_odd_backslash: bool = add_overflow(bs_bits, odd_starts, &mut odd_carries);
108+
let (mut odd_carries, iter_ends_odd_backslash) = bs_bits.overflowing_add(odd_starts);
111109

112110
odd_carries |= *prev_iter_ends_odd_backslash; // push in bit zero as a potential end
113111
// if we had an odd-numbered run at the
@@ -251,7 +249,7 @@ unsafe fn find_whitespace_and_structurals(
251249
//TODO: usize was u32 here does this matter?
252250
#[cfg_attr(not(feature = "no-inline"), inline(always))]
253251
fn flatten_bits(base: &mut Vec<u32>, idx: u32, mut bits: u64) {
254-
let cnt: usize = hamming(bits) as usize;
252+
let cnt: usize = bits.count_ones() as usize;
255253
let mut l = base.len();
256254
let idx_minus_64 = idx.wrapping_sub(64);
257255
let idx_64_v = unsafe {
@@ -279,21 +277,21 @@ fn flatten_bits(base: &mut Vec<u32>, idx: u32, mut bits: u64) {
279277

280278
while bits != 0 {
281279
unsafe {
282-
let v0 = static_cast_i32!(trailingzeroes(bits));
280+
let v0 = bits.trailing_zeros() as i32;
283281
bits &= bits.wrapping_sub(1);
284-
let v1 = static_cast_i32!(trailingzeroes(bits));
282+
let v1 = bits.trailing_zeros() as i32;
285283
bits &= bits.wrapping_sub(1);
286-
let v2 = static_cast_i32!(trailingzeroes(bits));
284+
let v2 = bits.trailing_zeros() as i32;
287285
bits &= bits.wrapping_sub(1);
288-
let v3 = static_cast_i32!(trailingzeroes(bits));
286+
let v3 = bits.trailing_zeros() as i32;
289287
bits &= bits.wrapping_sub(1);
290-
let v4 = static_cast_i32!(trailingzeroes(bits));
288+
let v4 = bits.trailing_zeros() as i32;
291289
bits &= bits.wrapping_sub(1);
292-
let v5 = static_cast_i32!(trailingzeroes(bits));
290+
let v5 = bits.trailing_zeros() as i32;
293291
bits &= bits.wrapping_sub(1);
294-
let v6 = static_cast_i32!(trailingzeroes(bits));
292+
let v6 = bits.trailing_zeros() as i32;
295293
bits &= bits.wrapping_sub(1);
296-
let v7 = static_cast_i32!(trailingzeroes(bits));
294+
let v7 = bits.trailing_zeros() as i32;
297295
bits &= bits.wrapping_sub(1);
298296

299297
let v: __m256i = _mm256_set_epi32(v7, v6, v5, v4, v3, v2, v1, v0);

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ mod macros;
8080
mod error;
8181
mod numberparse;
8282
mod parsedjson;
83-
mod portability;
8483
mod stringparse;
8584

8685
#[cfg(target_feature = "avx2")]

src/portability.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/sse42/deser.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ pub use crate::Result;
1111
pub use crate::sse42::utf8check::*;
1212
pub use crate::stringparse::*;
1313

14-
use crate::portability::trailingzeroes;
15-
1614

1715
impl<'de> Deserializer<'de> {
1816
#[cfg_attr(not(feature = "no-inline"), inline(always))]
@@ -60,7 +58,7 @@ impl<'de> Deserializer<'de> {
6058
if (bs_bits.wrapping_sub(1) & quote_bits) != 0 {
6159
// we encountered quotes first. Move dst to point to quotes and exit
6260
// find out where the quote is...
63-
let quote_dist: u32 = trailingzeroes(u64::from(quote_bits)) as u32;
61+
let quote_dist: u32 = quote_bits.trailing_zeros();
6462

6563
///////////////////////
6664
// Above, check for overflow in case someone has a crazy string (>=4GB?)
@@ -81,7 +79,7 @@ impl<'de> Deserializer<'de> {
8179
}
8280
if (quote_bits.wrapping_sub(1) & bs_bits) != 0 {
8381
// Move to the 'bad' character
84-
let bs_dist: u32 = trailingzeroes(u64::from(bs_bits));
82+
let bs_dist: u32 = bs_bits.trailing_zeros();
8583
len += bs_dist as usize;
8684
src_i += bs_dist as usize;
8785
break;
@@ -132,7 +130,7 @@ impl<'de> Deserializer<'de> {
132130
if (bs_bits.wrapping_sub(1) & quote_bits) != 0 {
133131
// we encountered quotes first. Move dst to point to quotes and exit
134132
// find out where the quote is...
135-
let quote_dist: u32 = trailingzeroes(u64::from(quote_bits)) as u32;
133+
let quote_dist: u32 = quote_bits.trailing_zeros();
136134

137135
///////////////////////
138136
// Above, check for overflow in case someone has a crazy string (>=4GB?)
@@ -158,7 +156,7 @@ impl<'de> Deserializer<'de> {
158156
}
159157
if (quote_bits.wrapping_sub(1) & bs_bits) != 0 {
160158
// find out where the backspace is
161-
let bs_dist: u32 = trailingzeroes(u64::from(bs_bits));
159+
let bs_dist: u32 = bs_bits.trailing_zeros();
162160
let escape_char: u8 = unsafe { *src.get_unchecked(src_i + bs_dist as usize + 1) };
163161
// we encountered backslash first. Handle backslash
164162
if escape_char == b'u' {

src/sse42/stage1.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![allow(dead_code)]
2-
use crate::portability::*;
32
use crate::sse42::utf8check::*;
43
use crate::*;
54
#[cfg(target_arch = "x86")]
@@ -130,11 +129,10 @@ fn find_odd_backslash_sequences(input: &SimdInput, prev_iter_ends_odd_backslash:
130129
let odd_starts: u64 = start_edges & !even_start_mask;
131130
let even_carries: u64 = bs_bits.wrapping_add(even_starts);
132131

133-
let mut odd_carries: u64 = 0;
134132
// must record the carry-out of our odd-carries out of bit 63; this
135133
// indicates whether the sense of any edge going to the next iteration
136134
// should be flipped
137-
let iter_ends_odd_backslash: bool = add_overflow(bs_bits, odd_starts, &mut odd_carries);
135+
let (mut odd_carries, iter_ends_odd_backslash) = bs_bits.overflowing_add(odd_starts);
138136

139137
odd_carries |= *prev_iter_ends_odd_backslash; // push in bit zero as a potential end
140138
// if we had an odd-numbered run at the
@@ -312,7 +310,7 @@ unsafe fn find_whitespace_and_structurals(
312310
//TODO: usize was u32 here does this matter?
313311
#[cfg_attr(not(feature = "no-inline"), inline(always))]
314312
fn flatten_bits(base: &mut Vec<u32>, idx: u32, mut bits: u64) {
315-
let cnt: usize = hamming(bits) as usize;
313+
let cnt: usize = bits.count_ones() as usize;
316314
let mut l = base.len();
317315
let idx_minus_64 = idx.wrapping_sub(64);
318316
let idx_64_v = unsafe {
@@ -336,13 +334,13 @@ fn flatten_bits(base: &mut Vec<u32>, idx: u32, mut bits: u64) {
336334

337335
while bits != 0 {
338336
unsafe {
339-
let v0 = static_cast_i32!(trailingzeroes(bits));
337+
let v0 = bits.trailing_zeros() as i32;
340338
bits &= bits.wrapping_sub(1);
341-
let v1 = static_cast_i32!(trailingzeroes(bits));
339+
let v1 = bits.trailing_zeros() as i32;
342340
bits &= bits.wrapping_sub(1);
343-
let v2 = static_cast_i32!(trailingzeroes(bits));
341+
let v2 = bits.trailing_zeros() as i32;
344342
bits &= bits.wrapping_sub(1);
345-
let v3 = static_cast_i32!(trailingzeroes(bits));
343+
let v3 = bits.trailing_zeros() as i32;
346344
bits &= bits.wrapping_sub(1);
347345

348346
let v: __m128i = _mm_set_epi32(v3, v2, v1, v0);

src/value/generator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//
55
// https://github.com/maciejhirsz/json-rust/blob/master/src/codegen.rs
66

7-
use crate::portability::trailingzeroes;
87
use crate::value::ValueTrait;
98
#[cfg(target_arch = "x86")]
109
use std::arch::x86::*;
@@ -130,7 +129,7 @@ pub trait BaseGenerator {
130129
let in_range = _mm256_cmpeq_epi8(is_unchanged, zero);
131130
let quote_bits = _mm256_movemask_epi8(_mm256_or_si256(bs_or_quote, in_range));
132131
if quote_bits != 0 {
133-
let quote_dist = trailingzeroes(quote_bits as u64) as usize;
132+
let quote_dist = quote_bits.trailing_zeros() as usize;
134133
stry!(self.get_writer().write_all(&string[0..idx + quote_dist]));
135134
let ch = string[idx + quote_dist];
136135
match ESCAPED[ch as usize] {
@@ -169,7 +168,7 @@ pub trait BaseGenerator {
169168
let in_range = _mm_cmpeq_epi8(is_unchanged, zero);
170169
let quote_bits = _mm_movemask_epi8(_mm_or_si128(bs_or_quote, in_range));
171170
if quote_bits != 0 {
172-
let quote_dist = trailingzeroes(quote_bits as u64) as usize;
171+
let quote_dist = quote_bits.trailing_zeros() as usize;
173172
stry!(self.get_writer().write_all(&string[0..idx + quote_dist]));
174173
let ch = string[idx + quote_dist];
175174
match ESCAPED[ch as usize] {

0 commit comments

Comments
 (0)