]> git.saurik.com Git - apple/xnu.git/blobdiff - libkern/libkern/i386/OSByteOrder.h
xnu-792.21.3.tar.gz
[apple/xnu.git] / libkern / libkern / i386 / OSByteOrder.h
index e650d5c1fb424df0b04c5556d957cac29abe878b..5dde65e05fc92f2705bfbbab090e929db7ad5005 100644 (file)
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
- */
-/*
- * Copyright (c) 1999 Apple Computer, Inc.  All rights reserved. 
- *
- * HISTORY
- *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
 #ifndef _OS_OSBYTEORDERI386_H
 #define _OS_OSBYTEORDERI386_H
 
-#include <libkern/OSTypes.h>
-
-/* Functions for byte reversed loads. */
+#include <stdint.h>
 
-OS_INLINE
-UInt16
-OSReadSwapInt16(
-    volatile void               * base,
-    UInt                          offset
-)
-{
-    UInt16 result;
+#if !defined(OS_INLINE)
+#        define OS_INLINE static inline
+#endif
 
-    result = *(volatile UInt16 *)((UInt8 *)base + offset);
-    __asm__ volatile("xchgb %b0,%h0"
-                     : "=q" (result)
-                     : "0"  (result));
-    return result;
-}
+/* Generic byte swapping functions. */
 
 OS_INLINE
