256-bit floating point
#1
Posted here because this isn’t user sample code, AI generated.
Inspired by Dale Yarker : "Euclidean division; quotient and remainder 1 call”
Passes 2,154 edge test. However, this is tricky stuff, proceed with caution.
The #2 versions were for my use, ignore.
Public domain, use at own risk. SDurham
Added Note: probably slow, but if you need it, you need it.

Code:
' F256.inc
' 256-bit binary floating point
' IEEE 754-2008 binary256
' 1 sign bit, 19 exponent bits (bias 262143), 236 fraction bits
' value = (-1)^sign * m * 2^(e - 255)
' m is the 256-bit significand : the leading 1 is bit 255
' d(0) = least significant 16 bits : d(15) = most significant
' d(16 to 31) are zero for a value : used by the 512-bit intermediates
' kind = 0 zero, 1 finite, 2 infinity, 3 not a number
' a variable of type F256T starts as zero

'inspired by Dale Yarker : "Euclidean division; quotient and remainder 1 call"
'https://pump.richheimer.de/showthread.php?tid=133

#If Not %Def(%F256INC261107)
%F256INC261107 = 1

%F256_ZERO   = 0
%F256_FINITE = 1
%F256_INF    = 2
%F256_NAN    = 3

%F256_EMAX   = 262143
%F256_EMIN   = -262142
%F256_DIGITS = 73
%F256_WORK   = 96

Type F256T
    d(0 To 31) As Word
    e As Long
    sign As Byte
    kind As Byte
End Type

' ================================================================
' public procedures
' ================================================================
' PowerBASIC resolves forward references : no prototypes needed

' ---------------------------------------------------------------- F256Zero
' set a value to positive zero
' parameters :
'   num = value to clear
' returns :
'   1 = success
' after the call :
'   num.kind = %F256_ZERO, num.sign = 0, all 32 words and the exponent are 0
' notes :
'   zero is the only value with kind = %F256_ZERO
'   the value is safe to use or free again
' ----------------------------------------------------------------

Function F256Zero(ByRef num As F256T) As Long
    ' set zero
    F256SetZero num
    Function = 1
End Function

' ---------------------------------------------------------------- F256Neg
' negate a value in place : flip the sign
' parameters :
'   num = value to negate
' returns :
'   1 = success
' notes :
'   +x becomes -x and -x becomes +x
'   zero becomes negative zero (-0) and negative zero becomes +0
'   nan is left unchanged : the sign of nan has no meaning
'   significand and exponent are not changed
' ----------------------------------------------------------------

Function F256Neg(ByRef num As F256T) As Long
    ' negate : flip sign
    If num.kind <> %F256_NAN Then
        If num.sign Then
            num.sign = 0
        Else
            num.sign = 1
        End If
    End If
    Function = 1
End Function

' ---------------------------------------------------------------- F256Copy
' copy one value to another
' parameters :
'   dst = destination value
'   src = source value
' returns :
'   1 = success
' notes :
'   a plain structure assignment : no memory is allocated
'   dst and src may be the same variable
'   the copy is independent : changing one never changes the other
' ----------------------------------------------------------------

Function F256Copy(ByRef dst As F256T, ByRef src As F256T) As Long
    ' copy value
    dst = src
    Function = 1
End Function

' ---------------------------------------------------------------- F256Free
' release a value
' parameters :
'   num = value to release
' returns :
'   1 = success
' notes :
'   provided for symmetry with heap based types : all storage lives
'   inside F256T, so there is nothing to free
'   the value is set to zero
'   safe to call more than once
' ----------------------------------------------------------------

Function F256Free(ByRef num As F256T) As Long
    ' nothing to release : all storage is inside the type
    F256SetZero num
    Function = 1
End Function

' ---------------------------------------------------------------- F256FromF64
' convert a Double to F256 : exact, no rounding
' parameters :
'   num   = destination value
'   value = Double to convert
' returns :
'   1 = numeric value stored (including +inf, -inf and zero)
'   0 = value was nan : num is set to nan
' notes :
'   a Double carries 53 significand bits and F256 carries 237, so
'   every finite Double is represented exactly
'   subnormal Doubles are normalised into the F256 exponent range
'   -0.0 keeps its sign
'   the Double sign, exponent and fraction fields are decoded directly
'   with VarPtr / Peek : no conversion library is required
' ----------------------------------------------------------------

Function F256FromF64(ByRef num As F256T, ByVal value As Double) As Long
    ' set from Double : exact, Double has 53 significand bits
    Local v As Double
    Local p As Dword, hw As Dword
    Local q As Quad, ex As Long, sg As Long, top As Long
    Local fr As Quad, mant As Quad

    If value <> value Then
        F256SetNaN num
        Function = 0
        Exit Function
    End If
    v = value
    p = VarPtr(v)
    hw = Peek(Dword, p + 4)
    sg = 0
    q = hw
    If q >= 2147483648 Then sg = 1
    q = q \ 1048576
    ex = q And 2047
    q = hw And 1048575
    fr = q * 4294967296 + Peek(Dword, p)
    If ex = 2047 Then
        If fr = 0 Then
            F256SetInf num, sg
            Function = 1
        Else
            F256SetNaN num
            Function = 0
        End If
        Exit Function
    End If
    If ex = 0 Then
        If fr = 0 Then
            F256SetZero num
            num.sign = sg
            Function = 1
            Exit Function
        End If
        F256SetZero num
        F256FromMantissa num, fr, sg, -1074
        Function = 1
        Exit Function
    End If
    mant = fr + 4503599627370496
    F256SetZero num
    F256FromMantissa num, mant, sg, ex - 1075
    Function = 1
End Function

' ---------------------------------------------------------------- F256FromStr
' convert a decimal string to F256 with correct rounding
' parameters :
'   num = destination value
'   s   = text to parse
' returns :
'   1 = parsed
'   0 = invalid text : num is set to zero
' accepted forms, case insensitive, surrounding spaces ignored :
'   [sign] digits [ . digits ] [ e [sign] digits ]
'   [sign] inf
'   [sign] infinity
'   [sign] nan
' notes :
'   up to 96 significant digits are kept, further digits set sticky
'   the decimal exponent is applied with repeated multiply / divide by
'   10^9, holding about 480 bits, then rounded once to 237 bits
'   values too large for the format become infinity
'   values too small for the format become zero
'   a decimal point with no digits after it (1.) is accepted
'   an exponent marker with no digits after it (1e) is rejected
' examples :
'   "3.14"   " -1.5e-3 "   "inf"   "NaN"   "-0"
' ----------------------------------------------------------------

