Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 608b42b

Browse files
committed
added missing
1 parent 3eff24f commit 608b42b

File tree

8 files changed

+1005
-1
lines changed

8 files changed

+1005
-1
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app

include/float12.hpp

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#include <stdbool.h>
2+
#include <stdint.h>
3+
#include "platform.h"
4+
#include "internals.h"
5+
#include "specialize.h"
6+
#include "softfloat.h"
7+
8+
struct ui12_f12
9+
{
10+
11+
};
12+
float12_t
13+
14+
#define packToF12UI( sign, exp, sig ) (((uint12_t) (sign)<<15) + ((uint12_t) (exp)<<10) + (sig))
15+
#define softfloat_commonNaNToF12UI(aPtr) (uint_fast12_t) ((aPtr)->sign<<15 | 0x7E00 | aPtr->v64>>54)
16+
17+
18+
int_float12_t
19+
uint_float12_t
20+
21+
// f8 is typically 1 4 3
22+
// f12 is typically 1 5 6 or 1 4 7
23+
// f16 is 1 6 9
24+
template <int exponentbits, int mantissabits, int mantissamask>
25+
float12_t f32_to_f12( float32_t a )
26+
{
27+
union ui32_f32 uA;
28+
uint_fast32_t uiA;
29+
bool sign;
30+
int_fast12_t exp;
31+
uint_fast32_t frac;
32+
struct commonNaN commonNaN;
33+
uint_fast12_t uiZ, frac16;
34+
union ui12_f12 uZ;
35+
36+
/*------------------------------------------------------------------------
37+
*------------------------------------------------------------------------*/
38+
uA.f = a;
39+
uiA = uA.ui;
40+
sign = signF32UI( uiA );
41+
exp = expF32UI( uiA );
42+
frac = fracF32UI( uiA );
43+
/*------------------------------------------------------------------------
44+
*------------------------------------------------------------------------*/
45+
if ( exp == 0xFF ) {
46+
if ( frac ) {
47+
softfloat_f32UIToCommonNaN( uiA, &commonNaN );
48+
uiZ = softfloat_commonNaNToF12UI( &commonNaN );
49+
} else {
50+
uiZ = packToF12UI( sign, 0x1F, 0 );
51+
}
52+
goto uiZ;
53+
}
54+
/*------------------------------------------------------------------------
55+
*------------------------------------------------------------------------*/
56+
frac16 = frac>> mantissabits| ((frac & mantissamask) != 0);
57+
if ( ! (exp | frac16) ) {
58+
uiZ = packToF12UI( sign, 0, 0 );
59+
goto uiZ;
60+
}
61+
/*------------------------------------------------------------------------
62+
*------------------------------------------------------------------------*/
63+
return softfloat_roundPackToF12( sign, exp - 0x71, frac16 | 0x4000 );
64+
uiZ:
65+
uZ.ui = uiZ;
66+
return uZ.f;
67+
68+
}
69+
70+
71+
72+
float12_t
73+
softfloat_roundPackToF12( bool sign, int_fast12_t exp, uint_fast12_t sig )
74+
{
75+
uint_fast8_t roundingMode;
76+
bool roundNearEven;
77+
uint_fast8_t roundIncrement, roundBits;
78+
bool isTiny;
79+
uint_fast12_t uiZ;
80+
union ui12_f12 uZ;
81+
82+
/*------------------------------------------------------------------------
83+
*------------------------------------------------------------------------*/
84+
roundingMode = softfloat_roundingMode;
85+
roundNearEven = (roundingMode == softfloat_round_near_even);
86+
roundIncrement = 0x8;
87+
if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
88+
roundIncrement =
89+
(roundingMode
90+
== (sign ? softfloat_round_min : softfloat_round_max))
91+
? 0xF
92+
: 0;
93+
}
94+
roundBits = sig & 0xF;
95+
/*------------------------------------------------------------------------
96+
*------------------------------------------------------------------------*/
97+
if ( 0x1D <= (unsigned int) exp ) {
98+
if ( exp < 0 ) {
99+
/*----------------------------------------------------------------
100+
*----------------------------------------------------------------*/
101+
isTiny =
102+
(softfloat_detectTininess == softfloat_tininess_beforeRounding)
103+
|| (exp < -1) || (sig + roundIncrement < 0x8000);
104+
sig = softfloat_shiftRightJam32( sig, -exp );
105+
exp = 0;
106+
roundBits = sig & 0xF;
107+
if ( isTiny && roundBits ) {
108+
softfloat_raiseFlags( softfloat_flag_underflow );
109+
}
110+
} else if ( (0x1D < exp) || (0x8000 <= sig + roundIncrement) ) {
111+
/*----------------------------------------------------------------
112+
*----------------------------------------------------------------*/
113+
softfloat_raiseFlags(
114+
softfloat_flag_overflow | softfloat_flag_inexact );
115+
uiZ = packToF12UI( sign, 0x1F, 0 ) - ! roundIncrement;
116+
goto uiZ;
117+
}
118+
}
119+
/*------------------------------------------------------------------------
120+
*------------------------------------------------------------------------*/
121+
sig = (sig + roundIncrement)>>4;
122+
if ( roundBits ) {
123+
softfloat_exceptionFlags |= softfloat_flag_inexact;
124+
#ifdef SOFTFLOAT_ROUND_ODD
125+
if ( roundingMode == softfloat_round_odd ) {
126+
sig |= 1;
127+
goto packReturn;
128+
}
129+
#endif
130+
}
131+
sig &= ~(uint_fast12_t) (! (roundBits ^ 8) & roundNearEven);
132+
if ( ! sig ) exp = 0;
133+
/*------------------------------------------------------------------------
134+
*------------------------------------------------------------------------*/
135+
packReturn:
136+
uiZ = packToF12UI( sign, exp, sig );
137+
uiZ:
138+
uZ.ui = uiZ;
139+
return uZ.f;
140+
141+
}
142+
143+
struct exp8_sig16 softfloat_normSubnormalF12Sig( uint_fast12_t sig )
144+
{
145+
int_fast8_t shiftDist;
146+
struct exp8_sig16 z;
147+
148+
shiftDist = softfloat_countLeadingZeros16( sig ) - 5; // TODO
149+
z.exp = 1 - shiftDist;
150+
z.sig = sig<<shiftDist;
151+
return z;
152+
153+
}
154+
155+
156+
void softfloat_f12UIToCommonNaN( uint_fast12_t uiA, struct commonNaN *zPtr )
157+
{
158+
if ( softfloat_isSigNaNF12UI( uiA ) ) {
159+
softfloat_raiseFlags( softfloat_flag_invalid );
160+
}
161+
zPtr->sign = uiA>>15;
162+
zPtr->v64 = (uint_fast64_t) uiA<<54;
163+
zPtr->v0 = 0;
164+
}
165+
166+
167+
float32_t f12_to_f32( float12_t a )
168+
{
169+
union ui12_f12 uA;
170+
uint_fast12_t uiA;
171+
bool sign;
172+
int_fast8_t exp;
173+
uint_fast12_t frac;
174+
struct commonNaN commonNaN;
175+
uint_fast32_t uiZ;
176+
struct exp8_sig16 normExpSig;
177+
union ui32_f32 uZ;
178+
179+
/*------------------------------------------------------------------------
180+
*------------------------------------------------------------------------*/
181+
uA.f = a;
182+
uiA = uA.ui;
183+
sign = signF12UI( uiA );
184+
exp = expF12UI( uiA );
185+
frac = fracF12UI( uiA );
186+
/*------------------------------------------------------------------------
187+
*------------------------------------------------------------------------*/
188+
if ( exp == 0x1F ) {
189+
if ( frac ) {
190+
softfloat_f12UIToCommonNaN( uiA, &commonNaN );
191+
uiZ = softfloat_commonNaNToF32UI( &commonNaN );
192+
} else {
193+
uiZ = packToF32UI( sign, 0xFF, 0 );
194+
}
195+
goto uiZ;
196+
}
197+
/*------------------------------------------------------------------------
198+
*------------------------------------------------------------------------*/
199+
if ( ! exp ) {
200+
if ( ! frac ) {
201+
uiZ = packToF32UI( sign, 0, 0 );
202+
goto uiZ;
203+
}
204+
normExpSig = softfloat_normSubnormalF12Sig( frac );
205+
exp = normExpSig.exp - 1;
206+
frac = normExpSig.sig;
207+
}
208+
/*------------------------------------------------------------------------
209+
*------------------------------------------------------------------------*/
210+
uiZ = packToF32UI( sign, exp + 0x70, (uint_fast32_t) frac<<13 );
211+
uiZ:
212+
uZ.ui = uiZ;
213+
return uZ.f;
214+
215+
}
216+
217+

