rust/library/core/src
Guillaume Gomez 62b834fb9f
Rollup merge of #84751 - Soveu:is_char_boundary_opt, r=Amanieu
str::is_char_boundary - slight optimization

Current `str::is_char_boundary` implementation emits slightly more instructions, because it includes an additional branch for `index == s.len()`
```rust
pub fn is_char_boundary(s: &str, index: usize) -> bool {
    if index == 0 || index == s.len() {
        return true;
    }
    match s.as_bytes().get(index) {
        None => false,
        Some(&b) => (b as i8) >= -0x40,
    }
}
```
Just changing the place of `index == s.len()` merges it with `index < s.len()` from `s.as_bytes().get(index)`
```rust
pub fn is_char_boundary2(s: &str, index: usize) -> bool {
    if index == 0 {
        return true;
    }

    match s.as_bytes().get(index) {
        // For some reason, LLVM likes this comparison here more
        None => index == s.len(),
        // This is bit magic equivalent to: b < 128 || b >= 192
        Some(&b) => (b as i8) >= -0x40,
    }
}
```
This one has better codegen on every platform, except powerpc
<details><summary>x86 codegen</summary>
<p>

```nasm
example::is_char_boundary:
        mov     al, 1
        test    rdx, rdx
        je      .LBB0_5
        cmp     rsi, rdx
        je      .LBB0_5
        cmp     rsi, rdx
        jbe     .LBB0_3
        cmp     byte ptr [rdi + rdx], -65
        setg    al
.LBB0_5:
        ret
.LBB0_3:
        xor     eax, eax
        ret

example::is_char_boundary2:
        test    rdx, rdx
        je      .LBB1_1
        cmp     rsi, rdx
        jbe     .LBB1_4
        cmp     byte ptr [rdi + rdx], -65
        setg    al
        ret
.LBB1_1:  ; technically this branch is the same as LBB1_4
        mov     al, 1
        ret
.LBB1_4:
        sete    al
        ret
 ```
</p>
</details>

<details><summary>aarch64 codegen</summary>
<p>

```as
example::is_char_boundary:
        mov     x8, x0
        mov     w0, #1
        cbz     x2, .LBB0_4
        cmp     x1, x2
        b.eq    .LBB0_4
        b.ls    .LBB0_5
        ldrsb   w8, [x8, x2]
        cmn     w8, #65
        cset    w0, gt
.LBB0_4:
        ret
.LBB0_5:
        mov     w0, wzr
        ret

example::is_char_boundary2:
        cbz     x2, .LBB1_3
        cmp     x1, x2
        b.ls    .LBB1_4
        ldrsb   w8, [x0, x2]
        cmn     w8, #65
        cset    w0, gt
        ret
.LBB1_3:
        mov     w0, #1
        ret
.LBB1_4:
        cset    w0, eq
        ret
```

</p>
</details>

<details><summary>riscv64gc codegen</summary>
<p>

example::is_char_boundary:
        seqz    a3, a2
        xor     a4, a1, a2
        seqz    a4, a4
        or      a4, a4, a3
        addi    a3, zero, 1
        bnez    a4, .LBB0_3
        bgeu    a2, a1, .LBB0_4
        add     a0, a0, a2
        lb      a0, 0(a0)
        addi    a1, zero, -65
        slt     a3, a1, a0
.LBB0_3:
        mv      a0, a3
        ret
.LBB0_4:
        mv      a0, zero
        ret

example::is_char_boundary2:
        beqz    a2, .LBB1_3
        bgeu    a2, a1, .LBB1_4
        add     a0, a0, a2
        lb      a0, 0(a0)
        addi    a1, zero, -65
        slt     a0, a1, a0
        ret
.LBB1_3:
        addi    a0, zero, 1
        ret
.LBB1_4:
        xor     a0, a1, a2
        seqz    a0, a0
        ret

</p>
</details>

