Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
f16_sqrt.c
Go to the documentation of this file.
1
2/*============================================================================
3
4This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5Package, Release 3e, by John R. Hauser.
6
7Copyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of
8California. All rights reserved.
9
10Redistribution and use in source and binary forms, with or without
11modification, are permitted provided that the following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright notice,
14 this list of conditions, and the following disclaimer.
15
16 2. Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions, and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19
20 3. Neither the name of the University nor the names of its contributors may
21 be used to endorse or promote products derived from this software without
22 specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35=============================================================================*/
36
37#include <stdbool.h>
38#include <stdint.h>
39#include "platform.h"
40#include "internals.h"
41#include "specialize.h"
42#include "softfloat.h"
43
46
48{
49 union ui16_f16 uA;
50 uint_fast16_t uiA;
51 bool signA;
52 int_fast8_t expA;
53 uint_fast16_t sigA, uiZ;
54 struct exp8_sig16 normExpSig;
55 int_fast8_t expZ;
56 int index;
58 uint_fast32_t ESqrR0;
60 uint_fast16_t recipSqrt16, sigZ, shiftedSigZ;
61 uint16_t negRem;
62 union ui16_f16 uZ;
63
64 /*------------------------------------------------------------------------
65 *------------------------------------------------------------------------*/
66 uA.f = a;
67 uiA = uA.ui;
68 signA = signF16UI( uiA );
69 expA = expF16UI( uiA );
70 sigA = fracF16UI( uiA );
71 /*------------------------------------------------------------------------
72 *------------------------------------------------------------------------*/
73 if ( expA == 0x1F ) {
74 if ( sigA ) {
75 uiZ = softfloat_propagateNaNF16UI( uiA, 0 );
76 goto uiZ;
77 }
78 if ( ! signA ) return a;
79 goto invalid;
80 }
81 /*------------------------------------------------------------------------
82 *------------------------------------------------------------------------*/
83 if ( signA ) {
84 if ( ! (expA | sigA) ) return a;
85 goto invalid;
86 }
87 /*------------------------------------------------------------------------
88 *------------------------------------------------------------------------*/
89 if ( ! expA ) {
90 if ( ! sigA ) return a;
91 normExpSig = softfloat_normSubnormalF16Sig( sigA );
92 expA = normExpSig.exp;
93 sigA = normExpSig.sig;
94 }
95 /*------------------------------------------------------------------------
96 *------------------------------------------------------------------------*/
97 expZ = ((expA - 0xF)>>1) + 0xE;
98 expA &= 1;
99 sigA |= 0x0400;
100 index = (sigA>>6 & 0xE) + expA;
103 * (sigA & 0x7F))
104 >>11);
105 ESqrR0 = ((uint_fast32_t) r0 * r0)>>1;
106 if ( expA ) ESqrR0 >>= 1;
107 sigma0 = ~(uint_fast16_t) ((ESqrR0 * sigA)>>16);
108 recipSqrt16 = r0 + (((uint_fast32_t) r0 * sigma0)>>25);
109 if ( ! (recipSqrt16 & 0x8000) ) recipSqrt16 = 0x8000;
110 sigZ = ((uint_fast32_t) (sigA<<5) * recipSqrt16)>>16;
111 if ( expA ) sigZ >>= 1;
112 /*------------------------------------------------------------------------
113 *------------------------------------------------------------------------*/
114 ++sigZ;
115 if ( ! (sigZ & 7) ) {
116 shiftedSigZ = sigZ>>1;
117 negRem = shiftedSigZ * shiftedSigZ;
118 sigZ &= ~1;
119 if ( negRem & 0x8000 ) {
120 sigZ |= 1;
121 } else {
122 if ( negRem ) --sigZ;
123 }
124 }
125 return softfloat_roundPackToF16( 0, expZ, sigZ );
126 /*------------------------------------------------------------------------
127 *------------------------------------------------------------------------*/
128 invalid:
130 uiZ = defaultNaNF16UI;
131 uiZ:
132 uZ.ui = uiZ;
133 return uZ.f;
134
135}
136
#define sigma0(x)
Definition hash_impl.h:21
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
uint_fast16_t softfloat_propagateNaNF16UI(uint_fast16_t uiA, uint_fast16_t uiB)
void softfloat_raiseFlags(uint_fast8_t flags)
#define defaultNaNF16UI
Definition specialize.h:88
const uint16_t softfloat_approxRecipSqrt_1k0s[]
const uint16_t softfloat_approxRecipSqrt_1k1s[]
float16_t f16_sqrt(float16_t a)
Definition f16_sqrt.c:47
#define expF16UI(a)
Definition internals.h:84
#define signF16UI(a)
Definition internals.h:83
float16_t softfloat_roundPackToF16(bool, int_fast16_t, uint_fast16_t)
#define fracF16UI(a)
Definition internals.h:85
struct exp8_sig16 softfloat_normSubnormalF16Sig(uint_fast16_t)
@ softfloat_flag_invalid
Definition softfloat.h:89
unsigned short uint16_t
Definition stdint.h:125
uint16_t uint_fast16_t
Definition stdint.h:155
int8_t int_fast8_t
Definition stdint.h:150
uint32_t uint_fast32_t
Definition stdint.h:156
int_fast8_t exp
Definition internals.h:90
uint_fast16_t sig
Definition internals.h:90
uint16_t ui
Definition internals.h:45
float16_t f
Definition internals.h:45