summaryrefslogtreecommitdiffstats
path: root/src/libs/softfloat-3e/source/extF80_sincos.c
blob: 6836ad824d857f29d2cb2950c735defb0e195fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* $Id: extF80_sincos.c $ */
/** @file
 * SoftFloat - VBox Extension - extF80_sin, extF80_cos, extF80_sincos, extF80_atan2.
 */

/*
 * Copyright (C) 2022-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <stdbool.h>
#include <stdint.h>
#include "platform.h"
#include "internals.h"
#include "specialize.h"
#include "softfloat.h"
#include <iprt/types.h>
#include <iprt/x86.h>

#include "extF80_sincos.h"


static void cordic_sincos( float128_t z, float128_t *pv1, float128_t *pv2 SOFTFLOAT_STATE_DECL_COMMA )
{
    float128_t v1 = { { 0, 0 } }; /* MSC thinks it can be used uninitialized */
    float128_t v2 = { { 0, 0 } }; /* MSC thinks it can be used uninitialized */
    /** @todo TBD: CORDIC kernel should be easily implemented in assembly *   */

    float128_t x1 = ui32_to_f128(1, pState);
    float128_t x2 = ui32_to_f128(0, pState);
    float128_t zz = ui32_to_f128(0, pState);

    float128_t p2m = ui32_to_f128(1, pState);
    float128_t two = ui32_to_f128(2, pState);

    for (unsigned k = 0; k < RT_ELEMENTS(g_ar128FsincosCORDICConsts); k++)
    {
        float128_t atg   = *(float128_t *)&g_ar128FsincosCORDICConsts[k];
        float128_t scale = *(float128_t *)&g_ar128FsincosCORDICConsts2[k];

        float128_t px1 = f128_mul(x1, p2m, pState);
        float128_t px2 = f128_mul(x2, p2m, pState);

        if (f128_le(zz, z, pState))
        {
            x1 = f128_sub(x1, px2, pState);
            x2 = f128_add(x2, px1, pState);
            zz = f128_add(zz, atg, pState);
        }
        else
        {
            x1 = f128_add(x1, px2, pState);
            x2 = f128_sub(x2, px1, pState);
            zz = f128_sub(zz, atg, pState);
        }

        p2m = f128_div(p2m, two, pState);

        v1 = f128_mul(x1, scale, pState);
        v2 = f128_mul(x2, scale, pState);
    }

    *pv1 = v1;
    *pv2 = v2;
}

static float128_t cordic_atan2( float128_t y, float128_t x SOFTFLOAT_STATE_DECL_COMMA )
{
    float128_t v1 = { { 0, 0 } }; /* MSC thinks it can be used uninitialized */
    float128_t v2 = { { 0, 0 } }; /* MSC thinks it can be used uninitialized */
    /** @todo TBD: CORDIC kernel should be easily implemented in assembly *   */

    float128_t x1 = x, x2 = y;
    float128_t z = ui32_to_f128(0, pState);
    float128_t zero = ui32_to_f128(0, pState);
    float128_t p2m = ui32_to_f128(1, pState);
    float128_t two = ui32_to_f128(2, pState);

    for (unsigned k = 0; k < RT_ELEMENTS(g_ar128FsincosCORDICConsts); k++)
    {
        float128_t atg   = *(float128_t *)&g_ar128FsincosCORDICConsts[k];
        float128_t scale = *(float128_t *)&g_ar128FsincosCORDICConsts2[k];

        float128_t px1 = f128_mul(x1, p2m, pState);
        float128_t px2 = f128_mul(x2, p2m, pState);

        if (f128_le(x2, zero, pState))
        {
            x1 = f128_sub(x1, px2, pState);
            x2 = f128_add(x2, px1, pState);
            z = f128_sub(z, atg, pState);
        }
        else
        {
            x1 = f128_add(x1, px2, pState);
            x2 = f128_sub(x2, px1, pState);
            z = f128_add(z, atg, pState);
        }

        p2m = f128_div(p2m, two, pState);

        v1 = f128_mul(x1, scale, pState);
        v2 = f128_mul(x2, scale, pState);
    }

    return z;
}

extFloat80_t extF80_sin( extFloat80_t x SOFTFLOAT_STATE_DECL_COMMA )
{
    int32_t fSign = 0;
    extFloat80_t f80zero = ui32_to_extF80(0, pState);
    if (extF80_le(x, f80zero, pState))
    {
        x = extF80_sub(f80zero, x, pState);
        fSign = 1;
    }

    extFloat80_t f80pi2 = f128_to_extF80(*(float128_t *)&g_r128pi2, pState);

    /** @todo TBD: Partial remainder should be calculated using float128 value of pi2 to increase precision **/
    uint16_t fCxFlags = 0;
    extFloat80_t rem = extF80_partialRem(x, f80pi2, pState->roundingMode, &fCxFlags, pState);
    int32_t const quo = X86_FSW_CX_TO_QUOTIENT(fCxFlags);

    float128_t z = extF80_to_f128(rem, pState);
    float128_t f128zero = ui32_to_f128(0, pState);

    float128_t v1, v2;
    cordic_sincos(z, &v1, &v2, pState);

    float128_t v;
    switch(quo % 4)
    {
#ifdef _MSC_VER /* stupid MSC thinks v might be used uninitialized otherwise: */
        default:
#endif
        case 0:
            v = v2;
            break;

        case 1:
            v = v1;
            break;

        case 2:
            v = f128_sub(f128zero, v2, pState);
            break;

        case 3:
            v = f128_sub(f128zero, v1, pState);
            break;
    }

    if (fSign)
        v = f128_sub(f128zero, v, pState);

    return f128_to_extF80(v, pState);
}

