pico_port.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef PICO_PORT_INCLUDED
  2. #define PICO_PORT_INCLUDED
  3. // provide size_t, uintptr_t
  4. #include <stdlib.h>
  5. #if !(defined(_MSC_VER) && _MSC_VER < 1800)
  6. #include <stdint.h>
  7. #endif
  8. #include "pico_types.h"
  9. #if defined(__GNUC__) && defined(__i386__)
  10. #define REGPARM(x) __attribute__((regparm(x)))
  11. #else
  12. #define REGPARM(x)
  13. #endif
  14. #ifdef __GNUC__
  15. #define NOINLINE __attribute__((noinline))
  16. #define ALIGNED(n) __attribute__((aligned(n)))
  17. #define unlikely(x) __builtin_expect((x), 0)
  18. #define likely(x) __builtin_expect(!!(x), 1)
  19. #else
  20. #define NOINLINE
  21. #define ALIGNED(n)
  22. #define unlikely(x) (x)
  23. #define likely(x) (x)
  24. #endif
  25. #ifdef _MSC_VER
  26. #define snprintf _snprintf
  27. #define strcasecmp _stricmp
  28. #define strncasecmp _strnicmp
  29. #define strdup _strdup
  30. #endif
  31. // There's no standard way to determine endianess at compile time. Try using
  32. // some well known non-standard macros for detection.
  33. #if defined __BYTE_ORDER__
  34. #define CPU_IS_LE __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  35. #elif defined __BYTE_ORDER
  36. #define CPU_IS_LE __BYTE_ORDER == __LITTLE_ENDIAN
  37. #elif defined __BIG_ENDIAN__ || defined _M_PPC // Windows on PPC was big endian
  38. #define CPU_IS_LE 0
  39. #elif defined __LITTLE_ENDIAN__ || defined _WIN32 // all other Windows is LE
  40. #define CPU_IS_LE 1
  41. #else
  42. #warning "can't detect byte order, assume little endian"
  43. #define CPU_IS_LE 1
  44. #endif
  45. // NB mixed endian integer platforms are not supported.
  46. #if CPU_IS_LE
  47. // address/offset operations
  48. #define MEM_BE2(a) ((a)^1) // addr/offs of u8 in u16, or u16 in u32
  49. #define MEM_BE4(a) ((a)^3) // addr/offs of u8 in u32
  50. #define MEM_LE2(a) (a)
  51. #define MEM_LE4(a) (a)
  52. // swapping
  53. #define CPU_BE2(v) ((u32)((u64)(v)<<16)|((u32)(v)>>16))
  54. #define CPU_BE4(v) (((u32)(v)>>24)|(((v)>>8)&0x00ff00)| \
  55. (((v)<<8)&0xff0000)|(u32)((v)<<24))
  56. #define CPU_LE2(v) (v) // swap of 2*u16 in u32
  57. #define CPU_LE4(v) (v) // swap of 4*u8 in u32
  58. #else
  59. // address/offset operations
  60. #define MEM_BE2(a) (a)
  61. #define MEM_BE4(a) (a)
  62. #define MEM_LE2(a) ((a)^1)
  63. #define MEM_LE4(a) ((a)^3)
  64. // swapping
  65. #define CPU_BE2(v) (v)
  66. #define CPU_BE4(v) (v)
  67. #define CPU_LE2(v) ((u32)((u64)(v)<<16)|((u32)(v)>>16))
  68. #define CPU_LE4(v) (((u32)(v)>>24)|(((v)>>8)&0x00ff00)| \
  69. (((v)<<8)&0xff0000)|(u32)((v)<<24))
  70. #endif
  71. #endif // PICO_PORT_INCLUDED