-UInt32
-OSReadSwapInt32(
-    volatile void               * base,
-    UInt                          offset
+uint16_t
+_OSSwapInt16(
+    uint16_t        data
 )
 {
-    UInt32 result;
-
-    result = *(volatile UInt32 *)((UInt8 *)base + offset);
-    __asm__ volatile("bswap %0"
-                     : "=r" (result)
-                     : "0"  (result));
-    return result;
+    __asm__ ("xchgb %b0, %h0" : "+q" (data));
+    return data;
 }
 
 OS_INLINE
-UInt64
-OSReadSwapInt64(
-    volatile void               * base,
-    UInt                          offset
+uint32_t
+_OSSwapInt32(
+    uint32_t        data
 )
 {
-    UInt64 * inp;
-    union ullc {
-        UInt64     ull;
-        UInt       ul[2];
-    } outv;
-
-    inp = (UInt64 *)base;
-    outv.ul[0] = OSReadSwapInt32(inp, offset + 4);
-    outv.ul[1] = OSReadSwapInt32(inp, offset);
-    return outv.ull;
+    __asm__ ("bswap %0" : "+r" (data));
+    return data;
 }
 
 OS_INLINE
-UInt
-OSReadSwapInt(
-    volatile void               * base,
-    UInt                          offset
+uint64_t
+_OSSwapInt64(
+    uint64_t        data
 )
 {
-    UInt result;
-
-    result = *(volatile UInt *)((UInt8 *)base + offset);
-    __asm__ volatile("bswap %0"
-                     : "=r" (result)
-                     : "0"  (result));
-    return result;
+    union {
+        uint64_t ull;
+        uint32_t ul[2];
+    } u;
+
+    /* This actually generates the best code */
+    u.ul[0] = data >> 32;
+    u.ul[1] = data & 0xffffffff;
+    u.ul[0] = _OSSwapInt32(u.ul[0]);
+    u.ul[1] = _OSSwapInt32(u.ul[1]);
+    return u.ull;
 }
 
-/* Functions for byte reversed stores. */
+/* Functions for byte reversed loads. */
 
 OS_INLINE
-void
-OSWriteSwapInt16(
-    volatile void               * base,
-    UInt                          offset,
-    UInt16                        data
+uint16_t
+OSReadSwapInt16(
+    const volatile void   * base,
+    uintptr_t       offset
 )
 {
-    __asm__ volatile("xchgb %b0,%h0"
-                     : "=q" (data)
-                     : "0"  (data));
-    *(volatile UInt16 *)((UInt8 *)base + offset) = data;
+    uint16_t result;
+
+    result = *(volatile uint16_t *)((uintptr_t)base + offset);
+    return _OSSwapInt16(result);
 }
 
 OS_INLINE
-void
-OSWriteSwapInt32(
-    volatile void               * base,
-    UInt                          offset,
-    UInt32                        data
+uint32_t
+OSReadSwapInt32(
+    const volatile void   * base,
+    uintptr_t       offset
 )
 {
-    __asm__ volatile("bswap %0"
-                     : "=r" (data)
-                     : "0"  (data));
-    *(volatile UInt32 *)((UInt8 *)base + offset) = data;
+    uint32_t result;
+
+    result = *(volatile uint32_t *)((uintptr_t)base + offset);
+    return _OSSwapInt32(result);
 }
 
 OS_INLINE
-void
-OSWriteSwapInt64(
-    volatile void               * base,
-    UInt                          offset,
-    UInt64                        data
+uint64_t
+OSReadSwapInt64(
+    const volatile void   * base,
+    uintptr_t       offset
 )
 {
-    UInt64 * outp;
+    const volatile uint32_t * inp;
     union ullc {
-        UInt64     ull;
-        UInt       ul[2];
-    } *inp;
-
-    outp = (UInt64 *)base;
-    inp  = (union ullc *)&data;
-    OSWriteSwapInt32(outp, offset, inp->ul[1]);
-    OSWriteSwapInt32(outp, offset + 4, inp->ul[0]);
-}
+        uint64_t     ull;
+        uint32_t     ul[2];
+    } outv;
 
-OS_INLINE
-void
-OSWriteSwapInt(
-    volatile void               * base,
-    UInt                          offset,
-    UInt                          data
-)
-{
-    __asm__ volatile("bswap %0"
-                     : "=r" (data)
-                     : "0"  (data));
-    *(volatile UInt *)((UInt8 *)base + offset) = data;
+    inp = (const volatile uint32_t *)((uintptr_t)base + offset);
+    outv.ul[0] = inp[1];
+    outv.ul[1] = inp[0];
+    outv.ul[0] = _OSSwapInt32(outv.ul[0]);
+    outv.ul[1] = _OSSwapInt32(outv.ul[1]);
+    return outv.ull;
 }
 
-/* Generic byte swapping functions. */
-
-OS_INLINE
-UInt16
-OSSwapInt16(
-    UInt16                        data
-)
-{
-    UInt16 temp = data;
-    return OSReadSwapInt16(&temp, 0);
-}
+/* Functions for byte reversed stores. */
 
 OS_INLINE
-UInt32
-OSSwapInt32(
-    UInt32                        data
+void
+OSWriteSwapInt16(
+    volatile void   * base,
+    uintptr_t       offset,
+    uint16_t        data
 )
 {
-    UInt32 temp = data;
-    return OSReadSwapInt32(&temp, 0);
+    *(volatile uint16_t *)((uintptr_t)base + offset) = _OSSwapInt16(data);
 }
 
 OS_INLINE
-UInt64
-OSSwapInt64(
-    UInt64                        data
+void
+OSWriteSwapInt32(
+    volatile void   * base,
+    uintptr_t       offset,
+    uint32_t        data
 )
 {
-    UInt64 temp = data;
-    return OSReadSwapInt64(&temp, 0);
+    *(volatile uint32_t *)((uintptr_t)base + offset) = _OSSwapInt32(data);
 }
 
 OS_INLINE
-UInt
-OSSwapInt(
-    UInt                          data
+void
+OSWriteSwapInt64(
+    volatile void    * base,
+    uintptr_t        offset,
+    uint64_t         data
 )
 {
-    UInt temp = data;
-    return OSReadSwapInt(&temp, 0);
+    *(volatile uint64_t *)((uintptr_t)base + offset) = _OSSwapInt64(data);
 }
 
 #endif /* ! _OS_OSBYTEORDERI386_H */