extFloat80_t extF80_cos( extFloat80_t x SOFTFLOAT_STATE_DECL_COMMA )
{
    extFloat80_t f80zero = ui32_to_extF80(0, pState);
    if (extF80_le(x, f80zero, pState))
        x = extF80_sub(f80zero, x, pState);

    extFloat80_t f80pi2 = f128_to_extF80(*(float128_t *)&g_r128pi2, pState);

    /** TBD: Partial remainder should be calculated using float128 value of pi2 to increase precision **/
    uint16_t fCxFlags = 0;
    extFloat80_t rem = extF80_partialRem(x, f80pi2, pState->roundingMode, &fCxFlags, pState);
    int32_t const quo = X86_FSW_CX_TO_QUOTIENT(fCxFlags);

    float128_t z = extF80_to_f128(rem, pState);
    float128_t f128zero = ui32_to_f128(0, pState);

    float128_t v1, v2;
    cordic_sincos(z, &v1, &v2, pState);

    float128_t v;
    switch(quo % 4)
    {
#ifdef _MSC_VER /* stupid MSC thinks v might be used uninitialized otherwise: */
        default:
#endif
        case 0:
            v = v1;
            break;

        case 1:
            v = f128_sub(f128zero, v2, pState);;
            break;

        case 2:
            v = f128_sub(f128zero, v1, pState);
            break;

        case 3:
            v = v2;
            break;
    }

    return f128_to_extF80(v, pState);
}

void extF80_sincos( extFloat80_t x, extFloat80_t* pSin, extFloat80_t* pCos SOFTFLOAT_STATE_DECL_COMMA )
{
    uint16_t fCxFlags = 0;
    int32_t quo;
    extFloat80_t rem, f80pi2, f80zero;
    int32_t fSign = 0;

    f80zero = ui32_to_extF80(0, pState);
    if (extF80_le(x, f80zero, pState))
    {
        x = extF80_sub(f80zero, x, pState);
        fSign = 1;
    }

    f80pi2 = f128_to_extF80(*(float128_t const *)&g_r128pi2, pState);

    /** @todo TBD: Partial remainder should be calculated using float128 value of pi2 to increase precision **/
    rem = extF80_partialRem(x, f80pi2, pState->roundingMode, &fCxFlags, pState);
    quo = X86_FSW_CX_TO_QUOTIENT(fCxFlags);

    float128_t z = extF80_to_f128(rem, pState);
    float128_t f128zero = ui32_to_f128(0, pState);

    float128_t v1, v2;
    cordic_sincos(z, &v1, &v2, pState);

    float128_t vCos, vSin;
    switch(quo % 4)
    {
#ifdef _MSC_VER /* stupid MSC thinks vCos & cSin might be used uninitialized otherwise: */
        default:
#endif
        case 0:
            vCos = v1;
            vSin = v2;
            break;

        case 1:
            vCos = f128_sub(f128zero, v2, pState);
            vSin = v1;
            break;

        case 2:
            vCos = f128_sub(f128zero, v1, pState);
            vSin = f128_sub(f128zero, v2, pState);
            break;

        case 3:
            vCos = v2;
            vSin = f128_sub(f128zero, v1, pState);
            break;
    }

    if (fSign)
        vSin = f128_sub(f128zero, vSin, pState);

    *pCos = f128_to_extF80(vCos, pState);
    *pSin = f128_to_extF80(vSin, pState);
}

extFloat80_t extF80_atan2( extFloat80_t f80y, extFloat80_t f80x SOFTFLOAT_STATE_DECL_COMMA )
{
    float128_t v;
    int32_t fSignX = 0, fSignY = 0;
    float128_t f128zero = ui32_to_f128(0, pState);
    float128_t y = extF80_to_f128(f80y, pState);
    float128_t x = extF80_to_f128(f80x, pState);

    if (f128_le(x, f128zero, pState))
    {
        x = f128_sub(f128zero, x, pState);
        fSignX = 1;
    }

    if (f128_le(y, f128zero, pState))
    {
        y = f128_sub(f128zero, y, pState);
        fSignY = 1;
    }

    v = cordic_atan2(y, x, pState);

    if (fSignX)
    {
        if (fSignY)
            v = f128_sub(v, *(float128_t const *)&g_r128pi, pState);
        else
            v = f128_sub(*(float128_t const *)&g_r128pi, v, pState);
    }
    else
    {
        if (fSignY)
            v = f128_sub(f128zero, v, pState);
    }

    return f128_to_extF80(v, pState);
}