include/float2posit.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
g++ -I. --std=c++14 float2posit.hpp -DTESTFLOAT2POSIT -c
55
*/
66
#include "posit.h"
7+
#include "floatconst2bits.hpp"
78

89
template <class T,int totalbits, int esbits, class FT, bool withnan, class Trait>
910
CONSTEXPR14 T float2posit(typename Trait::holder_t value)
@@ -84,5 +85,9 @@ T float2positF(typename Trait::value_t fvalue)
8485
#ifdef TESTFLOAT2POSIT
8586

8687
// import struct; print("%04X" % struct.unpack("I",struct.pack('f',3.5)))
87-
enum Q : int16_t { vv = float2posit<int16_t,16, 2, uint16_t, false, single_trait>(0x40600000)};
88+
enum Q : int16_t {
89+
v1 = float2posit<int16_t,16, 2, uint16_t, false, single_trait>(0x40600000),
90+
v2 = float2posit<int16_t,16, 2, uint16_t, false, single_trait>(float2bits(3.5f)),
91+
92+
};
8893
#endif

include/floatconst2bits.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#pragma once
2+
// http://brnz.org/hbr/?p=1518
3+
// Based on code from
4+
// https://graphics.stanford.edu/~seander/bithacks.html
5+
constexpr int count_leading_zeroes(uint64_t v)
6+
{
7+
constexpr char bit_position[64] = {
8+
0, 1, 2, 7, 3, 13, 8, 19, 4, 25, 14, 28, 9, 34, 20, 40,
9+
5, 17, 26, 38, 15, 46, 29, 48, 10, 31, 35, 54, 21, 50, 41, 57,
10+
63, 6, 12, 18, 24, 27, 33, 39, 16, 37, 45, 47, 30, 53, 49, 56,
11+
62, 11, 23, 32, 36, 44, 52, 55, 61, 22, 43, 51, 60, 42, 59, 58 };
12+
13+
v |= v >> 1; // first round down to one less than a power of 2
14+
v |= v >> 2;
15+
v |= v >> 4;
16+
v |= v >> 8;
17+
v |= v >> 16;
18+
v |= v >> 32;
19+
v = (v >> 1) + 1;
20+
21+
return 63 - bit_position[(v * 0x0218a392cd3d5dbf)>>58]; // [3]
22+
}
23+
24+
constexpr uint32_t float2bits(float f)
25+
{
26+
if (f == 0.0f)
27+
return 0; // also matches -0.0f and gives wrong result
28+
else if (f == INFINITY)
29+
return 0x7f800000;
30+
else if (f == -INFINITY)
31+
return 0xff800000;
32+
else if (f != f) // NaN
33+
return 0x7fc00000; // This is my NaN...
34+
35+
bool sign = f < 0.0f;
36+
float abs_f = sign ? -f : f;
37+
38+
int exponent = 254;
39+
40+
while(abs_f < 0x1p87f)
41+
{
42+
abs_f *= 0x1p41f;
43+
exponent -= 41;
44+
}
45+
46+
uint64_t a = (uint64_t)(abs_f * 0x1p-64f);
47+
int lz = count_leading_zeroes(a);
48+
exponent -= lz;
49+
50+
if (exponent <= 0)
51+
{
52+
exponent = 0;
53+
lz = 8 - 1;
54+
}
55+
56+
uint32_t significand = (a << (lz + 1)) >> (64 - 23); // [3]
57+
return (sign << 31) | (exponent << 23) | significand;
58+
}

