source: libpipi/trunk/common.h @ 2844

Last change on this file since 2844 was 2844, checked in by Sam Hocevar, 11 years ago

Fix headers.

File size: 2.1 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To 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#ifndef __COMMON_H__
21#define __COMMON_H__
22
23/* C99 types */
24#if defined HAVE_INTTYPES_H && !defined __KERNEL__
25#   include <inttypes.h>
26#else
27typedef signed char int8_t;
28typedef signed short int16_t;
29typedef signed long int int32_t;
30
31typedef unsigned char uint8_t;
32typedef unsigned short uint16_t;
33typedef unsigned long int uint32_t;
34
35typedef long int intptr_t;
36typedef unsigned long int uintptr_t;
37#endif
38
39/* hton16() and hton32() */
40#if defined HAVE_HTONS
41#   if defined __KERNEL__
42        /* Nothing to do */
43#   elif defined HAVE_ARPA_INET_H
44#       include <arpa/inet.h>
45#   elif defined HAVE_NETINET_IN_H
46#       include <netinet/in.h>
47#   endif
48#   define hton16 htons
49#   define hton32 htonl
50#else
51#   if defined HAVE_ENDIAN_H
52#       include <endian.h>
53#   endif
54static inline uint16_t hton16(uint16_t x)
55{
56    /* This is compile-time optimised with at least -O1 or -Os */
57#if defined HAVE_ENDIAN_H
58    if(__BYTE_ORDER == __BIG_ENDIAN)
59#else
60    uint32_t const dummy = 0x12345678;
61    if(*(uint8_t const *)&dummy == 0x12)
62#endif
63        return x;
64    else
65        return (x >> 8) | (x << 8);
66}
67
68static inline uint32_t hton32(uint32_t x)
69{
70    /* This is compile-time optimised with at least -O1 or -Os */
71#if defined HAVE_ENDIAN_H
72    if(__BYTE_ORDER == __BIG_ENDIAN)
73#else
74    uint32_t const dummy = 0x12345678;
75    if(*(uint8_t const *)&dummy == 0x12)
76#endif
77        return x;
78    else
79        return (x >> 24) | ((x >> 8) & 0x0000ff00)
80                | ((x << 8) & 0x00ff0000) | (x << 24);
81}
82#endif
83
84#endif /* __COMMON_H__ */
85
Note: See TracBrowser for help on using the repository browser.