Function F256FromStr(ByRef num As F256T, ByVal s As String) As Long
    ' set from decimal string : digits, optional point, optional exponent
    Local i As Long, sl As Long, ch As Long
    Local digits() As Byte
    Local cnt As Long, dropped As Long, fracd As Long
    Local decxp As Quad, esign As Long
    Local sign As Long, anyd As Long, edig As Long, sticky As Long
    Local m As F256T
    Local top As Long, be As Long

    Dim digits(0 To 95)

    sl = Len(s)
    i = 1
    Do While i <= sl
        ch = F256Ch(s, i)
        If ch = 32 Or ch = 9 Or ch = 13 Or ch = 10 Then
            i = i + 1
        Else
            Exit Do
        End If
    Loop
    sign = 0
    ch = F256Ch(s, i)
    If ch = 43 Or ch = 45 Then
        If ch = 45 Then sign = 1
        i = i + 1
    End If

    ' special values : inf, infinity, nan
    If (F256Ch(s, i) Or 32) = 105 And (F256Ch(s, i + 1) Or 32) = 110 And (F256Ch(s, i + 2) Or 32) = 102 Then
        i = i + 3
        If (F256Ch(s, i) Or 32) = 105 And (F256Ch(s, i + 1) Or 32) = 110 And (F256Ch(s, i + 2) Or 32) = 105 _
            And (F256Ch(s, i + 3) Or 32) = 116 And (F256Ch(s, i + 4) Or 32) = 121 Then
            i = i + 5
        End If
        Do While i <= sl
            ch = F256Ch(s, i)
            If ch = 32 Or ch = 9 Or ch = 13 Or ch = 10 Then
                i = i + 1
            Else
                Exit Do
            End If
        Loop
        If i <= sl Then
            F256SetZero num
            Function = 0
            Exit Function
        End If
        F256SetInf num, sign
        Function = 1
        Exit Function
    End If
    If (F256Ch(s, i) Or 32) = 110 And (F256Ch(s, i + 1) Or 32) = 97 And (F256Ch(s, i + 2) Or 32) = 110 Then
        i = i + 3
        Do While i <= sl
            ch = F256Ch(s, i)
            If ch = 32 Or ch = 9 Or ch = 13 Or ch = 10 Then
                i = i + 1
            Else
                Exit Do
            End If
        Loop
        If i <= sl Then
            F256SetZero num
            Function = 0
            Exit Function
        End If
        F256SetNaN num
        Function = 1
        Exit Function
    End If

    cnt = 0
    dropped = 0
    fracd = 0
    anyd = 0
    sticky = 0

    ' integer part
    Do While i <= sl
        ch = F256Ch(s, i)
        If ch < 48 Or ch > 57 Then Exit Do
        If cnt = 0 And ch = 48 Then
            ' leading zero
        ElseIf cnt < %F256_WORK Then
            digits(cnt) = ch - 48
            cnt = cnt + 1
        Else
            dropped = dropped + 1
            If ch <> 48 Then sticky = 1
        End If
        anyd = 1
        i = i + 1
    Loop

    ' fraction part
    If F256Ch(s, i) = 46 Then
        i = i + 1
        Do While i <= sl
            ch = F256Ch(s, i)
            If ch < 48 Or ch > 57 Then Exit Do
            fracd = fracd + 1
            If cnt = 0 And ch = 48 Then
                ' leading zero
            ElseIf cnt < %F256_WORK Then
                digits(cnt) = ch - 48
                cnt = cnt + 1
            Else
                dropped = dropped + 1
                If ch <> 48 Then sticky = 1
            End If
            anyd = 1
            i = i + 1
        Loop
    End If

    If anyd = 0 Then
        F256SetZero num
        Function = 0
        Exit Function
    End If

    ' exponent
    decxp = 0
    ch = F256Ch(s, i)
    If ch = 101 Or ch = 69 Then
        i = i + 1
        esign = 1
        ch = F256Ch(s, i)
        If ch = 43 Or ch = 45 Then
            If ch = 45 Then esign = -1
            i = i + 1
        End If
        edig = 0
        Do While i <= sl
            ch = F256Ch(s, i)
            If ch < 48 Or ch > 57 Then Exit Do
            edig = 1
            If decxp < 100000000 Then decxp = decxp * 10 + (ch - 48)
            i = i + 1
        Loop
        If edig = 0 Then
            F256SetZero num
            Function = 0
            Exit Function
        End If
        decxp = decxp * esign
    End If

    Do While i <= sl
        ch = F256Ch(s, i)
        If ch = 32 Or ch = 9 Or ch = 13 Or ch = 10 Then
            i = i + 1
        Else
            Exit Do
        End If
    Loop
    If i <= sl Then
        F256SetZero num
        Function = 0
        Exit Function
    End If

    If cnt = 0 Then
        F256SetZero num
        num.sign = sign
        Function = 1
        Exit Function
    End If

    decxp = decxp - fracd + dropped
    If decxp > 90000 Then
        F256SetInf num, sign
        Function = 1
        Exit Function
    End If
    If decxp < -90000 Then
        F256SetZero num
        num.sign = sign
        Function = 1
        Exit Function
    End If

    F256Clear m, 32
    i = 0
    Do While i < cnt
        F256MulAddSmall m, 32, 10, digits(i)
        i = i + 1
    Loop

    be = 0
    Do While decxp >= 9
        top = F256TopBit(m, 32)
        If top > 480 Then
            F256ShrBits m, 32, 32, sticky
            be = be + 32
        End If
        F256MulSmall m, 32, 1000000000
        decxp = decxp - 9
    Loop
    Do While decxp > 0
        top = F256TopBit(m, 32)
        If top > 480 Then
            F256ShrBits m, 32, 32, sticky
            be = be + 32
        End If
        F256MulSmall m, 32, F256Pow10(decxp)
        decxp = 0
    Loop
    Do While decxp <= -9
        top = F256TopBit(m, 32)
        If top < 480 Then
            F256ShlBits m, 32, 480 - top
            be = be - (480 - top)
        End If
        If F256DivSmall(m, 32, 1000000000) <> 0 Then sticky = 1
        decxp = decxp + 9
    Loop
    If decxp < 0 Then
        top = F256TopBit(m, 32)
        If top < 480 Then
            F256ShlBits m, 32, 480 - top
            be = be - (480 - top)
        End If
        If F256DivSmall(m, 32, F256Pow10(-decxp)) <> 0 Then sticky = 1
    End If

    F256Finish num, sign, m, be, sticky
    Function = 1
End Function

