mem.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2016, Alliance for Open Media. All rights reserved
  3. *
  4. * This source code is subject to the terms of the BSD 2 Clause License and
  5. * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
  6. * was not distributed with this source code in the LICENSE file, you can
  7. * obtain it at www.aomedia.org/license/software. If the Alliance for Open
  8. * Media Patent License 1.0 was not distributed with this source code in the
  9. * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  10. */
  11. #ifndef AOM_AOM_PORTS_MEM_H_
  12. #define AOM_AOM_PORTS_MEM_H_
  13. #include "aom/aom_integer.h"
  14. #include "config/aom_config.h"
  15. #if (defined(__GNUC__) && __GNUC__) || defined(__SUNPRO_C)
  16. #define DECLARE_ALIGNED(n, typ, val) typ val __attribute__((aligned(n)))
  17. #elif defined(_MSC_VER)
  18. #define DECLARE_ALIGNED(n, typ, val) __declspec(align(n)) typ val
  19. #else
  20. #warning No alignment directives known for this compiler.
  21. #define DECLARE_ALIGNED(n, typ, val) typ val
  22. #endif
  23. /* Indicates that the usage of the specified variable has been audited to assure
  24. * that it's safe to use uninitialized. Silences 'may be used uninitialized'
  25. * warnings on gcc.
  26. */
  27. #if defined(__GNUC__) && __GNUC__
  28. #define UNINITIALIZED_IS_SAFE(x) x = x
  29. #else
  30. #define UNINITIALIZED_IS_SAFE(x) x
  31. #endif
  32. #if HAVE_NEON && defined(_MSC_VER)
  33. #define __builtin_prefetch(x)
  34. #endif
  35. /* Shift down with rounding for use when n >= 0. Usually value >= 0, but the
  36. * macro can be used with a negative value if the direction of rounding is
  37. * acceptable.
  38. */
  39. #define ROUND_POWER_OF_TWO(value, n) (((value) + (((1 << (n)) >> 1))) >> (n))
  40. /* Shift down with rounding for signed integers, for use when n >= 0 */
  41. #define ROUND_POWER_OF_TWO_SIGNED(value, n) \
  42. (((value) < 0) ? -ROUND_POWER_OF_TWO(-(value), (n)) \
  43. : ROUND_POWER_OF_TWO((value), (n)))
  44. /* Shift down with rounding for use when n >= 0 (64-bit value). Usually
  45. * value >= 0, but the macro can be used with a negative value if the direction
  46. * of rounding is acceptable.
  47. */
  48. #define ROUND_POWER_OF_TWO_64(value, n) \
  49. (((value) + ((((int64_t)1 << (n)) >> 1))) >> (n))
  50. /* Shift down with rounding for signed integers, for use when n >= 0 (64-bit
  51. * value)
  52. */
  53. #define ROUND_POWER_OF_TWO_SIGNED_64(value, n) \
  54. (((value) < 0) ? -ROUND_POWER_OF_TWO_64(-(value), (n)) \
  55. : ROUND_POWER_OF_TWO_64((value), (n)))
  56. /* Shift down with ceil() for use when n >= 0 and value >= 0.*/
  57. #define CEIL_POWER_OF_TWO(value, n) (((value) + (1 << (n)) - 1) >> (n))
  58. /* shift right or left depending on sign of n */
  59. #define RIGHT_SIGNED_SHIFT(value, n) \
  60. ((n) < 0 ? ((value) << (-(n))) : ((value) >> (n)))
  61. #define ALIGN_POWER_OF_TWO(value, n) \
  62. (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
  63. #define ALIGN_POWER_OF_TWO_UNSIGNED(value, n) \
  64. (((value) + ((1u << (n)) - 1)) & ~((1u << (n)) - 1))
  65. #define DIVIDE_AND_ROUND(x, y) (((x) + ((y) >> 1)) / (y))
  66. #define CONVERT_TO_SHORTPTR(x) ((uint16_t *)(((uintptr_t)(x)) << 1))
  67. #define CONVERT_TO_BYTEPTR(x) ((uint8_t *)(((uintptr_t)(x)) >> 1))
  68. /*!\brief force enum to be unsigned 1 byte*/
  69. #define UENUM1BYTE(enumvar) \
  70. ; \
  71. typedef uint8_t enumvar
  72. /*!\brief force enum to be signed 1 byte*/
  73. #define SENUM1BYTE(enumvar) \
  74. ; \
  75. typedef int8_t enumvar
  76. /*!\brief force enum to be unsigned 2 byte*/
  77. #define UENUM2BYTE(enumvar) \
  78. ; \
  79. typedef uint16_t enumvar
  80. /*!\brief force enum to be signed 2 byte*/
  81. #define SENUM2BYTE(enumvar) \
  82. ; \
  83. typedef int16_t enumvar
  84. /*!\brief force enum to be unsigned 4 byte*/
  85. #define UENUM4BYTE(enumvar) \
  86. ; \
  87. typedef uint32_t enumvar
  88. /*!\brief force enum to be unsigned 4 byte*/
  89. #define SENUM4BYTE(enumvar) \
  90. ; \
  91. typedef int32_t enumvar
  92. #endif // AOM_AOM_PORTS_MEM_H_