1 /* multimedia.c -- Builtins for HSAIL multimedia instructions.
2 
3    Copyright (C) 2015-2020 Free Software Foundation, Inc.
4    Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
5    for General Processor Tech.
6 
7    Permission is hereby granted, free of charge, to any person obtaining a
8    copy of this software and associated documentation files
9    (the "Software"), to deal in the Software without restriction, including
10    without limitation the rights to use, copy, modify, merge, publish,
11    distribute, sublicense, and/or sell copies of the Software, and to
12    permit persons to whom the Software is furnished to do so, subject to
13    the following conditions:
14 
15    The above copyright notice and this permission notice shall be included
16    in all copies or substantial portions of the Software.
17 
18    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24    USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26 
27 #include <math.h>
28 #include <stdint.h>
29 
30 uint32_t
__hsail_bitalign(uint64_t lower,uint64_t upper,uint32_t shift_amount)31 __hsail_bitalign (uint64_t lower, uint64_t upper, uint32_t shift_amount)
32 {
33   shift_amount = shift_amount & 31;
34   uint64_t packed_value = (upper << 32) | lower;
35   return (packed_value >> shift_amount) & 0xFFFFFFFF;
36 }
37 
38 uint32_t
__hsail_bytealign(uint64_t lower,uint64_t upper,uint32_t shift_amount)39 __hsail_bytealign (uint64_t lower, uint64_t upper, uint32_t shift_amount)
40 {
41   shift_amount = (shift_amount & 3) * 8;
42   uint64_t packed_value = (upper << 32) | lower;
43   return (packed_value >> shift_amount) & 0xFFFFFFFF;
44 }
45 
46 uint32_t
__hsail_lerp(uint32_t a,uint32_t b,uint32_t c)47 __hsail_lerp (uint32_t a, uint32_t b, uint32_t c)
48 {
49   uint32_t e3
50     = (((((a >> 24) & 0xff) + ((b >> 24) & 0xff) + ((c >> 24) & 0x1)) / 2)
51        & 0xff)
52       << 24;
53   uint32_t e2
54     = (((((a >> 16) & 0xff) + ((b >> 16) & 0xff) + ((c >> 16) & 0x1)) / 2)
55        & 0xff)
56       << 16;
57   uint32_t e1
58     = (((((a >> 8) & 0xff) + ((b >> 8) & 0xff) + ((c >> 8) & 0x1)) / 2) & 0xff)
59       << 8;
60   uint32_t e0 = (((a & 0xff) + (b & 0xff) + (c & 0x1)) / 2) & 0xff;
61 
62   return e3 | e2 | e1 | e0;
63 }
64 
65 static uint8_t
cvt_neari_sat_u8_f32(float a)66 cvt_neari_sat_u8_f32 (float a)
67 {
68   if (isinf (a))
69     {
70       if (signbit (a)) return 0;
71       else return 255;
72     }
73   else if (isnan (a)) return 0;
74   else if (a < 0.0)
75     return 0;
76   else if (a > 255.0)
77     return 255;
78   else
79     return (uint8_t) a;
80 }
81 
82 uint32_t
__hsail_packcvt(float a,float b,float c,float d)83 __hsail_packcvt (float a, float b, float c, float d)
84 {
85   return (uint32_t) cvt_neari_sat_u8_f32 (a)
86 	 | (uint32_t) cvt_neari_sat_u8_f32 (b) << 8
87 	 | (uint32_t) cvt_neari_sat_u8_f32 (c) << 16
88 	 | (uint32_t) cvt_neari_sat_u8_f32 (d) << 24;
89 }
90 
91 float
__hsail_unpackcvt(uint32_t val,uint32_t index)92 __hsail_unpackcvt (uint32_t val, uint32_t index)
93 {
94   return (float) ((val >> (index * 8)) & 0xff);
95 }
96 
97 static uint32_t
abs_diff(uint32_t a,uint32_t b)98 abs_diff (uint32_t a, uint32_t b)
99 {
100   if (a < b)
101     return b - a;
102   else
103     return a - b;
104 }
105 
106 uint32_t
__hsail_sad_u8x4(uint32_t a,uint32_t b,uint32_t add)107 __hsail_sad_u8x4 (uint32_t a, uint32_t b, uint32_t add)
108 {
109   return abs_diff ((a >> 24) & 0xff, (b >> 24) & 0xff)
110 	 + abs_diff ((a >> 16) & 0xff, (b >> 16) & 0xff)
111 	 + abs_diff ((a >> 8) & 0xff, (b >> 8) & 0xff)
112 	 + abs_diff ((a >> 0) & 0xff, (b >> 0) & 0xff) + add;
113 }
114 
115 uint32_t
__hsail_sad_u16x2(uint32_t a,uint32_t b,uint32_t add)116 __hsail_sad_u16x2 (uint32_t a, uint32_t b, uint32_t add)
117 {
118   return abs_diff ((a >> 16) & 0xffff, (b >> 16) & 0xffff)
119 	 + abs_diff ((a >> 0) & 0xffff, (b >> 0) & 0xffff) + add;
120 }
121 
122 uint32_t
__hsail_sad_u32(uint32_t a,uint32_t b,uint32_t add)123 __hsail_sad_u32 (uint32_t a, uint32_t b, uint32_t add)
124 {
125   return abs_diff (a, b) + add;
126 }
127 
128 uint32_t
__hsail_sadhi_u16x2_u8x4(uint32_t a,uint32_t b,uint32_t add)129 __hsail_sadhi_u16x2_u8x4 (uint32_t a, uint32_t b, uint32_t add)
130 {
131   return (abs_diff ((a >> 24) & 0xff, (b >> 24) & 0xff) << 16)
132 	 + (abs_diff ((a >> 16) & 0xff, (b >> 16) & 0xff) << 16)
133 	 + (abs_diff ((a >> 8) & 0xff, (b >> 8) & 0xff) << 16)
134 	 + (abs_diff ((a >> 0) & 0xff, (b >> 0) & 0xff) << 16) + add;
135 }
136