' ---------------------------------------------------------------- F256FromWStr
' convert a wide (UTF-16) decimal string to F256
' parameters :
'   num = destination value
'   s   = wide text to parse
' returns :
'   1 = parsed
'   0 = invalid text : num is set to zero
' notes :
'   the wide text is converted to an ANSI string and passed to
'   F256FromStr, so every rule of F256FromStr applies
' ----------------------------------------------------------------

Function F256FromWStr(ByRef num As F256T, ByVal s As WString) As Long
    ' set from wide string
    Local t As String
    t = s
    Function = F256FromStr(num, t)
End Function

' ---------------------------------------------------------------- F256ToStr
' convert a value to decimal text
' parameters :
'   num = value to convert
' returns :
'   text describing the value
' notes :
'   the significand is converted to decimal digits, then 2^(e-255) is
'   applied by doubling or halving that digit string
'   the text carries 73 significant digits : enough digits to read
'   every 237-bit value back exactly with F256FromStr
'   trailing zeros are not printed
'   plain form (no exponent) is used when the decimal exponent is in
'   the range -18 to +18, scientific form otherwise
'   special values : "0", "-0", "inf", "-inf", "nan"
' examples :
'   "0.5"   "-1234.5"   "1.267650600228229401496703205376e+30"
' ----------------------------------------------------------------

Function F256ToStr(ByRef num As F256T) As String
    ' string displays all digits
    ' plain form when -18 <= exponent <= 18
    ' scientific form otherwise
    Local d() As Byte
    Local cnt As Long, dexp As Long, decxp As Long
    Local i As Long, pp As Long, di As Long, lod As Long, cut As Long
    Local first As Long, rest As Long, lsb As Long, up As Long
    Local sticky As Long
    Local s As String, tmp As String
    Local mag As Long

    Dim d(0 To 97)

    If num.kind = %F256_NAN Then
        Function = "nan"
        Exit Function
    End If
    If num.kind = %F256_INF Then
        If num.sign Then Function = "-inf" Else Function = "inf"
        Exit Function
    End If
    If num.kind = %F256_ZERO Then
        If num.sign Then Function = "-0" Else Function = "0"
        Exit Function
    End If

    F256DigitsFromM num, d(), cnt
    dexp = 0
    sticky = 0

    i = num.e - 255
    If i > 0 Then
        Do While i > 0
            F256DigMul2 d(), cnt, dexp, sticky
            i = i - 1
        Loop
    Else
        Do While i < 0
            F256DigDiv2 d(), cnt, dexp, sticky
            i = i + 1
        Loop
    End If
    decxp = dexp + cnt - 1

    ' round to F256_DIGITS significant digits : round half even
    If cnt > %F256_DIGITS Then
        cut = cnt - %F256_DIGITS
        first = d(cut - 1)
        rest = sticky
        For i = 0 To cut - 2
            If d(i) <> 0 Then rest = 1
        Next i
        lsb = d(cut)
        up = 0
        If first > 5 Then
            up = 1
        ElseIf first = 5 Then
            If rest <> 0 Or (lsb And 1) <> 0 Then up = 1
        End If
        cnt = %F256_DIGITS
        dexp = dexp + cut
        For i = 0 To cnt - 1
            d(i) = d(i + cut)
        Next i
        If up Then
            i = 0
            Do While i < cnt
                If d(i) = 9 Then
                    d(i) = 0
                    i = i + 1
                Else
                    d(i) = d(i) + 1
                    Exit Do
                End If
            Loop
            If i = cnt Then
                d(cnt - 1) = 1
                For i = 0 To cnt - 2
                    d(i) = 0
                Next i
                decxp = decxp + 1
            End If
        End If
    End If

    ' trailing zeros are not significant
    lod = 0
    Do While lod < cnt - 1
        If d(lod) <> 0 Then Exit Do
        lod = lod + 1
    Loop

    s = ""
    If num.sign Then s = "-"

    If decxp >= -18 And decxp <= 18 Then
        If decxp >= 0 Then
            For pp = decxp To 0 Step -1
                di = pp - dexp
                If di >= 0 And di < cnt Then
                    s = s & Chr$(48 + d(di))
                Else
                    s = s & "0"
                End If
            Next pp
            If dexp < 0 And dexp + lod < 0 Then
                s = s & "."
                For pp = -1 To dexp + lod Step -1
                    di = pp - dexp
                    If di >= 0 And di < cnt Then
                        s = s & Chr$(48 + d(di))
                    Else
                        s = s & "0"
                    End If
                Next pp
            End If
        Else
            s = s & "0."
            For pp = -1 To dexp + lod Step -1
                di = pp - dexp
                If di >= 0 And di < cnt Then
                    s = s & Chr$(48 + d(di))
                Else
                    s = s & "0"
                End If
            Next pp
        End If
    Else
        s = s & Chr$(48 + d(cnt - 1))
        s = s & "."
        If cnt - 1 > lod Then
            For i = cnt - 2 To lod Step -1
                s = s & Chr$(48 + d(i))
            Next i
        Else
            s = s & "0"
        End If
        s = s & "e"
        If decxp < 0 Then s = s & "-" Else s = s & "+"
        mag = decxp
        If mag < 0 Then mag = -mag
        tmp = ""
        Do
            tmp = Chr$(48 + (mag Mod 10)) & tmp
            mag = mag \ 10
        Loop While mag > 0
        s = s & tmp
    End If

    Function = s
End Function

' ---------------------------------------------------------------- F256ToWStr
' convert a value to wide (UTF-16) decimal text
' parameters :
'   num = value to convert
' returns :
'   wide text describing the value
' notes :
'   F256ToStr is called and the ANSI text is converted to wide
'   all formatting rules of F256ToStr apply
' ----------------------------------------------------------------

Function F256ToWStr(ByRef num As F256T) As WString
    ' string displays all digits
    Function = F256ToStr(num)
End Function

' ---------------------------------------------------------------- F256Compare
' compare two values
' parameters :
'   a, b = values to compare
' returns :
'   -1 when a < b
'    0 when a = b
'   +1 when a > b
' ordering rules :
'   -0 and +0 compare equal
'   -inf < any finite value < +inf
'   every nan compares equal to every other nan and greater than +inf
'   the order is total, so sorting, binary search and duplicate removal
'   work correctly
' examples :
'   F256Compare(-2, -1)  = -1
'   F256Compare(-0, 0)   =  0
'   F256Compare(nan, inf) =  1
' ----------------------------------------------------------------

