Daily bump.
[official-gcc.git] / libhsail-rt / rt / multimedia.c
blobbd6927a2aef9111f42b05275da4c904c33e22296
1 /* multimedia.c -- Builtins for HSAIL multimedia instructions.
3 Copyright (C) 2015-2017 Free Software Foundation, Inc.
4 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
5 for General Processor Tech.
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:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
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.
27 #include <math.h>
28 #include <stdint.h>
30 uint32_t
31 __hsail_bitalign (uint64_t lower, uint64_t upper, uint32_t shift_amount)
33 shift_amount = shift_amount & 31;
34 uint64_t packed_value = (upper << 32) | lower;
35 return (packed_value >> shift_amount) & 0xFFFFFFFF;
38 uint32_t
39 __hsail_bytealign (uint64_t lower, uint64_t upper, uint32_t shift_amount)
41 shift_amount = (shift_amount & 3) * 8;
42 uint64_t packed_value = (upper << 32) | lower;
43 return (packed_value >> shift_amount) & 0xFFFFFFFF;
46 uint32_t
47 __hsail_lerp (uint32_t a, uint32_t b, uint32_t c)
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;
62 return e3 | e2 | e1 | e0;
65 static uint8_t
66 cvt_neari_sat_u8_f32 (float a)
68 if (isinf (a))
70 if (signbit (a)) return 0;
71 else return 255;
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;
82 uint32_t
83 __hsail_packcvt (float a, float b, float c, float d)
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;
91 float
92 __hsail_unpackcvt (uint32_t val, uint32_t index)
94 return (float) ((val >> (index * 8)) & 0xff);
97 static uint32_t
98 abs_diff (uint32_t a, uint32_t b)
100 if (a < b)
101 return b - a;
102 else
103 return a - b;
106 uint32_t
107 __hsail_sad_u8x4 (uint32_t a, uint32_t b, uint32_t add)
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;
115 uint32_t
116 __hsail_sad_u16x2 (uint32_t a, uint32_t b, uint32_t add)
118 return abs_diff ((a >> 16) & 0xffff, (b >> 16) & 0xffff)
119 + abs_diff ((a >> 0) & 0xffff, (b >> 0) & 0xffff) + add;
122 uint32_t
123 __hsail_sad_u32 (uint32_t a, uint32_t b, uint32_t add)
125 return abs_diff (a, b) + add;
128 uint32_t
129 __hsail_sadhi_u16x2_u8x4 (uint32_t a, uint32_t b, uint32_t add)
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;