1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2020 Shin Yoshida
//
// "LGPL-3.0-or-later OR Apache-2.0 OR BSD-2-Clause OR MIT"
//
// This is part of cpu-endian
//
//  cpu-endian is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  cpu-endian is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public License
//  along with cpu-endian.  If not, see <http://www.gnu.org/licenses/>.
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// Redistribution and use in source and binary forms, with or without modification, are permitted
// provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this
//    list of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice (including the next paragraph) shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#![deny(missing_docs)]

//! # 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),
//!     _ => {},
//! }
//! ```

use core::cell::Cell;

/// Byte order of scalar types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Endian {
    /// little-endian
    Little,
    /// big-endian
    Big,
    /// Neither little-endian nor big-endian. For example, PDP-endian, mixed-endian, middle-endian, and so on.
    /// (Such endian is very rare today.)
    Minor,
}

thread_local!(
    /// Cache of native_().
    ///
    /// Some CPUs change the byte order, however I don't think none of them changes it after process started.
    static CACHE: Cell<u8> = Cell::new(0)
);

/// Returns the CPU byte order.
///
/// ```
/// use cpu_endian::*;
///
/// // 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),
///     _ => {},
/// }
/// ```
#[inline]
pub fn working() -> Endian {
    inner_working()
}

/// Implementation for `working` .
///
/// This function is only for `x86` and `x86_64` architecture, and const function to return
/// `Endian::Little` . ( `x86` and `x86_64` are typical little-endian CPU.)
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
const fn inner_working() -> Endian {
    Endian::Little
}

/// Implementation for `working` .
///
/// This function is for CPUs but `x86` nor `x86_64` and calls C function to detect the endian.
/// (Some CPUs can switch the endian, so it is difficult to hard cord.)
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
#[inline]
fn inner_working() -> Endian {
    // No cache is hit.
    // Because native()_ always returns the same value, Ordering::Relaxed will do.
    if CACHE.with(|c| c.get()) == 0 {
        let order = unsafe { native_() };
        debug_assert_ne!(0, order);

        CACHE.with(|c| c.set(order as u8));
    }

    match CACHE.with(|c| c.get()) {
        1 => Endian::Little,
        2 => Endian::Big,
        _ => Endian::Minor,
    }
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
use std::os::raw::c_int;

#[link(name = "native_endian_")]
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
extern "C" {

    /// Returns the cpu native endian.
    ///
    /// - Little endian: 1
    /// - Big endian: 2
    /// - Other: 3
    fn native_() -> c_int;
}