#endianness #little-endian #big-endian

cpu-endian

cpu-endian is a portable crate to detect CPU byte order. It detects how CPU native scalar type is ordered; little-endian or big-endian, or something else (like PDP-endian, mixed-endian, middle-endian, and so on.)

2 releases

0.1.1 Dec 10, 2020
0.1.0 Dec 10, 2020

#442 in Operating systems

Download history 191/week @ 2023-12-18 87/week @ 2024-01-01 42/week @ 2024-01-08 23/week @ 2024-01-15 238/week @ 2024-01-22 4/week @ 2024-02-19 6/week @ 2024-02-26 1/week @ 2024-03-11 9/week @ 2024-03-25 43/week @ 2024-04-01

53 downloads per month

LGPL-3.0-or-later OR Apache-2…

775KB
62 lines

cpu-endian

cpu-endian is a portable crate to detect CPU byte order.

It detects how CPU native scalar type is ordered; little-endian or big-endian, or something else (like PDP-endian, mixed-endian, middle-endian, and so on.)

Examples

use cpu_endian::{Endian, working};

// Takes first octet of 0x00ff: u16.
let v: u16 = 0x00ff;
let first_octet: u8 = unsafe {
    let ptr = &v as *const u16;
    let ptr = ptr as *const u8;
    *ptr
};

// If the byte-order is little-endian, the first octet should be 0xff, or if big-endian,
// it should be 0x00.
match working() {
    Endian::Little => assert_eq!(0xff, first_octet),
    Endian::Big => assert_eq!(0x00, first_octet),
    _ => {},
}

Requirements

If the CPU is neither x86 nor x86_64 , C++ compiler with feature c++20 is required.


lib.rs:

cpu-endian

cpu-endian is a portable crate to detect CPU byte order.

It detects how CPU native scalar type is ordered; little-endian or big-endian, or something else (like PDP-endian, mixed-endian, middle-endian, and so on.)

Examples

use cpu_endian::{Endian, working};

// Takes first octet of 0x00ff: u16.
let v: u16 = 0x00ff;
let first_octet: u8 = unsafe {
    let ptr = &v as *const u16;
    let ptr = ptr as *const u8;
    *ptr
};

// If the byte-order is little-endian, the first octet should be 0xff, or if big-endian,
// it should be 0x00.
match working() {
    Endian::Little => assert_eq!(0xff, first_octet),
    Endian::Big => assert_eq!(0x00, first_octet),
    _ => {},
}

No runtime deps