You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.4KB

  1. /*
  2. * Copyright (c) 2002 Brian Foley
  3. * Copyright (c) 2002 Dieter Shirley
  4. * Copyright (c) 2003-2004 Romain Dolbeau <romain@dolbeau.org>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #if HAVE_ALTIVEC_H
  24. #include <altivec.h>
  25. #endif
  26. #include <string.h>
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/cpu.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/ppc/cpu.h"
  31. #include "libavutil/ppc/types_altivec.h"
  32. #include "libavcodec/blockdsp.h"
  33. /* ***** WARNING ***** WARNING ***** WARNING ***** */
  34. /*
  35. * clear_blocks_dcbz32_ppc will not work properly on PowerPC processors with
  36. * a cache line size not equal to 32 bytes. Fortunately all processors used
  37. * by Apple up to at least the 7450 (AKA second generation G4) use 32-byte
  38. * cache lines. This is due to the use of the 'dcbz' instruction. It simply
  39. * clears a single cache line to zero, so you need to know the cache line
  40. * size to use it! It's absurd, but it's fast...
  41. *
  42. * update 24/06/2003: Apple released the G5 yesterday, with a PPC970.
  43. * cache line size: 128 bytes. Oups.
  44. * The semantics of dcbz was changed, it always clears 32 bytes. So the function
  45. * below will work, but will be slow. So I fixed check_dcbz_effect to use dcbzl,
  46. * which is defined to clear a cache line (as dcbz before). So we can still
  47. * distinguish, and use dcbz (32 bytes) or dcbzl (one cache line) as required.
  48. *
  49. * see <http://developer.apple.com/technotes/tn/tn2087.html>
  50. * and <http://developer.apple.com/technotes/tn/tn2086.html>
  51. */
  52. static void clear_blocks_dcbz32_ppc(int16_t *blocks)
  53. {
  54. register int misal = (unsigned long) blocks & 0x00000010, i = 0;
  55. if (misal) {
  56. ((unsigned long *) blocks)[0] = 0L;
  57. ((unsigned long *) blocks)[1] = 0L;
  58. ((unsigned long *) blocks)[2] = 0L;
  59. ((unsigned long *) blocks)[3] = 0L;
  60. i += 16;
  61. }
  62. for (; i < sizeof(int16_t) * 6 * 64 - 31; i += 32)
  63. __asm__ volatile ("dcbz %0,%1" :: "b" (blocks), "r" (i) : "memory");
  64. if (misal) {
  65. ((unsigned long *) blocks)[188] = 0L;
  66. ((unsigned long *) blocks)[189] = 0L;
  67. ((unsigned long *) blocks)[190] = 0L;
  68. ((unsigned long *) blocks)[191] = 0L;
  69. i += 16;
  70. }
  71. }
  72. /* Same as above, when dcbzl clears a whole 128 bytes cache line
  73. * i.e. the PPC970 AKA G5. */
  74. static void clear_blocks_dcbz128_ppc(int16_t *blocks)
  75. {
  76. #if HAVE_DCBZL
  77. register int misal = (unsigned long) blocks & 0x0000007f, i = 0;
  78. if (misal) {
  79. /* We could probably also optimize this case,
  80. * but there's not much point as the machines
  81. * aren't available yet (2003-06-26). */
  82. memset(blocks, 0, sizeof(int16_t) * 6 * 64);
  83. } else {
  84. for (; i < sizeof(int16_t) * 6 * 64; i += 128)
  85. __asm__ volatile ("dcbzl %0,%1" :: "b" (blocks), "r" (i) : "memory");
  86. }
  87. #else
  88. memset(blocks, 0, sizeof(int16_t) * 6 * 64);
  89. #endif
  90. }
  91. /* Check dcbz report how many bytes are set to 0 by dcbz. */
  92. /* update 24/06/2003: Replace dcbz by dcbzl to get the intended effect
  93. * (Apple "fixed" dcbz). Unfortunately this cannot be used unless the
  94. * assembler knows about dcbzl ... */
  95. static long check_dcbzl_effect(void)
  96. {
  97. long count = 0;
  98. #if HAVE_DCBZL
  99. register char *fakedata = av_malloc(1024);
  100. register char *fakedata_middle;
  101. register long zero = 0, i = 0;
  102. if (!fakedata)
  103. return 0L;
  104. fakedata_middle = fakedata + 512;
  105. memset(fakedata, 0xFF, 1024);
  106. /* Below the constraint "b" seems to mean "address base register"
  107. * in gcc-3.3 / RS/6000 speaks. Seems to avoid using r0, so.... */
  108. __asm__ volatile ("dcbzl %0, %1" :: "b" (fakedata_middle), "r" (zero));
  109. for (i = 0; i < 1024; i++)
  110. if (fakedata[i] == (char) 0)
  111. count++;
  112. av_free(fakedata);
  113. #endif
  114. return count;
  115. }
  116. #if HAVE_ALTIVEC
  117. static void clear_block_altivec(int16_t *block)
  118. {
  119. LOAD_ZERO;
  120. vec_st(zero_s16v, 0, block);
  121. vec_st(zero_s16v, 16, block);
  122. vec_st(zero_s16v, 32, block);
  123. vec_st(zero_s16v, 48, block);
  124. vec_st(zero_s16v, 64, block);
  125. vec_st(zero_s16v, 80, block);
  126. vec_st(zero_s16v, 96, block);
  127. vec_st(zero_s16v, 112, block);
  128. }
  129. #endif /* HAVE_ALTIVEC */
  130. av_cold void ff_blockdsp_init_ppc(BlockDSPContext *c, unsigned high_bit_depth)
  131. {
  132. // common optimizations whether AltiVec is available or not
  133. if (!high_bit_depth) {
  134. switch (check_dcbzl_effect()) {
  135. case 32:
  136. c->clear_blocks = clear_blocks_dcbz32_ppc;
  137. break;
  138. case 128:
  139. c->clear_blocks = clear_blocks_dcbz128_ppc;
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. #if HAVE_ALTIVEC
  146. if (!PPC_ALTIVEC(av_get_cpu_flags()))
  147. return;
  148. if (!high_bit_depth)
  149. c->clear_block = clear_block_altivec;
  150. #endif /* HAVE_ALTIVEC */
  151. }