int_altivec.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2007 Luca Barbato <lu_zero@gentoo.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. ** @file libavcodec/ppc/int_altivec.c
  22. ** integer misc ops.
  23. **/
  24. #include "config.h"
  25. #if HAVE_ALTIVEC_H
  26. #include <altivec.h>
  27. #endif
  28. #include "libavcodec/dsputil.h"
  29. #include "dsputil_altivec.h"
  30. #include "types_altivec.h"
  31. static int ssd_int8_vs_int16_altivec(const int8_t *pix1, const int16_t *pix2,
  32. int size) {
  33. int i, size16;
  34. vector signed char vpix1;
  35. vector signed short vpix2, vdiff, vpix1l,vpix1h;
  36. union { vector signed int vscore;
  37. int32_t score[4];
  38. } u;
  39. u.vscore = vec_splat_s32(0);
  40. //
  41. //XXX lazy way, fix it later
  42. #define vec_unaligned_load(b) \
  43. vec_perm(vec_ld(0,b),vec_ld(15,b),vec_lvsl(0, b));
  44. size16 = size >> 4;
  45. while(size16) {
  46. // score += (pix1[i]-pix2[i])*(pix1[i]-pix2[i]);
  47. //load pix1 and the first batch of pix2
  48. vpix1 = vec_unaligned_load(pix1);
  49. vpix2 = vec_unaligned_load(pix2);
  50. pix2 += 8;
  51. //unpack
  52. vpix1h = vec_unpackh(vpix1);
  53. vdiff = vec_sub(vpix1h, vpix2);
  54. vpix1l = vec_unpackl(vpix1);
  55. // load another batch from pix2
  56. vpix2 = vec_unaligned_load(pix2);
  57. u.vscore = vec_msum(vdiff, vdiff, u.vscore);
  58. vdiff = vec_sub(vpix1l, vpix2);
  59. u.vscore = vec_msum(vdiff, vdiff, u.vscore);
  60. pix1 += 16;
  61. pix2 += 8;
  62. size16--;
  63. }
  64. u.vscore = vec_sums(u.vscore, vec_splat_s32(0));
  65. size %= 16;
  66. for (i = 0; i < size; i++) {
  67. u.score[3] += (pix1[i]-pix2[i])*(pix1[i]-pix2[i]);
  68. }
  69. return u.score[3];
  70. }
  71. static void add_int16_altivec(int16_t * v1, int16_t * v2, int order)
  72. {
  73. int i;
  74. register vec_s16 vec, *pv;
  75. for(i = 0; i < order; i += 8){
  76. pv = (vec_s16*)v2;
  77. vec = vec_perm(pv[0], pv[1], vec_lvsl(0, v2));
  78. vec_st(vec_add(vec_ld(0, v1), vec), 0, v1);
  79. v1 += 8;
  80. v2 += 8;
  81. }
  82. }
  83. static void sub_int16_altivec(int16_t * v1, int16_t * v2, int order)
  84. {
  85. int i;
  86. register vec_s16 vec, *pv;
  87. for(i = 0; i < order; i += 8){
  88. pv = (vec_s16*)v2;
  89. vec = vec_perm(pv[0], pv[1], vec_lvsl(0, v2));
  90. vec_st(vec_sub(vec_ld(0, v1), vec), 0, v1);
  91. v1 += 8;
  92. v2 += 8;
  93. }
  94. }
  95. static int32_t scalarproduct_int16_altivec(int16_t * v1, int16_t * v2, int order, const int shift)
  96. {
  97. int i;
  98. LOAD_ZERO;
  99. register vec_s16 vec1, *pv;
  100. register vec_s32 res = vec_splat_s32(0), t;
  101. register vec_u32 shifts;
  102. DECLARE_ALIGNED_16(int32_t, ires);
  103. shifts = zero_u32v;
  104. if(shift & 0x10) shifts = vec_add(shifts, vec_sl(vec_splat_u32(0x08), vec_splat_u32(0x1)));
  105. if(shift & 0x08) shifts = vec_add(shifts, vec_splat_u32(0x08));
  106. if(shift & 0x04) shifts = vec_add(shifts, vec_splat_u32(0x04));
  107. if(shift & 0x02) shifts = vec_add(shifts, vec_splat_u32(0x02));
  108. if(shift & 0x01) shifts = vec_add(shifts, vec_splat_u32(0x01));
  109. for(i = 0; i < order; i += 8){
  110. pv = (vec_s16*)v1;
  111. vec1 = vec_perm(pv[0], pv[1], vec_lvsl(0, v1));
  112. t = vec_msum(vec1, vec_ld(0, v2), zero_s32v);
  113. t = vec_sr(t, shifts);
  114. res = vec_sums(t, res);
  115. v1 += 8;
  116. v2 += 8;
  117. }
  118. res = vec_splat(res, 3);
  119. vec_ste(res, 0, &ires);
  120. return ires;
  121. }
  122. void int_init_altivec(DSPContext* c, AVCodecContext *avctx)
  123. {
  124. c->ssd_int8_vs_int16 = ssd_int8_vs_int16_altivec;
  125. c->add_int16 = add_int16_altivec;
  126. c->sub_int16 = sub_int16_altivec;
  127. c->scalarproduct_int16 = scalarproduct_int16_altivec;
  128. }