Function F256Compare(ByRef a As F256T, ByRef b As F256T) As Long
    ' compare values : -1, 0, 1
    ' zero : +0 = -0
    ' nan : all nan compare equal and greater than +infinity
    ' the order is total so sorting and searching work
    Local ra As Long, rb As Long, sa As Long, sb As Long, mag As Long, i As Long

    If a.kind = %F256_NAN Or b.kind = %F256_NAN Then
        If a.kind = %F256_NAN And b.kind = %F256_NAN Then
            Function = 0
        ElseIf a.kind = %F256_NAN Then
            Function = 1
        Else
            Function = -1
        End If
        Exit Function
    End If
    ra = a.kind
    rb = b.kind
    sa = a.sign
    sb = b.sign
    If a.kind = %F256_ZERO Then sa = 0
    If b.kind = %F256_ZERO Then sb = 0
    If sa <> sb Then
        If sa Then Function = -1 Else Function = 1
        Exit Function
    End If
    mag = 0
    If ra <> rb Then
        If ra < rb Then mag = -1 Else mag = 1
    ElseIf ra = %F256_FINITE Then
        If a.e <> b.e Then
            If a.e < b.e Then mag = -1 Else mag = 1
        Else
            For i = 15 To 0 Step -1
                If a.d(i) <> b.d(i) Then
                    If a.d(i) < b.d(i) Then mag = -1 Else mag = 1
                    Exit For
                End If
            Next i
        End If
    End If
    If sa Then mag = -mag
    Function = mag
End Function

' ---------------------------------------------------------------- F256CompareCB
' compare callback : F256Compare with pointer arguments
' parameters :
'   a, b = addresses of two F256T values, passed as Long
' returns :
'   -1, 0, +1 exactly as F256Compare
' notes :
'   the callback shape used by the other library containers, so it can
'   be passed directly to sort, search and unique procedures
'   PB/Win is 32-bit : a pointer fits in Long
' example :
'   n = F256CompareCB(VarPtr(x), VarPtr(y))
' ----------------------------------------------------------------

Function F256CompareCB(ByVal a As Long, ByVal b As Long) As Long
    ' compare callback : a and b are cast pointers
    Local pa As F256T Ptr, pb As F256T Ptr
    pa = a
    pb = b
    Function = F256Compare(@pa, @pb)
End Function

' ---------------------------------------------------------------- F256Abs
' absolute value
' parameters :
'   r = result
'   a = input value
' returns :
'   nothing : the result is stored in r
' notes :
'   r = |a| : the sign is cleared
'   -0 becomes +0
'   nan is copied unchanged
'   r and a may be the same variable
' ----------------------------------------------------------------

Sub F256Abs(ByRef r As F256T, ByRef a As F256T)
    ' get absolute value
    r = a
    If r.kind <> %F256_NAN Then r.sign = 0
End Sub

' ---------------------------------------------------------------- F256Add
' r = a + b
' parameters :
'   r = result
'   a, b = operands
' notes :
'   the operand with the smaller exponent is aligned to the larger one
'   in a 512-bit register, so bits below the 237-bit significand are
'   kept long enough for correct rounding
'   the sum is rounded once to nearest, ties to even
'   special values follow IEEE 754 :
'     inf + finite = inf
'     inf + -inf   = nan
'     nan + x      = nan
'     x + 0        = x
'   r may be the same variable as a or b
' ----------------------------------------------------------------

Sub F256Add(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T)
    ' r = a + b
    F256Compute r, a, b, 0, 0
End Sub

' ---------------------------------------------------------------- F256Subtract
' r = a - b
' parameters :
'   r = result
'   a, b = operands
' notes :
'   performed as a + (-b) with the same 512-bit alignment and one
'   rounding step
'   special values follow IEEE 754 :
'     inf - finite = inf
'     inf - inf    = nan
'     nan - x      = nan
'     x - x        = +0
'   r may be the same variable as a or b
' ----------------------------------------------------------------

Sub F256Subtract(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T)
    ' r = a - b
    F256Compute r, a, b, 1, 0
End Sub

' ---------------------------------------------------------------- F256Multiply
' r = a * b
' parameters :
'   r = result
'   a, b = operands
' notes :
'   the two 256-bit significands are multiplied into a 512-bit
'   product, then normalised and rounded once to 237 bits
'   special values :
'     inf * 0 = nan
'     inf * x = inf, sign from the operands
'     0 * x   = 0
'     nan * x = nan
'   r may be the same variable as a or b
' ----------------------------------------------------------------

Sub F256Multiply(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T)
    ' r = a * b
    F256Compute r, a, b, 2, 0
End Sub

' ---------------------------------------------------------------- F256Divide
' r = a / b
' parameters :
'   r = result
'   a = dividend
'   b = divisor
' notes :
'   the dividend significand is shifted left 256 bits and divided by
'   the divisor significand one bit at a time, giving a 512-bit
'   quotient; the remainder sets the sticky bit used for rounding
'   the quotient is rounded once to nearest, ties to even
'   special values :
'     x / 0     = inf, sign from the operands
'     0 / 0     = nan
'     inf / inf = nan
'     x / inf   = 0
'     inf / x   = inf
'     nan / x   = nan
'   use F256Divide2 when a zero divisor must return zero
'   r may be the same variable as a or b
' ----------------------------------------------------------------

Sub F256Divide(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T)
    ' r = a / b
    F256Compute r, a, b, 3, 0
End Sub

' ---------------------------------------------------------------- F256Divide2
' r = a / b : division by zero returns zero instead of infinity
' parameters :
'   r = result
'   a = dividend
'   b = divisor
' notes :
'   identical to F256Divide except when b is zero : the result is zero
'   useful in iterative code where a zero divisor is a normal event and
'   an infinity would poison later operations
'   every other special value behaves exactly as F256Divide
' ----------------------------------------------------------------

Sub F256Divide2(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T)
    ' r = a / b : divide by zero = zero
    F256Compute r, a, b, 3, 1
End Sub

' ---------------------------------------------------------------- F256Squared
' r = a * a
' parameters :
'   r = result
'   a = input value
' notes :
'   one correctly rounded multiplication
'   r may be the same variable as a
' ----------------------------------------------------------------

Sub F256Squared(ByRef r As F256T, ByRef a As F256T)
    ' r = a * a
    F256Compute r, a, a, 2, 0
End Sub

' ---------------------------------------------------------------- F256Cubed
' r = a * a * a
' parameters :
'   r = result
'   a = input value
' notes :
'   two multiplications, each correctly rounded
'   the sign follows the operand : (-2) cubed is -8
'   r may be the same variable as a
' ----------------------------------------------------------------