matlab/Lenna.png

463 KB
Loading

scripts/listposits8_16bin.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
SCRIPT_HOME=`dirname $0 | while read a; do cd $a && pwd && break; done`
3+
XX="-DLISTFORMAT "
4+
listme() {
5+
$SCRIPT_HOME/listpositany.sh $1 -DLISTFORMAT=Biov
6+
mv tmp "posit$1.bin"
7+
}
8+
9+
listme int8_t,8,0,uint16_t,false
10+
listme int8_t,8,0,uint16_t,true
11+
listme int8_t,8,1,uint16_t,false
12+
listme int8_t,8,1,uint16_t,true
13+
listme int8_t,8,2,uint16_t,false
14+
listme int8_t,8,2,uint16_t,true
15+
listme int8_t,8,3,uint16_t,false
16+
listme int8_t,8,3,uint16_t,true
17+
listme int16_t,12,0,uint16_t,false
18+
listme int16_t,12,0,uint16_t,true
19+
listme int16_t,12,1,uint16_t,false
20+
listme int16_t,12,1,uint16_t,true
21+
listme int16_t,12,2,uint16_t,false
22+
listme int16_t,12,2,uint16_t,true
23+
listme int16_t,12,3,uint16_t,false
24+
listme int16_t,12,3,uint16_t,true
25+
listme int16_t,12,4,uint16_t,false
26+
listme int16_t,12,4,uint16_t,true
27+
listme int16_t,10,0,uint16_t,false
28+
listme int16_t,10,0,uint16_t,true
29+
listme int16_t,10,2,uint16_t,false
30+
listme int16_t,10,1,uint16_t,true
31+
listme int16_t,10,1,uint16_t,false
32+
listme int16_t,10,2,uint16_t,true
33+
listme int16_t,10,3,uint16_t,true
34+
listme int16_t,10,3,uint16_t,false
35+
listme int16_t,16,0,uint16_t,false
36+
listme int16_t,16,0,uint16_t,true

0 commit comments

Comments
 (0)