[Link to godbolt](https://godbolt.org/z/K8avEz8Gr)

`@rustbot` label: A-codegen
2021-05-15 17:56:47 +02:00
..
alloc Fix const stability since versions. 2021-03-15 14:39:18 +00:00
array Auto merge of #84147 - cuviper:array-method-dispatch, r=nikomatsakis,m-ou-se 2021-04-25 07:26:49 +00:00
char Update char::escape_debug_ext to handle different escapes in strings vs. chars 2021-03-26 11:23:51 +03:00
convert Get rid of "[+] show undocumented items" toggle on numeric From impls 2021-04-22 11:51:05 -07:00
fmt Rollup merge of #84390 - m-ou-se:make-debug-non-exhaustive-without-fields-a-little-bit-less-verbose, r=kennytm 2021-04-21 23:06:21 +02:00
future Rename #[doc(spotlight)] to #[doc(notable_trait)] 2021-03-15 13:59:54 -07:00
hash Auto merge of #83390 - clarfonthey:hasher_docs, r=Amanieu 2021-04-26 08:21:55 +00:00
iter Fix Step feature flag, make tidy lint more useful to find things like this 2021-05-09 17:15:54 -04:00
macros Add support for const operands and options to global_asm! 2021-05-13 22:31:57 +01:00
mem Fix typo in MaybeUninit::array_assume_init safety comment 2021-05-05 12:31:38 +03:00
num add BITS associated constant to core::num::Wrapping 2021-05-11 13:36:43 +02:00
ops Auto merge of #84092 - scottmcm:try_trait_initial, r=yaahc,m-ou-se 2021-04-26 23:17:31 +00:00
prelude Bump cfgs 2021-04-04 14:57:05 -04:00
ptr #[inline(always)] on basic pointer methods 2021-05-12 10:10:28 +01:00
slice Clarify documentation for [T]::contains. Fixes #84877. 2021-05-03 12:01:16 -07:00
str str::is_char_boundary - few comments 2021-04-30 20:51:30 +02:00
stream Remove Stream::next 2021-01-23 16:54:56 +01:00
sync Stabilize atomic_fetch_update methods on AtomicBool and AtomicPtr. 2021-04-11 11:45:46 +02:00
task Add the try_trait_v2 library basics 2021-04-17 11:58:18 -07:00
unicode Add a check for ASCII characters in to_upper and to_lower 2021-02-26 11:39:36 -06:00
any.rs Change the Debug impl of Any and UnsafeCell to use finish_non_exhaustive 2021-04-21 14:51:04 +02:00
ascii.rs Replace all fmt.pad with debug_struct 2021-04-21 14:38:24 +02:00
bool.rs Stabilise then 2020-11-22 13:45:14 +00:00
borrow.rs Fix borrow and deref 2021-03-03 11:23:29 +01:00
cell.rs Use #[inline(always)] on trivial UnsafeCell methods 2021-04-04 11:55:13 -07:00
clone.rs Fix core tests 2021-03-03 11:22:49 +01:00
cmp.rs Disallows #![feature(no_coverage)] on stable and beta 2021-05-05 07:52:26 -07:00
default.rs Add diagnostic item to Default trait 2021-03-04 10:14:48 -08:00
ffi.rs Replace all fmt.pad with debug_struct 2021-04-21 14:38:24 +02:00
hint.rs Auto merge of #84725 - sebpop:arm64-isb, r=joshtriplett 2021-05-02 04:54:31 +00:00
internal_macros.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
intrinsics.rs Rollup merge of #84755 - jyn514:core-links, r=kennytm 2021-05-07 00:38:38 +02:00
lazy.rs Capitalize safety comments 2020-09-08 22:26:44 -04:00
lib.rs Rollup merge of #85096 - clarfonthey:const_unchecked, r=oli-obk 2021-05-10 16:15:02 +02:00
marker.rs Add additional migrations to handle auto-traits and clone traits 2021-05-06 14:17:59 -04:00
option.rs Reorder the parameter descriptions of map_or and map_or_else 2021-04-27 18:00:30 +08:00
panic.rs Implement new panic!() behaviour for Rust 2021. 2021-01-25 13:48:11 +01:00
panicking.rs Fix panic message of assert_failed_inner 2021-03-13 18:50:43 +08:00
pin.rs Fix overlength lines in core::pin. 2021-01-05 20:14:02 +01:00
primitive.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
raw.rs Deprecate the core::raw / std::raw module 2021-04-15 02:32:33 +02:00
result.rs Reorder the parameter descriptions of map_or and map_or_else 2021-04-27 18:00:30 +08:00
time.rs use else if in std library 2021-05-03 07:05:08 -04:00
tuple.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
unit.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00