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