Sub F256Cubed(ByRef r As F256T, ByRef a As F256T)
    ' r = a * a * a
    Local t As F256T
    F256Compute t, a, a, 2, 0
    F256Compute r, t, a, 2, 0
End Sub

' ---------------------------------------------------------------- F256Cubed2
' r = |a| * |a| * |a|
' parameters :
'   r = result
'   a = input value
' notes :
'   the absolute value is used, so the result is never negative
'   (-2) cubed gives 8
'   zero gives zero
'   r may be the same variable as a
' ----------------------------------------------------------------

Sub F256Cubed2(ByRef r As F256T, ByRef a As F256T)
    ' r = |a| * |a| * |a| : absolute result
    Local v As F256T
    F256Abs v, a
    F256Compute r, v, v, 2, 0
    F256Compute r, r, v, 2, 0
End Sub

' ---------------------------------------------------------------- F256SquareRoot
' r = square root of a
' parameters :
'   r = result
'   a = input value
' notes :
'   Newton iteration from a power of two estimate, 12 passes
'   the operand is scaled into [1,4) first, so very large and very
'   small exponents keep full precision
'   a negative operand gives nan
'   sqrt(0) = 0, sqrt(inf) = inf, sqrt(nan) = nan
'   the result is within a few units in the last place of the exact root
'   r may be the same variable as a
' ----------------------------------------------------------------

Sub F256SquareRoot(ByRef r As F256T, ByRef a As F256T)
    ' square root
    F256Root r, a, 2, 0
End Sub

' ---------------------------------------------------------------- F256SquareRoot2
' r = square root of |a|
' parameters :
'   r = result
'   a = input value
' notes :
'   a negative operand is accepted : the absolute value is used
'   the result is never nan for a numeric operand
'   -4 gives 2, -inf gives +inf, zero gives zero, nan gives nan
' ----------------------------------------------------------------

Sub F256SquareRoot2(ByRef r As F256T, ByRef a As F256T)
    ' square root of absolute value
    F256Root r, a, 2, 1
End Sub

' ---------------------------------------------------------------- F256CubeRoot
' r = cube root of a
' parameters :
'   r = result
'   a = input value
' notes :
'   Newton iteration from a power of two estimate, 12 passes
'   the operand is scaled into [1,8) first, so very large and very
'   small exponents keep full precision
'   a negative operand returns the real (negative) cube root
'   -27 gives -3, -inf gives -inf
'   zero gives zero, nan gives nan
'   r may be the same variable as a
' ----------------------------------------------------------------

Sub F256CubeRoot(ByRef r As F256T, ByRef a As F256T)
    ' cube root
    F256Root r, a, 3, 0
End Sub

' ---------------------------------------------------------------- F256CubeRoot2
' r = cube root of |a|
' parameters :
'   r = result
'   a = input value
' notes :
'   a negative operand is accepted and the result is positive
'   -27 gives 3, -inf gives +inf
'   zero gives zero, nan gives nan
' ----------------------------------------------------------------

Sub F256CubeRoot2(ByRef r As F256T, ByRef a As F256T)
    ' cube root of absolute value
    F256Root r, a, 3, 1
End Sub

' ---------------------------------------------------------------- F256Power
' r = a raised to an integer power
' parameters :
'   r  = result
'   a  = base
'   pw = integer exponent, Quad (64-bit signed)
' notes :
'   exponentiation by squaring : about log2(|pw|) multiplications
'   a negative exponent computes 1 / (a ^ |pw|)
'   a ^ 0 = 1 for every base, including zero and nan
'   0 ^ positive = 0, 0 ^ negative = inf
'   a negative base with an odd exponent is negative, even is positive
'   overflow gives inf, underflow gives zero
'   r may be the same variable as a
' ----------------------------------------------------------------

Sub F256Power(ByRef r As F256T, ByRef a As F256T, ByVal pw As Quad)
    ' r = a raised to an integer power
    Local bs As F256T, Res As F256T, one As F256T
    Local mag As Quad, sg As Long

    If pw = 0 Then
        F256FromF64 Res, 1.0
        r = Res
        Exit Sub
    End If
    mag = pw
    sg = 0
    If mag < 0 Then
        sg = 1
        mag = -mag
    End If
    bs = a
    F256FromF64 Res, 1.0
    Do While mag > 0
        If (mag And 1) <> 0 Then F256Compute Res, Res, bs, 2, 0
        mag = mag \ 2
        If mag > 0 Then F256Compute bs, bs, bs, 2, 0
    Loop
    If sg Then
        F256FromF64 one, 1.0
        F256Compute Res, one, Res, 3, 0
    End If
    r = Res
End Sub

' //////////////////// private ////////////////////

Function F256Pow2(ByVal n As Long) As Long
    ' 2 raised to n : n = 0 to 15
    Local v As Long
    v = 1
    Do While n > 0
        v = v * 2
        n = n - 1
    Loop
    Function = v
End Function

Function F256Pow10(ByVal n As Long) As Long
    ' 10 raised to n : n = 0 to 8
    Local v As Long, i As Long
    v = 1
    For i = 1 To n
        v = v * 10
    Next i
    Function = v
End Function

Sub F256Clear(ByRef a As F256T, ByVal n As Long)
    ' clear n words
    Local i As Long
    For i = 0 To n - 1
        a.d(i) = 0
    Next i
End Sub

Function F256IsZero(ByRef a As F256T, ByVal n As Long) As Long
    ' true when n words are zero
    Local i As Long
    For i = 0 To n - 1
        If a.d(i) <> 0 Then
            Function = 0
            Exit Function
        End If
    Next i
    Function = 1
End Function

Function F256TopBit(ByRef a As F256T, ByVal n As Long) As Long
    ' index of highest set bit : -1 when zero
    Local i As Long, v As Long, bt As Long
    For i = n - 1 To 0 Step -1
        v = a.d(i)
        If v <> 0 Then
            bt = 15
            Do While (v And 32768) = 0
                v = v * 2
                bt = bt - 1
            Loop
            Function = i * 16 + bt
            Exit Function
        End If
    Next i
    Function = -1
End Function

Sub F256Shl1(ByRef a As F256T, ByVal n As Long)
    ' shift n words left one bit
    Local i As Long, carry As Long, v As Long
    carry = 0
    For i = 0 To n - 1
        v = CLng(a.d(i)) * 2 + carry
        a.d(i) = v And 65535
        carry = v \ 65536
    Next i
End Sub

Sub F256ShlBits(ByRef a As F256T, ByVal n As Long, ByVal k As Long)
    ' shift n words left k bits
    Local t As Long
    If k <= 0 Then Exit Sub
    If k >= n * 16 Then
        F256Clear a, n
        Exit Sub
    End If
    For t = 1 To k
        F256Shl1 a, n
    Next t
