source: libcaca/trunk/common.h @ 892

Last change on this file since 892 was 865, checked in by Sam Hocevar, 15 years ago
  • Renamed htons/htonl to hton16/hton32 to avoid useless conflicts.
  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  libcaca       Colour ASCII-Art library
4 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
5 *                All Rights Reserved
6 *
7 *  $Id: common.h 865 2006-04-24 21:09:06Z sam $
8 *
9 *  This library is free software; you can redistribute it and/or
10 *  modify it under the terms of the Do What The Fuck You Want To
11 *  Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains replacements for commonly found object types and
17 *  function prototypes that are sometimes missing.
18 */
19
20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#else
23typedef signed char int8_t;
24typedef signed short int16_t;
25typedef signed long int int32_t;
26
27typedef unsigned char uint8_t;
28typedef unsigned short uint16_t;
29typedef unsigned long int uint32_t;
30
31typedef long int intptr_t;
32typedef unsigned long int uintptr_t;
33#endif
34
35#if defined(HAVE_HTONS)
36#   define hton16 htons
37#   define hton32 htonl
38#else
39#   if defined(HAVE_ENDIAN_H)
40#       include <endian.h>
41#   endif
42static inline uint16_t hton16(uint16_t x)
43{
44    /* This is compile-time optimised with at least -O1 or -Os */
45#if defined(HAVE_ENDIAN_H)
46    if(__BYTE_ORDER == __BIG_ENDIAN)
47#else
48    uint32_t const dummy = 0x12345678;
49    if(*(uint8_t const *)&dummy == 0x12)
50#endif
51        return x;
52    else
53        return (x >> 8) | (x << 8);
54}
55
56static inline uint32_t hton32(uint32_t x)
57{
58    /* This is compile-time optimised with at least -O1 or -Os */
59#if defined(HAVE_ENDIAN_H)
60    if(__BYTE_ORDER == __BIG_ENDIAN)
61#else
62    uint32_t const dummy = 0x12345678;
63    if(*(uint8_t const *)&dummy == 0x12)
64#endif
65        return x;
66    else
67        return (x >> 24) | ((x >> 8) & 0x0000ff00)
68                | ((x << 8) & 0x00ff0000) | (x << 24);
69}
70#endif
71
Note: See TracBrowser for help on using the repository browser.