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, 2017 The Regents of the 8University of California. 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 44float16_t softfloat_addMagsF16( uint_fast16_t uiA, uint_fast16_t uiB ) 45{ 46 int_fast8_t expA; 47 uint_fast16_t sigA; 48 int_fast8_t expB; 49 uint_fast16_t sigB; 50 int_fast8_t expDiff; 51 uint_fast16_t uiZ; 52 bool signZ; 53 int_fast8_t expZ; 54 uint_fast16_t sigZ; 55 uint_fast16_t sigX, sigY; 56 int_fast8_t shiftDist; 57 uint_fast32_t sig32Z; 58 int_fast8_t roundingMode; 59 union ui16_f16 uZ; 60 61 /*------------------------------------------------------------------------ 62 *------------------------------------------------------------------------*/ 63 expA = expF16UI( uiA ); 64 sigA = fracF16UI( uiA ); 65 expB = expF16UI( uiB ); 66 sigB = fracF16UI( uiB ); 67 /*------------------------------------------------------------------------ 68 *------------------------------------------------------------------------*/ 69 expDiff = expA - expB; 70 if ( ! expDiff ) { 71 /*-------------------------------------------------------------------- 72 *--------------------------------------------------------------------*/ 73 if ( ! expA ) { 74 uiZ = uiA + sigB; 75 goto uiZ; 76 } 77 if ( expA == 0x1F ) { 78 if ( sigA | sigB ) goto propagateNaN; 79 uiZ = uiA; 80 goto uiZ; 81 } 82 signZ = signF16UI( uiA ); 83 expZ = expA; 84 sigZ = 0x0800 + sigA + sigB; 85 if ( ! (sigZ & 1) && (expZ < 0x1E) ) { 86 sigZ >>= 1; 87 goto pack; 88 } 89 sigZ <<= 3; 90 } else { 91 /*-------------------------------------------------------------------- 92 *--------------------------------------------------------------------*/ 93 signZ = signF16UI( uiA ); 94 if ( expDiff < 0 ) { 95 /*---------------------------------------------------------------- 96 *----------------------------------------------------------------*/ 97 if ( expB == 0x1F ) { 98 if ( sigB ) goto propagateNaN; 99 uiZ = packToF16UI( signZ, 0x1F, 0 ); 100 goto uiZ; 101 } 102 if ( expDiff <= -13 ) { 103 uiZ = packToF16UI( signZ, expB, sigB ); 104 if ( expA | sigA ) goto addEpsilon; 105 goto uiZ; 106 } 107 expZ = expB; 108 sigX = sigB | 0x0400; 109 sigY = sigA + (expA ? 0x0400 : sigA); 110 shiftDist = 19 + expDiff; 111 } else { 112 /*---------------------------------------------------------------- 113 *----------------------------------------------------------------*/ 114 uiZ = uiA; 115 if ( expA == 0x1F ) { 116 if ( sigA ) goto propagateNaN; 117 goto uiZ; 118 } 119 if ( 13 <= expDiff ) { 120 if ( expB | sigB ) goto addEpsilon; 121 goto uiZ; 122 } 123 expZ = expA; 124 sigX = sigA | 0x0400; 125 sigY = sigB + (expB ? 0x0400 : sigB); 126 shiftDist = 19 - expDiff; 127 } 128 sig32Z = 129 ((uint_fast32_t) sigX<<19) + ((uint_fast32_t) sigY<<shiftDist); 130 if ( sig32Z < 0x40000000 ) { 131 --expZ; 132 sig32Z <<= 1; 133 } 134 sigZ = sig32Z>>16; 135 if ( sig32Z & 0xFFFF ) { 136 sigZ |= 1; 137 } else { 138 if ( ! (sigZ & 0xF) && (expZ < 0x1E) ) { 139 sigZ >>= 4; 140 goto pack; 141 } 142 } 143 } 144 return softfloat_roundPackToF16( signZ, expZ, sigZ ); 145 /*------------------------------------------------------------------------ 146 *------------------------------------------------------------------------*/ 147 propagateNaN: 148 uiZ = softfloat_propagateNaNF16UI( uiA, uiB ); 149 goto uiZ; 150 /*------------------------------------------------------------------------ 151 *------------------------------------------------------------------------*/ 152 addEpsilon: 153 roundingMode = softfloat_roundingMode; 154 if ( roundingMode != softfloat_round_near_even ) { 155 if ( 156 roundingMode 157 == (signF16UI( uiZ ) ? softfloat_round_min 158 : softfloat_round_max) 159 ) { 160 ++uiZ; 161 if ( (uint16_t) (uiZ<<1) == 0xF800 ) { 162 softfloat_raiseFlags( 163 softfloat_flag_overflow | softfloat_flag_inexact ); 164 } 165 } 166#ifdef SOFTFLOAT_ROUND_ODD 167 else if ( roundingMode == softfloat_round_odd ) { 168 uiZ |= 1; 169 } 170#endif 171 } 172 softfloat_raiseFlags( softfloat_flag_inexact ); 173 goto uiZ; 174 /*------------------------------------------------------------------------ 175 *------------------------------------------------------------------------*/ 176 pack: 177 uiZ = packToF16UI( signZ, expZ, sigZ ); 178 uiZ: 179 uZ.ui = uiZ; 180 return uZ.f; 181 182} 183 184