End Sub

Sub F256Shr1(ByRef a As F256T, ByVal n As Long, ByRef sticky As Long)
    ' shift n words right one bit
    Local i As Long, carry As Long, v As Long
    carry = 0
    For i = n - 1 To 0 Step -1
        v = a.d(i)
        a.d(i) = (v \ 2) Or (carry * 32768)
        carry = v And 1
    Next i
    If carry <> 0 Then sticky = 1
End Sub

Sub F256ShrBits(ByRef a As F256T, ByVal n As Long, ByVal k As Long, ByRef sticky As Long)
    ' shift n words right k bits
    Local t As Long
    If k <= 0 Then Exit Sub
    If k >= n * 16 Then
        If F256IsZero(a, n) = 0 Then sticky = 1
        F256Clear a, n
        Exit Sub
    End If
    For t = 1 To k
        F256Shr1 a, n, sticky
    Next t
End Sub

Function F256CmpMag(ByRef a As F256T, ByRef b As F256T, ByVal n As Long) As Long
    ' compare n words : -1, 0, 1
    Local i As Long
    For i = n - 1 To 0 Step -1
        If a.d(i) <> b.d(i) Then
            If a.d(i) < b.d(i) Then
                Function = -1
            Else
                Function = 1
            End If
            Exit Function
        End If
    Next i
    Function = 0
End Function

Function F256AddInto(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T, ByVal n As Long) As Long
    ' r = a + b : return carry
    Local i As Long, s As Long, carry As Long
    carry = 0
    For i = 0 To n - 1
        s = CLng(a.d(i)) + CLng(b.d(i)) + carry
        r.d(i) = s And 65535
        carry = s \ 65536
    Next i
    Function = carry
End Function

Sub F256SubInto(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T, ByVal n As Long)
    ' r = a - b : a >= b
    Local i As Long, s As Long, borrow As Long
    borrow = 0
    For i = 0 To n - 1
        s = CLng(a.d(i)) - CLng(b.d(i)) - borrow
        If s < 0 Then
            r.d(i) = s + 65536
            borrow = 1
        Else
            r.d(i) = s
            borrow = 0
        End If
    Next i
End Sub

Sub F256MulWide(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T)
    ' r = a * b : 256 x 256 -> 512 bits
    Local i As Long, j As Long, t As Quad, carry As Quad
    F256Clear r, 32
    For i = 0 To 15
        carry = 0
        For j = 0 To 15
            t = a.d(i)
            t = t * b.d(j) + r.d(i + j) + carry
            r.d(i + j) = t And 65535
            carry = t \ 65536
        Next j
        r.d(i + 16) = carry And 65535
    Next i
End Sub

Sub F256DivModWide(ByRef q As F256T, ByRef rm As F256T, ByRef a As F256T, ByRef b As F256T)
    ' q = a / b : rm = a mod b : 512 / 256 : one bit at a time
    Local bt As Long, i As Long, v As Long, carry As Long
    F256Clear q, 32
    F256Clear rm, 32
    For bt = 511 To 0 Step -1
        carry = 0
        For i = 0 To 31
            v = CLng(rm.d(i)) * 2 + carry
            rm.d(i) = v And 65535
            carry = v \ 65536
        Next i
        If ((a.d(bt \ 16) \ F256Pow2(bt Mod 16)) And 1) <> 0 Then
            rm.d(0) = rm.d(0) Or 1
        End If
        If F256CmpMag(rm, b, 32) >= 0 Then
            F256SubInto rm, rm, b, 32
            q.d(bt \ 16) = q.d(bt \ 16) Or F256Pow2(bt Mod 16)
        End If
    Next bt
End Sub

Function F256MulSmall(ByRef a As F256T, ByVal n As Long, ByVal mv As Long) As Long
    ' a = a * mv : return carry
    Local i As Long, t As Quad, carry As Quad
    carry = 0
    For i = 0 To n - 1
        t = a.d(i)
        t = t * mv + carry
        a.d(i) = t And 65535
        carry = t \ 65536
    Next i
    Function = carry And 65535
End Function

Function F256MulAddSmall(ByRef a As F256T, ByVal n As Long, ByVal mv As Long, ByVal av As Long) As Long
    ' a = a * mv + av : return carry
    Local i As Long, t As Quad, carry As Quad
    carry = av
    For i = 0 To n - 1
        t = a.d(i)
        t = t * mv + carry
        a.d(i) = t And 65535
        carry = t \ 65536
    Next i
    Function = carry And 65535
End Function

Function F256DivSmall(ByRef a As F256T, ByVal n As Long, ByVal dv As Long) As Long
    ' a = a / dv : return remainder
    Local i As Long, rmdr As Quad, cv As Quad
    rmdr = 0
    For i = n - 1 To 0 Step -1
        cv = rmdr * 65536 + a.d(i)
        a.d(i) = (cv \ dv) And 65535
        rmdr = cv Mod dv
    Next i
    Function = rmdr
End Function

Sub F256SetZero(ByRef x As F256T)
    ' zero
    F256Clear x, 32
    x.e = 0
    x.sign = 0
    x.kind = %F256_ZERO
End Sub

Sub F256SetInf(ByRef x As F256T, ByVal s As Long)
    ' infinity
    F256SetZero x
    x.kind = %F256_INF
    x.sign = s And 1
End Sub

Sub F256SetNaN(ByRef x As F256T)
    ' not a number
    F256SetZero x
    x.kind = %F256_NAN
End Sub

Sub F256Round(ByRef x As F256T, ByVal stickyIn As Long)
    ' round to nearest, ties to even
    Local st As Long, shf As Long, guard As Long, lstick As Long, lsb As Long
    Local up As Long, carry As Long, i As Long, v As Long, sg As Long

    st = stickyIn
    If x.e < %F256_EMIN Then
        shf = %F256_EMIN - x.e
        If shf >= 256 Then
            sg = x.sign
            F256SetZero x
            x.sign = sg
            Exit Sub
        End If
        F256ShrBits x, 16, shf, st
        x.e = %F256_EMIN
    End If

    guard = (x.d(1) \ 4) And 1
    lstick = ((x.d(1) And 3) Or x.d(0)) <> 0
    lsb = (x.d(1) \ 8) And 1
    up = 0
    If guard <> 0 Then
        If lstick Or st Or lsb Then up = 1
    End If
    x.d(0) = 0
    x.d(1) = x.d(1) And 65528

    If up Then
        carry = 8
        i = 1
        Do While carry <> 0 And i < 16
            v = CLng(x.d(i)) + carry
            x.d(i) = v And 65535
            carry = v \ 65536
            i = i + 1
        Loop
        If carry <> 0 Then
            sg = x.sign
            F256Clear x, 32
            x.d(15) = 32768
            x.sign = sg
            x.kind = %F256_FINITE
            x.e = x.e + 1
        End If
    End If

    If F256IsZero(x, 16) Then
        sg = x.sign
        F256SetZero x
        x.sign = sg
        Exit Sub
    End If
    If x.e > %F256_EMAX Then
        F256SetInf x, x.sign
    End If
