amath  1.8.5
Simple command line calculator
prim.h
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #ifndef AMATH_LIB_REAL_PRIM_H
31 #define AMATH_LIB_REAL_PRIM_H
32 
33 /**
34  * @file real/prim.h
35  * @brief Primitives in math library for handling real numbers
36  * @details
37  * The library is based on fdlib by Sun Microsystems.
38  * The original library can be downloaded at:
39  * http://www.netlib.org/fdlibm/
40  *
41  * or from mirror site:
42  * http://www.hensa.ac.uk/
43  */
44 
45 #include "../amath.h"
46 #include "../mathr.h"
47 
48 #define TRIG_INEXACT(x) ((x >= 0.0 && x < 1e-15) || (x <= 0.0 && x > -1e-15))
49 
50 double __kernel_cos(double x, double y);
51 double __kernel_sin(double x, double y, int iy);
52 double __kernel_tan(double x, double y, int iy);
53 int __kernel_rem_pio2(double* x, double* y, int e0, int nx, int prec, const int* ipio2);
54 
55 /* Detect ARM CPUs */
56 #if !defined(__IEEE_LITTLE_ENDIAN) && !defined(__IEEE_BIG_ENDIAN)
57 #if defined(__arm__) || defined(__ARM_ARCH_2__) ||
58  defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__) ||
59  defined(__ARM_ARCH_4T__) || defined(__TARGET_ARM_4T) ||
60  defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5E__) ||
61  defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) ||
62  defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) ||
63  defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) ||
64  defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ||
65  defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) ||
66  defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) ||
67  defined(__ARM_ARCH_7S_) || defined(__ARM_ARCH_5TEJ__) ||
68  defined(_M_ARM) || defined(ARMCPU) ||
69  defined(__aarch64__)
70 #define __IEEE_LITTLE_ENDIAN
71 #endif
72 #endif
73 
74 /* Detect Motorola CPUs */
75 #if !defined(__IEEE_LITTLE_ENDIAN) && !defined(__IEEE_BIG_ENDIAN)
76 #if defined(mc68000) || defined(__mc68000) || defined(_M68000) ||
77  defined(mc68020) || defined(__mc68020) || defined(_M68020) ||
78  defined(mc68030) || defined(__mc68030) || defined(_M68030) ||
79  defined(mc68040) || defined(__mc68040) || defined(_M68040) ||
80  defined(mc68060) || defined(__mc68060) || defined(_M68060) ||
81  defined(__m68k__)
82 #define __IEEE_BIG_ENDIAN
83 #endif
84 #endif
85 
86 /* Detect Intel CPUs */
87 #if !defined(__IEEE_LITTLE_ENDIAN) && !defined(__IEEE_BIG_ENDIAN)
88 #if defined(i386) || defined(__i386) || defined(__i386__) ||
89  defined(i386) || defined(__i486) || defined(__i486__) ||
90  defined(i386) || defined(__i586) || defined(__i586__) ||
91  defined(i386) || defined(__i686) || defined(__i686__) ||
92  defined(x86) || defined(intel) || defined(i86pc) ||
93  defined(_M_IX86) || defined(_M_AMD64) || defined(__x86_64__) ||
94  defined(INTELCPU)
95 #define __IEEE_LITTLE_ENDIAN
96 #endif
97 #endif
98 
99 /* Detect other LE CPUs */
100 #if !defined(__IEEE_LITTLE_ENDIAN) && !defined(__IEEE_BIG_ENDIAN)
101 #if defined(__alpha) || defined(__osf__)
102 #define __IEEE_LITTLE_ENDIAN
103 #endif
104 #endif
105 
106 /* Detect other BE CPUs */
107 #if !defined(__IEEE_LITTLE_ENDIAN) && !defined(__IEEE_BIG_ENDIAN)
108 #if defined(PPCCPU) || defined(__PPC__) ||
109  defined(__powerpc__) || defined(__powerpc64__)
110  defined(__mips__) || defined(__mips)
111 #define __IEEE_BIG_ENDIAN
112 #endif
113 #endif
114 
115 #ifndef __IEEE_BIG_ENDIAN
116 #ifndef __IEEE_LITTLE_ENDIAN
117 #error Must define endianness
118 #endif
119 #endif
120 
121 #ifdef __IEEE_BIG_ENDIAN
122 /**
123  * @brief A union which permits us to convert between a double and two 32 bit ints
124  * @details Big endian version
125  */
126 typedef union
127 {
128  double value;
129  struct
130  {
131  uint32_t msw;
132  uint32_t lsw;
133  } parts;
134 } ieee_double_shape_type;
135 #endif
136 
137 #ifdef __IEEE_LITTLE_ENDIAN
138 /**
139  * @brief A union which permits us to convert between a double and two 32 bit ints
140  * @details Little endian version
141  */
142 typedef union
143 {
144  double value;
145  struct
146  {
147  uint32_t lsw;
148  uint32_t msw;
149  } parts;
150 } ieee_double_shape_type;
151 #endif
152 
153 /**
154  * @brief Get two 32 bit ints from a double
155  */
156 #define EXTRACT_WORDS(ix0,ix1,d)
157  do {
158  ieee_double_shape_type ew_u;
159  ew_u.value = (d);
160  (ix0) = ew_u.parts.msw;
161  (ix1) = ew_u.parts.lsw;
162  } while (0)
163 
164 /**
165  * @brief Get the more significant 32 bit int from a double
166  */
167 #define GET_HIGH_WORD(i,d)
168  do {
169  ieee_double_shape_type gh_u;
170  gh_u.value = (d);
171  (i) = gh_u.parts.msw;
172  } while (0)
173 
174 /**
175  * @brief Get the less significant 32 bit int from a double
176  */
177 #define GET_LOW_WORD(i,d)
178  do {
179  ieee_double_shape_type gl_u;
180  gl_u.value = (d);
181  (i) = gl_u.parts.lsw;
182  } while (0)
183 
184 /**
185  * @brief Set a double from two 32 bit ints
186  */
187 #define INSERT_WORDS(d,ix0,ix1)
188  do {
189  ieee_double_shape_type iw_u;
190  iw_u.parts.msw = (ix0);
191  iw_u.parts.lsw = (ix1);
192  (d) = iw_u.value;
193  } while (0)
194 
195 /**
196  * @brief Set the more significant 32 bits of a double from an int
197  */
198 #define SET_HIGH_WORD(d,v)
199  do {
200  ieee_double_shape_type sh_u;
201  sh_u.value = (d);
202  sh_u.parts.msw = (v);
203  (d) = sh_u.value;
204  } while (0)
205 
206 /**
207  * @brief Set the less significant 32 bits of a double from an int
208  */
209 #define SET_LOW_WORD(d,v)
210  do {
211  ieee_double_shape_type sl_u;
212  sl_u.value = (d);
213  sl_u.parts.lsw = (v);
214  (d) = sl_u.value;
215  } while (0)
216 
217 #endif
double __kernel_cos(double x, double y)
Kernel cosine function.
Definition: kcos.c:94
double __kernel_sin(double x, double y, int iy)
Kernel sin function.
Definition: ksin.c:89
double __kernel_tan(double x, double y, int iy)
Kernel tan function.
Definition: ktan.c:108
int __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec, const int *ipio2)
Kernel reduction function.
Definition: kremp2.c:184