1 /*
2 * Copyright 2008-2013 NVIDIA Corporation
3 * Copyright 2013 Filipe RNC Maia
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*-
19 * Copyright (c) 2011 David Schultz <das@FreeBSD.ORG>
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 */
43
44 /* adapted from FreeBSD:
45 * lib/msun/src/s_cexpf.c
46 * lib/msun/src/k_exp.c
47 *
48 */
49
50 #pragma once
51
52 #include <thrust/complex.h>
53 #include <thrust/detail/complex/math_private.h>
54
55 namespace thrust{
56 namespace detail{
57 namespace complex{
58
59 __host__ __device__ inline
frexp_expf(float x,int * expt)60 float frexp_expf(float x, int *expt){
61 const uint32_t k = 235; /* constant for reduction */
62 const float kln2 = 162.88958740F; /* k * ln2 */
63
64 // should this be a double instead?
65 float exp_x;
66 uint32_t hx;
67
68 exp_x = expf(x - kln2);
69 get_float_word(hx, exp_x);
70 *expt = (hx >> 23) - (0x7f + 127) + k;
71 set_float_word(exp_x, (hx & 0x7fffff) | ((0x7f + 127) << 23));
72 return (exp_x);
73 }
74
75 __host__ __device__ inline
76 complex<float>
ldexp_cexpf(complex<float> z,int expt)77 ldexp_cexpf(complex<float> z, int expt)
78 {
79 float x, y, exp_x, scale1, scale2;
80 int ex_expt, half_expt;
81
82 x = z.real();
83 y = z.imag();
84 exp_x = frexp_expf(x, &ex_expt);
85 expt += ex_expt;
86
87 half_expt = expt / 2;
88 set_float_word(scale1, (0x7f + half_expt) << 23);
89 half_expt = expt - half_expt;
90 set_float_word(scale2, (0x7f + half_expt) << 23);
91
92 return (complex<float>(std::cos(y) * exp_x * scale1 * scale2,
93 std::sin(y) * exp_x * scale1 * scale2));
94 }
95
96 __host__ __device__ inline
cexpf(const complex<float> & z)97 complex<float> cexpf(const complex<float>& z){
98 float x, y, exp_x;
99 uint32_t hx, hy;
100
101 const uint32_t
102 exp_ovfl = 0x42b17218, /* MAX_EXP * ln2 ~= 88.722839355 */
103 cexp_ovfl = 0x43400074; /* (MAX_EXP - MIN_DENORM_EXP) * ln2 */
104
105 x = z.real();
106 y = z.imag();
107
108 get_float_word(hy, y);
109 hy &= 0x7fffffff;
110
111 /* cexp(x + I 0) = exp(x) + I 0 */
112 if (hy == 0)
113 return (complex<float>(std::exp(x), y));
114 get_float_word(hx, x);
115 /* cexp(0 + I y) = cos(y) + I sin(y) */
116 if ((hx & 0x7fffffff) == 0){
117 return (complex<float>(std::cos(y), std::sin(y)));
118 }
119 if (hy >= 0x7f800000) {
120 if ((hx & 0x7fffffff) != 0x7f800000) {
121 /* cexp(finite|NaN +- I Inf|NaN) = NaN + I NaN */
122 return (complex<float>(y - y, y - y));
123 } else if (hx & 0x80000000) {
124 /* cexp(-Inf +- I Inf|NaN) = 0 + I 0 */
125 return (complex<float>(0.0, 0.0));
126 } else {
127 /* cexp(+Inf +- I Inf|NaN) = Inf + I NaN */
128 return (complex<float>(x, y - y));
129 }
130 }
131
132 if (hx >= exp_ovfl && hx <= cexp_ovfl) {
133 /*
134 * x is between 88.7 and 192, so we must scale to avoid
135 * overflow in expf(x).
136 */
137 return (ldexp_cexpf(z, 0));
138 } else {
139 /*
140 * Cases covered here:
141 * - x < exp_ovfl and exp(x) won't overflow (common case)
142 * - x > cexp_ovfl, so exp(x) * s overflows for all s > 0
143 * - x = +-Inf (generated by exp())
144 * - x = NaN (spurious inexact exception from y)
145 */
146 exp_x = std::exp(x);
147 return (complex<float>(exp_x * std::cos(y), exp_x * std::sin(y)));
148 }
149 }
150
151 } // namespace complex
152
153 } // namespace detail
154
155 template <>
156 __host__ __device__
exp(const complex<float> & z)157 inline complex<float> exp(const complex<float>& z){
158 return detail::complex::cexpf(z);
159 }
160
161 } // namespace thrust
162