End Sub

Sub F256Finish(ByRef x As F256T, ByVal sign As Long, ByRef w As F256T, ByVal s As Long, ByVal stickyIn As Long)
    ' value = w * 2^s : round and store
    Local top As Long, shf As Long, sticky As Long, i As Long
    F256SetZero x
    x.sign = sign And 1
    If F256IsZero(w, 32) Then Exit Sub
    top = F256TopBit(w, 32)
    shf = top - 255
    sticky = stickyIn
    If shf > 0 Then
        F256ShrBits w, 32, shf, sticky
    ElseIf shf < 0 Then
        F256ShlBits w, 32, -shf
    End If
    For i = 0 To 15
        x.d(i) = w.d(i)
    Next i
    x.e = top + s
    x.kind = %F256_FINITE
    F256Round x, sticky
End Sub

Sub F256Norm(ByRef x As F256T)
    ' move the leading significand bit to bit 255
    Local top As Long
    If x.kind <> %F256_FINITE Then Exit Sub
    top = F256TopBit(x, 16)
    If top < 0 Then Exit Sub
    If top < 255 Then
        F256ShlBits x, 16, 255 - top
        x.e = x.e - (255 - top)
    End If
End Sub

Sub F256Scale(ByRef x As F256T, ByVal k As Long)
    ' x = x * 2^k
    Local ee As Long, sg As Long
    If x.kind <> %F256_FINITE Then Exit Sub
    If k = 0 Then Exit Sub
    F256Norm x
    ee = x.e + k
    If ee > %F256_EMAX + 2 Then
        F256SetInf x, x.sign
        Exit Sub
    End If
    If ee < %F256_EMIN - 300 Then
        sg = x.sign
        F256SetZero x
        x.sign = sg
        Exit Sub
    End If
    x.e = ee
    F256Round x, 0
End Sub

Sub F256FromMantissa(ByRef x As F256T, ByRef mant As Quad, ByVal sign As Long, ByVal eZero As Long)
    ' x = mant * 2^eZero : mant is a positive integer, eZero its binary exponent
    Local top As Long
    F256Clear x, 32
    x.d(0) = mant And 65535
    x.d(1) = (mant \ 65536) And 65535
    x.d(2) = (mant \ 4294967296) And 65535
    x.d(3) = (mant \ 281474976710656) And 65535
    x.sign = sign And 1
    x.kind = %F256_FINITE
    top = F256TopBit(x, 16)
    If top < 0 Then
        F256SetZero x
        Exit Sub
    End If
    If top < 255 Then F256ShlBits x, 16, 255 - top
    x.e = eZero + top
    F256Round x, 0
End Sub

Function F256Ch(ByVal s As String, ByVal i As Long) As Long
    ' character code : 0 past the end
    If i < 1 Or i > Len(s) Then
        Function = 0
    Else
        Function = Asc(Mid$(s, i, 1))
    End If
End Function

Sub F256DigMul2(d() As Byte, cnt As Long, dexp As Long, sticky As Long)
    ' decimal digits = digits * 2
    Local i As Long, c As Long, t As Long
    c = 0
    For i = 0 To cnt - 1
        t = CLng(d(i)) * 2 + c
        d(i) = t Mod 10
        c = t \ 10
    Next i
    If c <> 0 Then
        d(cnt) = c
        cnt = cnt + 1
    End If
    If cnt > %F256_WORK Then
        If d(0) <> 0 Then sticky = 1
        For i = 0 To %F256_WORK - 1
            d(i) = d(i + 1)
        Next i
        cnt = cnt - 1
        dexp = dexp + 1
    End If
End Sub

Sub F256DigDiv2(d() As Byte, cnt As Long, dexp As Long, sticky As Long)
    ' decimal digits = digits / 2
    Local i As Long, rmdr As Long, cv As Long
    If (d(0) And 1) <> 0 Then
        i = cnt
        Do While i > 0
            d(i) = d(i - 1)
            i = i - 1
        Loop
        d(0) = 0
        cnt = cnt + 1
        dexp = dexp - 1
        If cnt > %F256_WORK Then
            For i = 0 To %F256_WORK - 1
                d(i) = d(i + 1)
            Next i
            cnt = cnt - 1
            dexp = dexp + 1
        End If
    End If
    rmdr = 0
    i = cnt - 1
    Do While i >= 0
        cv = rmdr * 10 + d(i)
        d(i) = cv \ 2
        rmdr = cv Mod 2
        i = i - 1
    Loop
    If rmdr <> 0 Then sticky = 1
    Do While cnt > 1
        If d(cnt - 1) <> 0 Then Exit Do
        cnt = cnt - 1
    Loop
End Sub

Sub F256DigitsFromM(ByRef x As F256T, d() As Byte, cnt As Long)
    ' decimal digits of the significand : least significant first
    Local t As F256T
    Local rmdr As Long, i As Long
    t = x
    cnt = 0
    Do While F256IsZero(t, 16) = 0
        rmdr = F256DivSmall(t, 16, 1000000000)
        For i = 1 To 9
            d(cnt) = rmdr Mod 10
            rmdr = rmdr \ 10
            cnt = cnt + 1
        Next i
    Loop
    If cnt = 0 Then
        d(0) = 0
        cnt = 1
    End If
    Do While cnt > 1
        If d(cnt - 1) <> 0 Then Exit Do
        cnt = cnt - 1
    Loop
End Sub

