xref: /reactos/sdk/lib/crt/math/libm_sse2/log_special.c (revision 77e6348f)
1 
2 /*******************************************************************************
3 MIT License
4 -----------
5 
6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this Software and associated documentaon files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 *******************************************************************************/
26 
27 #include <fpieee.h>
28 #include <excpt.h>
29 #include <float.h>
30 #include <math.h>
31 #include <errno.h>
32 
33 #include "libm_new.h"
34 
35 // y = log10f(x)
36 // y = log10(x)
37 // y = logf(x)
38 // y = log(x)
39 
40 // these codes and the ones in the related .asm files have to match
41 #define LOG_X_ZERO      1
42 #define LOG_X_NEG       2
43 #define LOG_X_NAN       3
44 
45 static float _logf_special_common(float x, float y, U32 code, unsigned int op, char *name)
46 {
47     switch(code)
48     {
49     case LOG_X_ZERO:
50         {
51             UT64 ym; ym.u64 = 0; ym.f32[0] = y;
52             _handle_errorf(name, op, ym.u64, _SING, AMD_F_DIVBYZERO, ERANGE, x, 0.0, 1);
53         }
54         break;
55 
56     case LOG_X_NEG:
57         {
58             UT64 ym; ym.u64 = 0; ym.f32[0] = y;
59             _handle_errorf(name, op, ym.u64, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1);
60         }
61         break;
62 
63     case LOG_X_NAN:
64         {
65             unsigned int is_snan;
66             UT32 xm; UT64 ym;
67             xm.f32 = x;
68             is_snan = (((xm.u32 & QNAN_MASK_32) == QNAN_SET_32) ? 0 : 1);
69             ym.u64 = 0; ym.f32[0] = y;
70 
71             if(is_snan)
72             {
73                 _handle_errorf(name, op, ym.u64, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1);
74             }
75             else
76             {
77                 _handle_errorf(name, op, ym.u64, _DOMAIN, 0, EDOM, x, 0.0, 1);
78             }
79         }
80         break;
81     }
82 
83     return y;
84 }
85 
86 float _logf_special(float x, float y, U32 code)
87 {
88     return _logf_special_common(x, y, code, _FpCodeLog, "logf");
89 }
90 
91 float _log10f_special(float x, float y, U32 code)
92 {
93     return _logf_special_common(x, y, code, _FpCodeLog10, "log10f");
94 }
95 
96 static double _log_special_common(double x, double y, U32 code, unsigned int op, char *name)
97 {
98     switch(code)
99     {
100     case LOG_X_ZERO:
101         {
102             UT64 ym; ym.f64 = y;
103             _handle_error(name, op, ym.u64, _SING, AMD_F_DIVBYZERO, ERANGE, x, 0.0, 1);
104         }
105         break;
106 
107     case LOG_X_NEG:
108         {
109             UT64 ym; ym.f64 = y;
110             _handle_error(name, op, ym.u64, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1);
111         }
112         break;
113 
114     case LOG_X_NAN:
115         {
116             UT64 ym; ym.f64 = y;
117             _handle_error(name, op, ym.u64, _DOMAIN, 0, EDOM, x, 0.0, 1);
118         }
119         break;
120     }
121 
122     return y;
123 }
124 
125 double _log_special(double x, double y, U32 code)
126 {
127     return _log_special_common(x, y, code, _FpCodeLog, "log");
128 }
129 
130 double _log10_special(double x, double y, U32 code)
131 {
132     return _log_special_common(x, y, code, _FpCodeLog10, "log10");
133 }
134