mirror of
https://github.com/dschmenk/PLASMA.git
synced 2025-01-10 06:30:41 +00:00
Better fixed point formatting
This commit is contained in:
parent
543f5fa338
commit
53307bf7d7
@ -1,10 +1,17 @@
|
||||
import fpstr
|
||||
//
|
||||
// String formatting bits
|
||||
//
|
||||
const FPSTR_FIXED = 1 // Fixed count of fractional digits
|
||||
const FPSTR_FLOAT = 0 // Floating count of fractional digits
|
||||
const FPSTR_STRIP = 2 // Strip trailing fractional zeros
|
||||
const FPSTR_EXP = 4 // Force exponential format
|
||||
//
|
||||
// Parse string into decrecord, return SANE conversion output
|
||||
//
|
||||
predef str2ext(str, ext)#1
|
||||
//
|
||||
// Convert extended FP to string using , return string
|
||||
//
|
||||
predef ext2str(ext, str, numdigits, expformat)#1
|
||||
predef ext2str(ext, str, intdigits, fracdigits, format)#1
|
||||
end
|
||||
|
@ -129,14 +129,15 @@ end
|
||||
//
|
||||
// Convert extended FP to string using , return string
|
||||
//
|
||||
export def ext2str(ext, str, numdigits, expformat)
|
||||
byte d, i, sigdigits
|
||||
export def ext2str(ext, str, intdigits, fracdigits, format)
|
||||
byte d, i, sigdigits, numdigits
|
||||
word dp, tens
|
||||
byte decform[t_decformat]
|
||||
byte decrec[t_decrecord]
|
||||
|
||||
decform:style = 0
|
||||
decform:digits = numdigits
|
||||
numdigits = intdigits + fracdigits
|
||||
decform:style = format & $01
|
||||
decform:digits = decform:style ?? fracdigits :: numdigits
|
||||
sane:fpOp3(FFEXT|FOB2D, @decrec, ext, @decform)
|
||||
^(str+1) = decrec.sgn ?? '-' :: ' '
|
||||
if decrec.sig.1 == 'I'
|
||||
@ -154,19 +155,24 @@ export def ext2str(ext, str, numdigits, expformat)
|
||||
return str
|
||||
fin
|
||||
if decrec.sig.1 == '0'
|
||||
while decrec.sig < fracdigits
|
||||
decrec.sig++
|
||||
decrec.sig[decrec.sig] = '0'
|
||||
loop
|
||||
decrec:exp = -decrec.sig
|
||||
fin
|
||||
dp = decrec.sig + decrec:exp
|
||||
sigdigits = decrec.sig
|
||||
if decrec:exp < 0
|
||||
if decrec:exp < 0 and format & $02
|
||||
//
|
||||
// Strip off trailing fractional zeros
|
||||
//
|
||||
while sigdigits > dp and decrec.sig[sigdigits] == '0'
|
||||
sigdigits--
|
||||
decrec:exp++
|
||||
loop
|
||||
fin
|
||||
if (decrec:exp + (decrec.sig - sigdigits)) < -numdigits or decrec:exp > 0 or expformat
|
||||
if sigdigits - decrec:exp > numdigits or decrec:exp > 0 or format & $04
|
||||
//
|
||||
// Convert to exponential format
|
||||
//
|
||||
|
@ -42,7 +42,7 @@ def shiftUp
|
||||
end
|
||||
def shiftDown
|
||||
stackRegs[0], stackRegs[1], stackRegs[2], stackRegs[3] = stackRegs[1], stackRegs[2], stackRegs[3], stackRegs[0]
|
||||
memset(stackRegs[3], 0, t_extended)
|
||||
memcpy(stackRegs[3], stackRegs[2], t_extended)
|
||||
end
|
||||
def dup
|
||||
stackRegs[0], stackRegs[1], stackRegs[2], stackRegs[3] = stackRegs[3], stackRegs[0], stackRegs[1], stackRegs[2]
|
||||
@ -154,9 +154,9 @@ def pushStr(pStr)
|
||||
str2ext(pStr, stackRegs[0])
|
||||
return sane:zpRestore()
|
||||
end
|
||||
def pullStr(pStr, sigdigits, expformat)
|
||||
def pullStr(pStr, intdigits, fracdigits, format)
|
||||
sane:zpSave()
|
||||
ext2str(stackRegs[0], pStr, sigdigits, expformat)
|
||||
ext2str(stackRegs[0], pStr, intdigits, fracdigits, format)
|
||||
sane:zpRestore()
|
||||
return shiftDown
|
||||
end
|
||||
@ -165,9 +165,9 @@ def loadStr(pStr, reg)
|
||||
str2ext(pStr, stackRegs[reg])
|
||||
return sane:zpRestore()
|
||||
end
|
||||
def storStr(pStr, sigdigits, expformat, reg)
|
||||
def storStr(pStr, intdigits, fracdigits, format, reg)
|
||||
sane:zpSave()
|
||||
ext2str(stackRegs[reg], pStr, sigdigits, expformat)
|
||||
ext2str(stackRegs[reg], pStr, intdigits, fracdigits, format)
|
||||
return sane:zpRestore()
|
||||
end
|
||||
//
|
||||
@ -254,10 +254,10 @@ end
|
||||
//
|
||||
def dumpStack#0
|
||||
byte r
|
||||
byte regStr[24]
|
||||
byte regStr[16]
|
||||
|
||||
for r = 3 downto 0
|
||||
storStr(@regStr, 8, 0, r)
|
||||
storStr(@regStr, 6, 4, 1, r)
|
||||
puts(" "); puti(r); putc(':'); puts(@regStr); putln
|
||||
next
|
||||
end
|
||||
|
@ -131,14 +131,15 @@ def str2ext(str, ext)
|
||||
//putc(decrec.sgn ?? '-' :: '+'); puts(@decrec.sig); putc('e'); puti(decrec:exp); putln
|
||||
return sane:fpOp2(FFEXT|FOD2B, ext, @decrec)
|
||||
end
|
||||
def ext2str(ext, str, numdigits, expformat)
|
||||
byte d, i, sigdigits
|
||||
def ext2str(ext, str, intdigits, fracdigits, format)
|
||||
byte d, i, sigdigits, numdigits
|
||||
word dp, tens
|
||||
byte decform[t_decformat]
|
||||
byte decrec[t_decrecord]
|
||||
|
||||
decform:style = 0
|
||||
decform:digits = numdigits
|
||||
numdigits = intdigits + fracdigits
|
||||
decform:style = format & $01
|
||||
decform:digits = decform:style ?? fracdigits :: numdigits
|
||||
sane:fpOp3(FFEXT|FOB2D, @decrec, ext, @decform)
|
||||
^(str+1) = decrec.sgn ?? '-' :: ' '
|
||||
if decrec.sig.1 == 'I'
|
||||
@ -156,21 +157,28 @@ def ext2str(ext, str, numdigits, expformat)
|
||||
return str
|
||||
fin
|
||||
if decrec.sig.1 == '0'
|
||||
while decrec.sig < fracdigits
|
||||
decrec.sig++
|
||||
decrec.sig[decrec.sig] = '0'
|
||||
loop
|
||||
decrec:exp = -decrec.sig
|
||||
fin
|
||||
dp = decrec.sig + decrec:exp
|
||||
sigdigits = decrec.sig
|
||||
//putc(decrec.sgn ?? '-' :: '+'); puts(@decrec.sig); putc('e'); puti(decrec:exp); putln
|
||||
if decrec:exp < 0
|
||||
if decrec:exp < 0 and format & $02
|
||||
//
|
||||
// Strip off trailing fractional zeros
|
||||
//
|
||||
while sigdigits > dp and decrec.sig[sigdigits] == '0'
|
||||
sigdigits--
|
||||
decrec:exp++
|
||||
loop
|
||||
fin
|
||||
//sane:zpRestore()
|
||||
//puts("sigdigits: "); puti(sigdigits); putln
|
||||
if (decrec:exp + (decrec.sig - sigdigits)) < -numdigits or decrec:exp > 0 or expformat
|
||||
//putc(decrec.sgn ?? '-' :: '+'); puts(@decrec.sig); putc('e'); puti(decrec:exp); putln
|
||||
//sane:zpSave()
|
||||
if sigdigits - decrec:exp > numdigits or decrec:exp > 0 or format & $04
|
||||
//
|
||||
// Convert to exponential format
|
||||
//
|
||||
@ -243,7 +251,7 @@ def ext2str(ext, str, numdigits, expformat)
|
||||
^str = i
|
||||
return str
|
||||
end
|
||||
def divstri(strNum, denom, expformat)#0
|
||||
def divstri(strNum, denom, format)#0
|
||||
byte strResult[20]
|
||||
byte xResult[t_extended]
|
||||
|
||||
@ -253,7 +261,7 @@ def divstri(strNum, denom, expformat)#0
|
||||
sane:zpSave()
|
||||
str2ext(strNum, @xResult)
|
||||
sane:fpOp2(FFINT|FODIV, @xResult, @denom) // Div int denom into ext Result
|
||||
ext2str(@xResult, @strResult, 6, expformat)
|
||||
ext2str(@xResult, @strResult, format & $05 ?? 1 :: 6, 4, format)
|
||||
sane:zpRestore()
|
||||
puts(strNum); putc('/'); puti(denom); putc('='); puts(@strResult); putln
|
||||
end
|
||||
@ -316,13 +324,14 @@ sane:zpRestore()
|
||||
//
|
||||
// String conversion tests
|
||||
//
|
||||
divstri("-3", 0, 0)
|
||||
divstri("3", 0, 0)
|
||||
divstri("-100.5", 4, 0)
|
||||
divstri("00.5", 2, 0)
|
||||
divstri(".5", 10, 0)
|
||||
divstri("800000", 2, 0)
|
||||
divstri("800000", 2, 1)
|
||||
divstri("1e+2", 2, 0)
|
||||
divstri("-1e-2", 2, 0)
|
||||
divstri("-3", 0, 2)
|
||||
divstri("3", 0, 2)
|
||||
divstri("-100.5", 4, 2)
|
||||
divstri("00.5", 2, 2)
|
||||
divstri(".5", 10, 3)
|
||||
divstri("800000", 2, 2)
|
||||
divstri("800000", 2, 4)
|
||||
divstri("1e+2", 2, 2)
|
||||
divstri("-1e-2", 2, 2)
|
||||
divstri("0", 1, 2)
|
||||
done
|
||||
|
Loading…
x
Reference in New Issue
Block a user