Sub F256Root(ByRef r As F256T, ByRef a As F256T, ByVal degree As Long, ByVal absIn As Long)
    ' square root or cube root : Newton from a power of two estimate
    Local x As F256T, y As F256T, t As F256T, u As F256T
    Local sg As Long, k As Long, i As Long, top As Long
    Local three As F256T

    If a.kind = %F256_NAN Then
        F256SetNaN r
        Exit Sub
    End If
    If a.kind = %F256_ZERO Then
        F256SetZero r
        Exit Sub
    End If
    If a.kind = %F256_INF Then
        If a.sign And (absIn = 0) And (degree = 2) Then
            F256SetNaN r
        Else
            r = a
            If absIn <> 0 Then r.sign = 0
        End If
        Exit Sub
    End If
    sg = a.sign
    If sg And absIn = 0 And degree = 2 Then
        F256SetNaN r
        Exit Sub
    End If

    x = a
    x.sign = 0
    F256Norm x
    top = F256TopBit(x, 16)
    If top < 0 Then
        F256SetZero r
        Exit Sub
    End If
    k = x.e \ degree
    If (x.e < 0) And ((x.e Mod degree) <> 0) Then k = k - 1
    F256SetZero y
    y.d(15) = 32768
    y.e = k
    y.kind = %F256_FINITE

    If degree = 2 Then
        For i = 1 To 12
            F256Compute t, x, y, 3, 0
            F256Compute t, y, t, 0, 0
            F256Scale t, -1
            y = t
        Next i
    Else
        F256FromF64 three, 3.0
        For i = 1 To 12
            F256Compute u, y, y, 2, 0
            F256Compute t, x, u, 3, 0
            F256Compute u, y, y, 0, 0
            F256Compute u, u, t, 0, 0
            F256Compute y, u, three, 3, 0
        Next i
    End If
    If sg And (absIn = 0) Then
        If y.kind <> %F256_ZERO Then y.sign = 1
    End If
    r = y
End Sub

Sub F256Compute(ByRef r As F256T, ByRef a As F256T, ByRef b As F256T, ByVal op As Long, ByVal zeroDiv As Long)
    ' op 0 add : 1 subtract : 2 multiply : 3 divide
    Local aa As F256T, bb As F256T, tt As F256T
    Local wa As F256T, wb As F256T, wq As F256T, wr As F256T
    Local d As Long, ss As Long, i As Long, carry As Long, sticky As Long
    Local sg As Long

    If op = 2 Then
        ' multiply
        If a.kind = %F256_NAN Or b.kind = %F256_NAN Then
            F256SetNaN r
            Exit Sub
        End If
        If a.kind = %F256_INF Or b.kind = %F256_INF Then
            If a.kind = %F256_ZERO Or b.kind = %F256_ZERO Then
                F256SetNaN r
            Else
                F256SetInf r, (a.sign Xor b.sign)
            End If
            Exit Sub
        End If
        If a.kind = %F256_ZERO Or b.kind = %F256_ZERO Then
            F256SetZero r
            r.sign = (a.sign Xor b.sign)
            Exit Sub
        End If
        F256MulWide tt, a, b
        F256Finish r, (a.sign Xor b.sign), tt, a.e + b.e - 510, 0
        Exit Sub
    End If

    If op = 3 Then
        ' divide
        If a.kind = %F256_NAN Or b.kind = %F256_NAN Then
            F256SetNaN r
            Exit Sub
        End If
        If a.kind = %F256_INF Then
            If b.kind = %F256_INF Then
                F256SetNaN r
            Else
                F256SetInf r, (a.sign Xor b.sign)
            End If
            Exit Sub
        End If
        If b.kind = %F256_INF Then
            F256SetZero r
            r.sign = (a.sign Xor b.sign)
            Exit Sub
        End If
        If b.kind = %F256_ZERO Then
            If zeroDiv Then
                F256SetZero r
                Exit Sub
            End If
            If a.kind = %F256_ZERO Then
                F256SetNaN r
            Else
                F256SetInf r, (a.sign Xor b.sign)
            End If
            Exit Sub
        End If
        If a.kind = %F256_ZERO Then
            F256SetZero r
            r.sign = (a.sign Xor b.sign)
            Exit Sub
        End If
        F256Clear wa, 32
        F256Clear wb, 32
        For i = 0 To 15
            wa.d(i + 16) = a.d(i)
            wb.d(i) = b.d(i)
        Next i
        F256DivModWide wq, wr, wa, wb
        sticky = 0
        If F256IsZero(wr, 32) = 0 Then sticky = 1
        F256Finish r, (a.sign Xor b.sign), wq, a.e - b.e - 256, sticky
        Exit Sub
    End If

    ' add and subtract
    If op = 1 Then
        bb = b
        If bb.kind <> %F256_NAN Then
            If bb.sign Then
                bb.sign = 0
            Else
                bb.sign = 1
            End If
        End If
    Else
        bb = b
    End If

    If a.kind = %F256_NAN Or bb.kind = %F256_NAN Then
        F256SetNaN r
        Exit Sub
    End If
    If a.kind = %F256_INF Then
        If bb.kind = %F256_INF And a.sign <> bb.sign Then
            F256SetNaN r
        Else
            r = a
        End If
        Exit Sub
    End If
    If bb.kind = %F256_INF Then
        r = bb
        Exit Sub
    End If
    If a.kind = %F256_ZERO And bb.kind = %F256_ZERO Then
        F256SetZero r
        Exit Sub
    End If
    If a.kind = %F256_ZERO Then
        r = bb
        Exit Sub
    End If
    If bb.kind = %F256_ZERO Then
        r = a
        Exit Sub
    End If

    aa = a
    If aa.e < bb.e Then
        tt = aa
        aa = bb
        bb = tt
    End If
    d = aa.e - bb.e

    F256Clear wa, 32
    For i = 0 To 15
        wa.d(i + 16) = aa.d(i)
    Next i
    F256Clear wb, 32
    sticky = 0
    If d >= 512 Then
        If F256IsZero(bb, 16) = 0 Then sticky = 1
    Else
        For i = 0 To 15
            wb.d(i + 16) = bb.d(i)
        Next i
        If d > 0 Then F256ShrBits wb, 32, d, sticky
    End If

    ss = aa.e - 511
    If aa.sign = bb.sign Then
        carry = F256AddInto(wa, wa, wb, 32)
    Else
        carry = 0
        If F256CmpMag(wa, wb, 32) < 0 Then
            F256SubInto wa, wb, wa, 32
            aa.sign = bb.sign
        Else
            F256SubInto wa, wa, wb, 32
        End If
    End If
    If carry <> 0 Then
        If (wa.d(0) And 1) <> 0 Then sticky = 1
        F256ShrBits wa, 32, 1, sticky
        wa.d(31) = wa.d(31) Or 32768
        ss = ss + 1
    End If
    F256Finish r, aa.sign, wa, ss, sticky
End Sub

#EndIf '%F256INC261107
Reply


Messages In This Thread
256-bit floating point - by Stanley Durham - 6 hours ago

Forum Jump:


Users browsing this thread: 1 Guest(s)