HIP: Heterogenous-computing Interface for Portability
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hip_complex.h
1 /*
2 Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #ifndef HIPCOMPLEX_H
24 #define HIPCOMPLEX_H
25 
26 typedef struct{
27  float x;
28  float y;
30 
31 __device__ static inline float hipCrealf(hipFloatComplex z){
32  return z.x;
33 }
34 
35 __device__ static inline float hipCimagf(hipFloatComplex z){
36  return z.y;
37 }
38 
39 __device__ static inline hipFloatComplex make_hipFloatComplex(float a, float b){
41  z.x = a;
42  z.y = b;
43  return z;
44 }
45 
46 __device__ static inline hipFloatComplex hipConjf(hipFloatComplex z){
47  hipFloatComplex ret;
48  ret.x = z.x;
49  ret.y = -z.y;
50  return ret;
51 }
52 
53 __device__ static inline float hipCsqabsf(hipFloatComplex z){
54  return z.x * z.x + z.y * z.y;
55 }
56 
57 __device__ static inline hipFloatComplex hipCaddf(hipFloatComplex p, hipFloatComplex q){
58  return make_hipFloatComplex(p.x + q.x, p.y + q.y);
59 }
60 
61 __device__ static inline hipFloatComplex hipCsubf(hipFloatComplex p, hipFloatComplex q){
62  return make_hipFloatComplex(p.x - q.x, p.y - q.y);
63 }
64 
65 __device__ static inline hipFloatComplex hipCmulf(hipFloatComplex p, hipFloatComplex q){
66  return make_hipFloatComplex(p.x * q.x - p.y * q.y, p.y * q.x + p.x * q.y);
67 }
68 
69 __device__ static inline hipFloatComplex hipCdivf(hipFloatComplex p, hipFloatComplex q){
70  float sqabs = hipCsqabsf(q);
71  hipFloatComplex ret;
72  ret.x = (p.x * q.x + p.y * q.y)/sqabs;
73  ret.y = (p.y * q.x - p.x * q.y)/sqabs;
74  return ret;
75 }
76 
77 __device__ static inline float hipCabsf(hipFloatComplex z){
78  return sqrtf(hipCsqabsf(z));
79 }
80 
81 
82 typedef struct{
83  double x;
84  double y;
86 
87 __device__ static inline double hipCreal(hipDoubleComplex z){
88  return z.x;
89 }
90 
91 __device__ static inline double hipCimag(hipDoubleComplex z){
92  return z.y;
93 }
94 
95 __device__ static inline hipDoubleComplex make_hipDoubleComplex(double a, double b){
97  z.x = a;
98  z.y = b;
99  return z;
100 }
101 
102 __device__ static inline hipDoubleComplex hipConj(hipDoubleComplex z){
103  hipDoubleComplex ret;
104  ret.x = z.x;
105  ret.y = z.y;
106  return ret;
107 }
108 
109 __device__ static inline double hipCsqabs(hipDoubleComplex z){
110  return z.x * z.x + z.y * z.y;
111 }
112 
113 __device__ static inline hipDoubleComplex hipCadd(hipDoubleComplex p, hipDoubleComplex q){
114  return make_hipDoubleComplex(p.x + q.x, p.y + q.y);
115 }
116 
117 __device__ static inline hipDoubleComplex hipCsub(hipDoubleComplex p, hipDoubleComplex q){
118  return make_hipDoubleComplex(p.x - q.x, p.y - q.y);
119 }
120 
121 __device__ static inline hipDoubleComplex hipCmul(hipDoubleComplex p, hipDoubleComplex q){
122  return make_hipDoubleComplex(p.x * q.x - p.y * q.y, p.y * q.x + p.x * q.y);
123 }
124 
125 __device__ static inline hipDoubleComplex hipCdiv(hipDoubleComplex p, hipDoubleComplex q){
126  double sqabs = hipCsqabs(q);
127  hipDoubleComplex ret;
128  ret.x = (p.x * q.x + p.y * q.y)/sqabs;
129  ret.y = (p.y * q.x - p.x * q.y)/sqabs;
130  return ret;
131 }
132 
133 __device__ static inline double hipCabs(hipDoubleComplex z){
134  return sqrtf(hipCsqabs(z));
135 }
136 
138 
139 __device__ static inline hipComplex make_hipComplex(float x,
140  float y){
141  return make_hipFloatComplex(x, y);
142 }
143 
144 __device__ static inline hipFloatComplex hipComplexDoubleToFloat
145 (hipDoubleComplex z){
146  return make_hipFloatComplex((float)z.x, (float)z.y);
147 }
148 
149 __device__ static inline hipDoubleComplex hipComplexFloatToDouble
150 (hipFloatComplex z){
151  return make_hipDoubleComplex((double)z.x, (double)z.y);
152 }
153 
154 __device__ static inline hipComplex hipCfmaf(hipComplex p, hipComplex q, hipComplex r){
155  float real = (p.x * q.x) + r.x;
156  float imag = (q.x * p.y) + r.y;
157 
158  real = -(p.y * q.y) + real;
159  imag = (p.x * q.y) + imag;
160 
161  return make_hipComplex(real, imag);
162 }
163 
164 __device__ static inline hipDoubleComplex hipCfma(hipDoubleComplex p, hipDoubleComplex q, hipDoubleComplex r){
165  float real = (p.x * q.x) + r.x;
166  float imag = (q.x * p.y) + r.y;
167 
168  real = -(p.y * q.y) + real;
169  imag = (p.x * q.y) + imag;
170 
171  return make_hipDoubleComplex(real, imag);
172 }
173 
174 #endif
Definition: hip_complex.h:26
Definition: hip_complex.h:82