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.

146 lines
4.3KB

  1. /*
  2. * Copyright (c) 2007 Luca Barbato <lu_zero@gentoo.org>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. ** @file
  22. ** integer misc ops.
  23. **/
  24. #include "config.h"
  25. #if HAVE_ALTIVEC_H
  26. #include <altivec.h>
  27. #endif
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/ppc/types_altivec.h"
  30. #include "libavcodec/dsputil.h"
  31. #include "dsputil_altivec.h"
  32. static int ssd_int8_vs_int16_altivec(const int8_t *pix1, const int16_t *pix2,
  33. int size) {
  34. int i, size16;
  35. vector signed char vpix1;
  36. vector signed short vpix2, vdiff, vpix1l,vpix1h;
  37. union { vector signed int vscore;
  38. int32_t score[4];
  39. } u;
  40. u.vscore = vec_splat_s32(0);
  41. //
  42. //XXX lazy way, fix it later
  43. #define vec_unaligned_load(b) \
  44. vec_perm(vec_ld(0,b),vec_ld(15,b),vec_lvsl(0, b));
  45. size16 = size >> 4;
  46. while(size16) {
  47. // score += (pix1[i]-pix2[i])*(pix1[i]-pix2[i]);
  48. //load pix1 and the first batch of pix2
  49. vpix1 = vec_unaligned_load(pix1);
  50. vpix2 = vec_unaligned_load(pix2);
  51. pix2 += 8;
  52. //unpack
  53. vpix1h = vec_unpackh(vpix1);
  54. vdiff = vec_sub(vpix1h, vpix2);
  55. vpix1l = vec_unpackl(vpix1);
  56. // load another batch from pix2
  57. vpix2 = vec_unaligned_load(pix2);
  58. u.vscore = vec_msum(vdiff, vdiff, u.vscore);
  59. vdiff = vec_sub(vpix1l, vpix2);
  60. u.vscore = vec_msum(vdiff, vdiff, u.vscore);
  61. pix1 += 16;
  62. pix2 += 8;
  63. size16--;
  64. }
  65. u.vscore = vec_sums(u.vscore, vec_splat_s32(0));
  66. size %= 16;
  67. for (i = 0; i < size; i++) {
  68. u.score[3] += (pix1[i]-pix2[i])*(pix1[i]-pix2[i]);
  69. }
  70. return u.score[3];
  71. }
  72. static int32_t scalarproduct_int16_altivec(const int16_t *v1, const int16_t *v2,
  73. int order)
  74. {
  75. int i;
  76. LOAD_ZERO;
  77. const vec_s16 *pv;
  78. register vec_s16 vec1;
  79. register vec_s32 res = vec_splat_s32(0), t;
  80. int32_t ires;
  81. for(i = 0; i < order; i += 8){
  82. pv = (const vec_s16*)v1;
  83. vec1 = vec_perm(pv[0], pv[1], vec_lvsl(0, v1));
  84. t = vec_msum(vec1, vec_ld(0, v2), zero_s32v);
  85. res = vec_sums(t, res);
  86. v1 += 8;
  87. v2 += 8;
  88. }
  89. res = vec_splat(res, 3);
  90. vec_ste(res, 0, &ires);
  91. return ires;
  92. }
  93. static int32_t scalarproduct_and_madd_int16_altivec(int16_t *v1, const int16_t *v2, const int16_t *v3, int order, int mul)
  94. {
  95. LOAD_ZERO;
  96. vec_s16 *pv1 = (vec_s16*)v1;
  97. register vec_s16 muls = {mul,mul,mul,mul,mul,mul,mul,mul};
  98. register vec_s16 t0, t1, i0, i1, i4;
  99. register vec_s16 i2 = vec_ld(0, v2), i3 = vec_ld(0, v3);
  100. register vec_s32 res = zero_s32v;
  101. register vec_u8 align = vec_lvsl(0, v2);
  102. int32_t ires;
  103. order >>= 4;
  104. do {
  105. i1 = vec_ld(16, v2);
  106. t0 = vec_perm(i2, i1, align);
  107. i2 = vec_ld(32, v2);
  108. t1 = vec_perm(i1, i2, align);
  109. i0 = pv1[0];
  110. i1 = pv1[1];
  111. res = vec_msum(t0, i0, res);
  112. res = vec_msum(t1, i1, res);
  113. i4 = vec_ld(16, v3);
  114. t0 = vec_perm(i3, i4, align);
  115. i3 = vec_ld(32, v3);
  116. t1 = vec_perm(i4, i3, align);
  117. pv1[0] = vec_mladd(t0, muls, i0);
  118. pv1[1] = vec_mladd(t1, muls, i1);
  119. pv1 += 2;
  120. v2 += 8;
  121. v3 += 8;
  122. } while(--order);
  123. res = vec_splat(vec_sums(res, zero_s32v), 3);
  124. vec_ste(res, 0, &ires);
  125. return ires;
  126. }
  127. av_cold void ff_int_init_altivec(DSPContext *c, AVCodecContext *avctx)
  128. {
  129. c->ssd_int8_vs_int16 = ssd_int8_vs_int16_altivec;
  130. c->scalarproduct_int16 = scalarproduct_int16_altivec;
  131. c->scalarproduct_and_madd_int16 = scalarproduct_and_madd_int16_altivec;
  132. }