Difference: FloatingPointUnit (20 vs. 21)

Revision 212022-11-05 - PeterSchmid

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
%DASHBOARD{ section="banner"
Changed:
<
<
image="/twiki/pub/Cosmac/ForthSTM32WB/nucleo-header.jpg"
>
>
image="/twiki/pub/MecrispCube/FloatingPointUnit/Float_example-header.png"
  title="Floating-Point Unit (FPU)"
Changed:
<
<
titlestyle="color:#F00000;"
>
>
titlestyle="color:#ffffff;"
 }%
Intro

Why Floating-Point?

Line: 29 to 29
 

Floating-Point Unit

Added:
>
>
The STM32 ARM Cortex M4F MPUs (e.g. STM32WB, STM32F4, STM32L4) have a single precision floating-point unit. The STM32H7 MPUs have a double precision FPU (not supported yet).
 Also from STM AN4044

Floating-point calculations require a lot of resources, as for any operation between two

Line: 61 to 63
 

IEEE.754 Single and Double Precision Floating-Point Coding

Added:
>
>
 ieee-754.png

FPU's Dark Corners

Line: 79 to 84
 
Deleted:
<
<
  • atof() oder strtof() benutzen?

  • nur single-precision? (7..8 Dezimalstellen, H7 hat double precision FPU)
  • kein eigener Stack
  • auf float in ISR verzichten
  • _FPU_USED

>float in Gforth engine/support.c verwendet strtod() (strtof() for single precision)

Cell to_float(Char *c_addr, UCell u, Float *rp)
{
  /* convertible string := <significand>[<exponent>]
     <significand> := [<sign>]{<digits>[.<digits0>] | .<digits> }
     <exponent>    := <marker><digits0>
     <marker>      := {<e-form> | <sign-form>}
     <e-form>      := <e-char>[<sign-form>]
     <sign-form>   := { + | - }
     <e-char>      := { D | d | E | e }
  */
  Char *s = c_addr;
  Char c;
  Char *send = c_addr+u;
  UCell ndigits = 0;
  UCell ndots = 0;
  UCell edigits = 0;
  char cnum[u+3]; /* append at most "e0\0" */
  char *t=cnum;
  char *endconv;
  Float r;
  
  if (s >= send) /* treat empty string as 0e */
    goto return0;
  switch ((c=*s)) {
  case ' ':
    /* "A string of blanks should be treated as a special case
       representing zero."*/
    for (s++; s<send; )
      if (*s++ != ' ')
        goto error;
    goto return0;
  case '-':
  case '+': *t++ = c; s++; goto aftersign;
  }
  aftersign: 
  if (s >= send)
    goto exponent;
  switch (c=*s) {
  case '0' ... '9': *t++ = c; ndigits++; s++; goto aftersign;
  case '.':         *t++ = c; ndots++;   s++; goto aftersign;
  default:                                    goto exponent;
  }
 exponent:
  if (ndigits < 1 || ndots > 1)
    goto error;
  *t++ = 'E';
  if (s >= send)
    goto done;
  switch (c=*s) {
  case 'D':
  case 'd':
  case 'E':
  case 'e': s++; break;
  }
  if (s >= send)
    goto done;
  switch (c=*s) {
  case '+':
  case '-': *t++ = c; s++; break;
  }
 edigits0:
  if (s >= send)
    goto done;
  switch (c=*s) {
  case '0' ... '9': *t++ = c; s++; edigits++; goto edigits0;
  default: goto error;
  }
 done:
  if (edigits == 0)
    *t++ = '0';
  *t++ = '\0';
  assert(t-cnum <= u+3);
  r = strtod(cnum, &endconv);
  assert(*endconv == '\0');
  *rp = r;
  return -1;
 return0:
  *rp = 0.0;
  return -1;
 error:
  *rp = 0.0;
  return 0;
}
#endif
 

Floating-Point Words

Added:
>
>
No separate floating-point stack. A single precision floating-point number is one cell. The 32-bit base-2 format is officially referred to as binary32 IEEE 754-2008.
 

Bare FPU Words (Without C Math Library)


Line: 206 to 119
  f>s ( r -- n ) n is the single-cell signed-integer equivalent of the integer portion of r s>f ( n -- r ) r is the floating-point equivalent of the single-cell value n
Changed:
<
<
f>fx ( r -- d ) d is the fixed-point equivalent of the floating-point r fx>f ( d -- r ) r is the floating-point equivalent of the fixed-point d
>
>
f>fx ( r -- x ) x is the fixed-point equivalent of the floating-point r fx>f ( x -- r ) r is the floating-point equivalent of the fixed-point x
  pi ( -- r ) r is pi, approx. 3.14159274101257324 e ( -- r ) r is e, approx. 2.7182818
Line: 222 to 135
 set-precision ( u -- ) set the number of significant digits currently used by F., FE., or FS. to u
Deleted:
<
<

Fixed-Point Words

d+      ( d1 d2 -- d3 )     add d1 to d2 giving the sum d3
d-      ( d1 d2 -- d3 )     subtract d2 from d1, giving d3
fx*     ( d1 d2 -- d3 )     multiply d1 by d2 giving d3
fx/     ( d1 d2 -- d3 )     divide d1 by d2, giving the quotient d3
fx.     ( d --  )           display, with a trailing space, the fixed-point number d
fx.n 	( d n -- ) 	    print a fixed-point number with n fractional digits (truncated)
fx#S 	( n1 -- n2 ) 	    Adds 32 comma-digits to number output
fx# 	( n1 -- n2 ) 	    Adds one comma-digit to number output
 

Words Using C Math Library

Line: 242 to 144
 
Added:
>
>

Fixed-Point Words

Fixed-point numbers (s31.32) are stored ( n-comma n-whole ) and can be handled like signed double numbers. Because of the name conflict with the floating-point words I changed the names of the fixed-point word and use for fixed-point words x instead of f.

All angles are in degrees.

d+      ( x1 x2 -- x3 )     add x1 to x2 giving the sum x3
d-      ( x1 x2 -- x3 )     subtract x2 from x1, giving x3
x*      ( x1 x2 -- x3 )     multiply x1 by x2 giving x3
x/      ( x1 x2 -- x3 )     divide x1 by x2, giving the quotient x3
x.      ( x --  )           display, with a trailing space, the fixed-point number x
x.n 	( x n -- ) 	    print a fixed-point number x with n fractional digits (truncated)
x#S 	( n1 -- n2 ) 	    Adds 32 comma-digits to number output
x# 	( n1 -- n2 ) 	    Adds one comma-digit to number output

sqrt    ( x1 -- x2 )        x2 is the square root of x1
sin
cos
tan
asin
acos
atan
log2
log10
ln
pow2
pow10
exp

floor
deg2rad ( deg -- rad )
rad2deg ( rad -- deg )

pi
pi/2
pi/4
+inf
-inf

fixpt-mat-lib.fs
 

Line: 252 to 197
 Creative Commons License
This work by Peter Schmid is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

META FILEATTACHMENT attachment="ieee-754.png" attr="" comment="" date="1667389970" name="ieee-754.png" path="ieee-754.png" size="12998" user="PeterSchmid" version="1"
Added:
>
>
META FILEATTACHMENT attachment="Float_example-header.png" attr="" comment="" date="1667645143" name="Float_example-header.png" path="Float_example-header.png" size="28867" user="PeterSchmid" version="1"
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback