No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

common.h 2.1 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * libpipi Proper image processing implementation 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. * This file contains replacements for commonly found object types and
  16. * function prototypes that are sometimes missing.
  17. */
  18. #ifndef __COMMON_H__
  19. #define __COMMON_H__
  20. /* C99 types */
  21. #if defined HAVE_INTTYPES_H && !defined __KERNEL__
  22. # include <inttypes.h>
  23. #else
  24. typedef signed char int8_t;
  25. typedef signed short int16_t;
  26. typedef signed long int int32_t;
  27. typedef unsigned char uint8_t;
  28. typedef unsigned short uint16_t;
  29. typedef unsigned long int uint32_t;
  30. typedef long int intptr_t;
  31. typedef unsigned long int uintptr_t;
  32. #endif
  33. /* hton16() and hton32() */
  34. #if defined HAVE_HTONS
  35. # if defined __KERNEL__
  36. /* Nothing to do */
  37. # elif defined HAVE_ARPA_INET_H
  38. # include <arpa/inet.h>
  39. # elif defined HAVE_NETINET_IN_H
  40. # include <netinet/in.h>
  41. # endif
  42. # define hton16 htons
  43. # define hton32 htonl
  44. #else
  45. # if defined HAVE_ENDIAN_H
  46. # include <endian.h>
  47. # endif
  48. static inline uint16_t hton16(uint16_t x)
  49. {
  50. /* This is compile-time optimised with at least -O1 or -Os */
  51. #if defined HAVE_ENDIAN_H
  52. if(__BYTE_ORDER == __BIG_ENDIAN)
  53. #else
  54. uint32_t const dummy = 0x12345678;
  55. if(*(uint8_t const *)&dummy == 0x12)
  56. #endif
  57. return x;
  58. else
  59. return (x >> 8) | (x << 8);
  60. }
  61. static inline uint32_t hton32(uint32_t x)
  62. {
  63. /* This is compile-time optimised with at least -O1 or -Os */
  64. #if defined HAVE_ENDIAN_H
  65. if(__BYTE_ORDER == __BIG_ENDIAN)
  66. #else
  67. uint32_t const dummy = 0x12345678;
  68. if(*(uint8_t const *)&dummy == 0x12)
  69. #endif
  70. return x;
  71. else
  72. return (x >> 24) | ((x >> 8) & 0x0000ff00)
  73. | ((x << 8) & 0x00ff0000) | (x << 24);
  74. }
  75. #endif
  76. #endif /* __COMMON_H__ */