d5a003a48b28d44411ff235419b07abbd2b50a55
[mesa.git] / src / gallium / auxiliary / gallivm / llvm_builtins.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * 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 NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Zack Rusin zack@tungstengraphics.com
31 */
32 typedef __attribute__(( ext_vector_type(4) )) float float4;
33
34 extern float powf(float a, float b);
35
36 inline float approx(float a, float b)
37 {
38 if (b < -128.0f) b = -128.0f;
39 if (b > 128.0f) b = 128.0f;
40 if (a < 0) a = 0;
41 return powf(a, b);
42 }
43
44 inline float4 lit(float4 tmp)
45 {
46 float4 result;
47 result.x = 1.0;
48 result.w = 1.0;
49 if (tmp.x > 0) {
50 result.y = tmp.x;
51 result.z = approx(tmp.y, tmp.w);
52 } else {
53 result.y = 0;
54 result.z = 0;
55 }
56 return result;
57 }
58
59 inline float4 cmp(float4 tmp0, float4 tmp1, float4 tmp2)
60 {
61 float4 result;
62
63 result.x = (tmp0.x < 0.0) ? tmp1.x : tmp2.x;
64 result.y = (tmp0.y < 0.0) ? tmp1.y : tmp2.y;
65 result.z = (tmp0.z < 0.0) ? tmp1.z : tmp2.z;
66 result.w = (tmp0.w < 0.0) ? tmp1.w : tmp2.w;
67
68 return result;
69 }
70
71 extern float cosf(float val);
72 extern float sinf(float val);
73
74 inline float4 vcos(float4 val)
75 {
76 float4 result;
77 printf("VEC IN is %f %f %f %f\n", val.x, val.y, val.z, val.w);
78 result.x = cosf(val.x);
79 result.y = cosf(val.x);
80 result.z = cosf(val.x);
81 result.w = cosf(val.x);
82 printf("VEC OUT is %f %f %f %f\n", result.x, result.y, result.z, result.w);
83 return result;
84 }
85
86 inline float4 scs(float4 val)
87 {
88 float4 result;
89 float tmp = val.x;
90 result.x = cosf(tmp);
91 result.y = sinf(tmp);
92 return result;
93 }
94
95
96 inline float4 vsin(float4 val)
97 {
98 float4 result;
99 float tmp = val.x;
100 float res = sinf(tmp);
101 result.x = res;
102 result.y = res;
103 result.z = res;
104 result.w = res;
105 return result;
106 }
107
108 inline int kil(float4 val)
109 {
110 if (val.x < 0 || val.y < 0 || val.z < 0 || val.w < 0)
111 return 1;
112 else
113 return 0;
114 }