rescaler_mips32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // MIPS version of rescaling functions
  11. //
  12. // Author(s): Djordje Pesut (djordje.pesut@imgtec.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_MIPS32)
  15. #include <assert.h>
  16. #include "../utils/rescaler_utils.h"
  17. //------------------------------------------------------------------------------
  18. // Row import
  19. static void ImportRowShrink(WebPRescaler* const wrk, const uint8_t* src) {
  20. const int x_stride = wrk->num_channels;
  21. const int x_out_max = wrk->dst_width * wrk->num_channels;
  22. const int fx_scale = wrk->fx_scale;
  23. const int x_add = wrk->x_add;
  24. const int x_sub = wrk->x_sub;
  25. const int x_stride1 = x_stride << 2;
  26. int channel;
  27. assert(!wrk->x_expand);
  28. assert(!WebPRescalerInputDone(wrk));
  29. for (channel = 0; channel < x_stride; ++channel) {
  30. const uint8_t* src1 = src + channel;
  31. rescaler_t* frow = wrk->frow + channel;
  32. int temp1, temp2, temp3;
  33. int base, frac, sum;
  34. int accum, accum1;
  35. int loop_c = x_out_max - channel;
  36. __asm__ volatile (
  37. "li %[temp1], 0x8000 \n\t"
  38. "li %[temp2], 0x10000 \n\t"
  39. "li %[sum], 0 \n\t"
  40. "li %[accum], 0 \n\t"
  41. "1: \n\t"
  42. "addu %[accum], %[accum], %[x_add] \n\t"
  43. "li %[base], 0 \n\t"
  44. "blez %[accum], 3f \n\t"
  45. "2: \n\t"
  46. "lbu %[base], 0(%[src1]) \n\t"
  47. "subu %[accum], %[accum], %[x_sub] \n\t"
  48. "addu %[src1], %[src1], %[x_stride] \n\t"
  49. "addu %[sum], %[sum], %[base] \n\t"
  50. "bgtz %[accum], 2b \n\t"
  51. "3: \n\t"
  52. "negu %[accum1], %[accum] \n\t"
  53. "mul %[frac], %[base], %[accum1] \n\t"
  54. "mul %[temp3], %[sum], %[x_sub] \n\t"
  55. "subu %[loop_c], %[loop_c], %[x_stride] \n\t"
  56. "mult %[temp1], %[temp2] \n\t"
  57. "maddu %[frac], %[fx_scale] \n\t"
  58. "mfhi %[sum] \n\t"
  59. "subu %[temp3], %[temp3], %[frac] \n\t"
  60. "sw %[temp3], 0(%[frow]) \n\t"
  61. "addu %[frow], %[frow], %[x_stride1] \n\t"
  62. "bgtz %[loop_c], 1b \n\t"
  63. : [accum]"=&r"(accum), [src1]"+r"(src1), [temp3]"=&r"(temp3),
  64. [sum]"=&r"(sum), [base]"=&r"(base), [frac]"=&r"(frac),
  65. [frow]"+r"(frow), [accum1]"=&r"(accum1),
  66. [temp2]"=&r"(temp2), [temp1]"=&r"(temp1)
  67. : [x_stride]"r"(x_stride), [fx_scale]"r"(fx_scale),
  68. [x_sub]"r"(x_sub), [x_add]"r"(x_add),
  69. [loop_c]"r"(loop_c), [x_stride1]"r"(x_stride1)
  70. : "memory", "hi", "lo"
  71. );
  72. assert(accum == 0);
  73. }
  74. }
  75. static void ImportRowExpand(WebPRescaler* const wrk, const uint8_t* src) {
  76. const int x_stride = wrk->num_channels;
  77. const int x_out_max = wrk->dst_width * wrk->num_channels;
  78. const int x_add = wrk->x_add;
  79. const int x_sub = wrk->x_sub;
  80. const int src_width = wrk->src_width;
  81. const int x_stride1 = x_stride << 2;
  82. int channel;
  83. assert(wrk->x_expand);
  84. assert(!WebPRescalerInputDone(wrk));
  85. for (channel = 0; channel < x_stride; ++channel) {
  86. const uint8_t* src1 = src + channel;
  87. rescaler_t* frow = wrk->frow + channel;
  88. int temp1, temp2, temp3, temp4;
  89. int frac;
  90. int accum;
  91. int x_out = channel;
  92. __asm__ volatile (
  93. "addiu %[temp3], %[src_width], -1 \n\t"
  94. "lbu %[temp2], 0(%[src1]) \n\t"
  95. "addu %[src1], %[src1], %[x_stride] \n\t"
  96. "bgtz %[temp3], 0f \n\t"
  97. "addiu %[temp1], %[temp2], 0 \n\t"
  98. "b 3f \n\t"
  99. "0: \n\t"
  100. "lbu %[temp1], 0(%[src1]) \n\t"
  101. "3: \n\t"
  102. "addiu %[accum], %[x_add], 0 \n\t"
  103. "1: \n\t"
  104. "subu %[temp3], %[temp2], %[temp1] \n\t"
  105. "mul %[temp3], %[temp3], %[accum] \n\t"
  106. "mul %[temp4], %[temp1], %[x_add] \n\t"
  107. "addu %[temp3], %[temp4], %[temp3] \n\t"
  108. "sw %[temp3], 0(%[frow]) \n\t"
  109. "addu %[frow], %[frow], %[x_stride1] \n\t"
  110. "addu %[x_out], %[x_out], %[x_stride] \n\t"
  111. "subu %[temp3], %[x_out], %[x_out_max] \n\t"
  112. "bgez %[temp3], 2f \n\t"
  113. "subu %[accum], %[accum], %[x_sub] \n\t"
  114. "bgez %[accum], 4f \n\t"
  115. "addiu %[temp2], %[temp1], 0 \n\t"
  116. "addu %[src1], %[src1], %[x_stride] \n\t"
  117. "lbu %[temp1], 0(%[src1]) \n\t"
  118. "addu %[accum], %[accum], %[x_add] \n\t"
  119. "4: \n\t"
  120. "b 1b \n\t"
  121. "2: \n\t"
  122. : [src1]"+r"(src1), [accum]"=&r"(accum), [temp1]"=&r"(temp1),
  123. [temp2]"=&r"(temp2), [temp3]"=&r"(temp3), [temp4]"=&r"(temp4),
  124. [x_out]"+r"(x_out), [frac]"=&r"(frac), [frow]"+r"(frow)
  125. : [x_stride]"r"(x_stride), [x_add]"r"(x_add), [x_sub]"r"(x_sub),
  126. [x_stride1]"r"(x_stride1), [src_width]"r"(src_width),
  127. [x_out_max]"r"(x_out_max)
  128. : "memory", "hi", "lo"
  129. );
  130. assert(wrk->x_sub == 0 /* <- special case for src_width=1 */ || accum == 0);
  131. }
  132. }
  133. //------------------------------------------------------------------------------
  134. // Row export
  135. static void ExportRowExpand(WebPRescaler* const wrk) {
  136. uint8_t* dst = wrk->dst;
  137. rescaler_t* irow = wrk->irow;
  138. const int x_out_max = wrk->dst_width * wrk->num_channels;
  139. const rescaler_t* frow = wrk->frow;
  140. int temp0, temp1, temp3, temp4, temp5, loop_end;
  141. const int temp2 = (int)wrk->fy_scale;
  142. const int temp6 = x_out_max << 2;
  143. assert(!WebPRescalerOutputDone(wrk));
  144. assert(wrk->y_accum <= 0);
  145. assert(wrk->y_expand);
  146. assert(wrk->y_sub != 0);
  147. if (wrk->y_accum == 0) {
  148. __asm__ volatile (
  149. "li %[temp3], 0x10000 \n\t"
  150. "li %[temp4], 0x8000 \n\t"
  151. "addu %[loop_end], %[frow], %[temp6] \n\t"
  152. "1: \n\t"
  153. "lw %[temp0], 0(%[frow]) \n\t"
  154. "addiu %[dst], %[dst], 1 \n\t"
  155. "addiu %[frow], %[frow], 4 \n\t"
  156. "mult %[temp3], %[temp4] \n\t"
  157. "maddu %[temp0], %[temp2] \n\t"
  158. "mfhi %[temp5] \n\t"
  159. "sb %[temp5], -1(%[dst]) \n\t"
  160. "bne %[frow], %[loop_end], 1b \n\t"
  161. : [temp0]"=&r"(temp0), [temp1]"=&r"(temp1), [temp3]"=&r"(temp3),
  162. [temp4]"=&r"(temp4), [temp5]"=&r"(temp5), [frow]"+r"(frow),
  163. [dst]"+r"(dst), [loop_end]"=&r"(loop_end)
  164. : [temp2]"r"(temp2), [temp6]"r"(temp6)
  165. : "memory", "hi", "lo"
  166. );
  167. } else {
  168. const uint32_t B = WEBP_RESCALER_FRAC(-wrk->y_accum, wrk->y_sub);
  169. const uint32_t A = (uint32_t)(WEBP_RESCALER_ONE - B);
  170. __asm__ volatile (
  171. "li %[temp3], 0x10000 \n\t"
  172. "li %[temp4], 0x8000 \n\t"
  173. "addu %[loop_end], %[frow], %[temp6] \n\t"
  174. "1: \n\t"
  175. "lw %[temp0], 0(%[frow]) \n\t"
  176. "lw %[temp1], 0(%[irow]) \n\t"
  177. "addiu %[dst], %[dst], 1 \n\t"
  178. "mult %[temp3], %[temp4] \n\t"
  179. "maddu %[A], %[temp0] \n\t"
  180. "maddu %[B], %[temp1] \n\t"
  181. "addiu %[frow], %[frow], 4 \n\t"
  182. "addiu %[irow], %[irow], 4 \n\t"
  183. "mfhi %[temp5] \n\t"
  184. "mult %[temp3], %[temp4] \n\t"
  185. "maddu %[temp5], %[temp2] \n\t"
  186. "mfhi %[temp5] \n\t"
  187. "sb %[temp5], -1(%[dst]) \n\t"
  188. "bne %[frow], %[loop_end], 1b \n\t"
  189. : [temp0]"=&r"(temp0), [temp1]"=&r"(temp1), [temp3]"=&r"(temp3),
  190. [temp4]"=&r"(temp4), [temp5]"=&r"(temp5), [frow]"+r"(frow),
  191. [irow]"+r"(irow), [dst]"+r"(dst), [loop_end]"=&r"(loop_end)
  192. : [temp2]"r"(temp2), [temp6]"r"(temp6), [A]"r"(A), [B]"r"(B)
  193. : "memory", "hi", "lo"
  194. );
  195. }
  196. }
  197. static void ExportRowShrink(WebPRescaler* const wrk) {
  198. const int x_out_max = wrk->dst_width * wrk->num_channels;
  199. uint8_t* dst = wrk->dst;
  200. rescaler_t* irow = wrk->irow;
  201. const rescaler_t* frow = wrk->frow;
  202. const int yscale = wrk->fy_scale * (-wrk->y_accum);
  203. int temp0, temp1, temp3, temp4, temp5, loop_end;
  204. const int temp2 = (int)wrk->fxy_scale;
  205. const int temp6 = x_out_max << 2;
  206. assert(!WebPRescalerOutputDone(wrk));
  207. assert(wrk->y_accum <= 0);
  208. assert(!wrk->y_expand);
  209. assert(wrk->fxy_scale != 0);
  210. if (yscale) {
  211. __asm__ volatile (
  212. "li %[temp3], 0x10000 \n\t"
  213. "li %[temp4], 0x8000 \n\t"
  214. "addu %[loop_end], %[frow], %[temp6] \n\t"
  215. "1: \n\t"
  216. "lw %[temp0], 0(%[frow]) \n\t"
  217. "mult %[temp3], %[temp4] \n\t"
  218. "addiu %[frow], %[frow], 4 \n\t"
  219. "maddu %[temp0], %[yscale] \n\t"
  220. "mfhi %[temp1] \n\t"
  221. "lw %[temp0], 0(%[irow]) \n\t"
  222. "addiu %[dst], %[dst], 1 \n\t"
  223. "addiu %[irow], %[irow], 4 \n\t"
  224. "subu %[temp0], %[temp0], %[temp1] \n\t"
  225. "mult %[temp3], %[temp4] \n\t"
  226. "maddu %[temp0], %[temp2] \n\t"
  227. "mfhi %[temp5] \n\t"
  228. "sw %[temp1], -4(%[irow]) \n\t"
  229. "sb %[temp5], -1(%[dst]) \n\t"
  230. "bne %[frow], %[loop_end], 1b \n\t"
  231. : [temp0]"=&r"(temp0), [temp1]"=&r"(temp1), [temp3]"=&r"(temp3),
  232. [temp4]"=&r"(temp4), [temp5]"=&r"(temp5), [frow]"+r"(frow),
  233. [irow]"+r"(irow), [dst]"+r"(dst), [loop_end]"=&r"(loop_end)
  234. : [temp2]"r"(temp2), [yscale]"r"(yscale), [temp6]"r"(temp6)
  235. : "memory", "hi", "lo"
  236. );
  237. } else {
  238. __asm__ volatile (
  239. "li %[temp3], 0x10000 \n\t"
  240. "li %[temp4], 0x8000 \n\t"
  241. "addu %[loop_end], %[irow], %[temp6] \n\t"
  242. "1: \n\t"
  243. "lw %[temp0], 0(%[irow]) \n\t"
  244. "addiu %[dst], %[dst], 1 \n\t"
  245. "addiu %[irow], %[irow], 4 \n\t"
  246. "mult %[temp3], %[temp4] \n\t"
  247. "maddu %[temp0], %[temp2] \n\t"
  248. "mfhi %[temp5] \n\t"
  249. "sw $zero, -4(%[irow]) \n\t"
  250. "sb %[temp5], -1(%[dst]) \n\t"
  251. "bne %[irow], %[loop_end], 1b \n\t"
  252. : [temp0]"=&r"(temp0), [temp1]"=&r"(temp1), [temp3]"=&r"(temp3),
  253. [temp4]"=&r"(temp4), [temp5]"=&r"(temp5), [irow]"+r"(irow),
  254. [dst]"+r"(dst), [loop_end]"=&r"(loop_end)
  255. : [temp2]"r"(temp2), [temp6]"r"(temp6)
  256. : "memory", "hi", "lo"
  257. );
  258. }
  259. }
  260. //------------------------------------------------------------------------------
  261. // Entry point
  262. extern void WebPRescalerDspInitMIPS32(void);
  263. WEBP_TSAN_IGNORE_FUNCTION void WebPRescalerDspInitMIPS32(void) {
  264. WebPRescalerImportRowExpand = ImportRowExpand;
  265. WebPRescalerImportRowShrink = ImportRowShrink;
  266. WebPRescalerExportRowExpand = ExportRowExpand;
  267. WebPRescalerExportRowShrink = ExportRowShrink;
  268. }
  269. #else // !WEBP_USE_MIPS32
  270. WEBP_DSP_INIT_STUB(WebPRescalerDspInitMIPS32)
  271. #endif // WEBP_USE_MIPS32