Skip to content

Commit

Permalink
Version 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
PsychoCod3r committed Oct 17, 2020
1 parent 3fab746 commit c5b2f3d
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 13 deletions.
142 changes: 142 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
## Documentation for libdfloat

### Data Types:

libdfloat defines the following data types:

`dfloat16_t`: 16-bit decimal float with 8-bit mantissa and exponent

`dfloat32_t`: 32-bit decimal float with 16-bit mantissa and exponent

`dfloat64_t`: 64-bit decimal float with 32-bit mantissa and exponent

`dfloat128_t`: 128-bit decimal float with 64-bit mantissa and exponent

The proper way to declare a `dfloat` is to specify a decimal literal in
string format and convert it to a `dfloat` using one of the `dfloatN_atof()`
functions.

---------------------------------------------------------------------------

### Standard Functions:

In the following function definitions, the capital letters *M* and *N* stand
for integers, either 16, 32, 64, or 128. These numbers can be substituted
as desired, as there are macros that generate the corresponding functions.

(Example: `dfloat64_add()`, no spaces)

libdfloat defines the following groups of functions:

`void dfloatN_add( dfloatN_t *dst, dfloatN_t *src )`

Add *N*-bit `src` and `dst` operands together and store the result in `dst`.

`void dfloatN_sub( dfloatN_t *dst, dfloatN_t *src )`

Subtract *N*-bit `src` from *N*-bit `dst` and store the result in `dst`.

`void dfloatN_mul( dfloatN_t *dst, dfloatN_t *src )`

Multiply *N*-bit `src` and `dst` operands together and store the result in `dst`.

`void dfloatN_div( dfloatN_t *dst, dfloatN_t *src, int precision )`

Divide *N*-bit `dst` by `src` and store the result in `src`, with up to `precision`
digits past the decimal point.

`int dfloatN_cmp( dfloatN_t *df1, dfloatN_t *df2 )`

Compares *N*-bit operands `df1` and `df2`.

Returns:

`1` if `df1 > df2`

`-1` if `df1 < df2`

`0` if `df1 == df2`

`dfloatN_t *dfloatN_atof( char *str )`

Reads an *N*-bit `dfloat` from input string `str`.

Input string must be in the following format (BNF):

`[-]<digit><digit>*[.<digit><digits>*]`

`char *dfloatN_ftoa( dfloatN_t *df )`

Generates a string representation of *N*-bit `dfloat` value `df`.

`void dfloatN_cpy( dfloatN_t *dst, dfloatN_t *src )`

Copies the mantissa and exponent from `src` to `dst`.

`dfloatN_t *dfloatM_castN( dfloatM *src )`

Takes a `dfloat` of size `M` and typecasts it, returning a `dfloat` of size `N`.

Cast functions where `M == N` have not been implemented as the author
saw no need for such functions.

*Note: Overflow errors may occur if the result of an operation is too
large to fit into the designated space. These will not be reported by
the compiler and will simply result in incorrect values. It is up to
the programmer to account for these overflows.*

---------------------------------------------------------------------------

### Free Functions:

As of Version 0.2, libdfloat has versions of some of the above functions
that free their source operands, allowing the user to build more complex
expressions using these functions without having to worry about lost
objects accumulating. These "free versions" are as follows:

`dfloatN_t *dfloatN_addf( dfloatN_t *arg1, dfloatN_t *arg2 )`

Adds `arg1` and `arg2` and returns the result

`dfloatN_t *dfloatN_subf( dfloatN_t *arg1, dfloatN_t *arg2 )`

Subtracts `arg2` from `arg1` and returns the result

`dfloatN_t *dfloatN_mulf( dfloatN_t *arg1, dfloatN_t *arg2 )`

Multiplies `arg1` by `arg2` and returns the result

`dfloatN_t *dfloatN_divf( dfloatN_t *arg1, dfloatN_t *arg2, int precision )`

Divides `arg1` by `arg2` with the given precision and returns the result

`int dfloatN_cmpf( dfloatN_t *arg1, dfloatN_t *arg2 )`

Like `dfloatN_cmp()`, but frees the source operands

`char *dfloatN_ftoaf( dfloat_t *src )`

Like `dfloatN_ftoa()`, but frees the source operand

`dfloatN_t *dfloatM_castNf( dfloatM_t *src )`

Like `dfloatM_castN()`, but frees the source operand

*Note: Because these functions free their operands, they should not be
used on variables. They should only be used on immediate operands,
where the output of one function is directly supplied as a parameter to
another.*

Example:

`dfloat64_t *sum = dfloat64_addf( dfloat64_atof( "1.2" ), dfloat64_atof( "3.4" ) );`

--------------------------------------------------------------------------

Any questions or problems? Feel free to contact me at the following:

Github: github.com/PsychoCod3r

Personal email: acidkicks@protonmail.com

Submit issues at github.com/PsychoCod3r/libdfloat
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,24 @@ decimal base.

**Files included in this repository:**

- README: This file
- README.md - this file

- LICENSE - the Michael Warren Free Software License under which this and
all my other software is released
- LICENSE.md - the Michael Warren Free Software License under which this
and all my other software is released

- CoC.md - the Apolitical Code of Conduct which is used for this project

- TODO.md - list of modifications I want to make to this software in
the future

- DOC.md - Documentation of all types and functions defined in libdfloat

- dfloat.h: Header file containing typedefs and function prototypes

- dfloat.c: C module containing all function definitions
- dfloat.c: C module containing standard dfloat functions

- doc.txt: Documentation for all types and functions defined in libdfloat
- dfloat_free.c: dfloat functions that free their source operands before
returning, written to facilitate more complex expressions

- change.log: Log of changes made with each release of libdfloat

Expand All @@ -65,9 +73,9 @@ None

2. Run the following commands:

`gcc -c dfloat.c`
`gcc -c dfloat.c dfloat_free.c`

`ar -rsv libdfloat.a dfloat.o`
`ar -rsv libdfloat.a dfloat.o dfloat_free.o`

3. To link the libdfloat library to a project, run the following command:

Expand Down
6 changes: 3 additions & 3 deletions dfloat.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/****************************************************
* libdfloat, version 0.1.2 Alpha *
* libdfloat, version 0.2 Alpha *
* Description: Implements floating point numbers *
* with exact decimal representations *
* Current file: All libdfloat function definitions *
* Author: Michael Warren, a.k.a. Psycho Cod3r *
* Date: October 2020 *
* License: Micheal Warren FSL *
* License: Micheal Warren FSL v1.1 *
* Current module: Standard dfloat functions *
****************************************************/

#include <stdio.h>
Expand Down
45 changes: 42 additions & 3 deletions dfloat.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/****************************************************
* libdfloat, version 0.1.2 Alpha *
* libdfloat, version 0.2 Alpha *
* Description: Implements floating point numbers *
* with exact decimal representations *
* Current file: Header file for entire project *
* Author: Michael Warren, a.k.a. Psycho Cod3r *
* Date: October 2020 *
* License: Micheal Warren FSL *
* License: Micheal Warren FSL v1.1 *
* Current module: Header file for entire project *
****************************************************/

#ifndef _DFLOAT_H_
Expand Down Expand Up @@ -34,6 +34,7 @@ typedef struct {
} dfloat128_t;

__BEGIN_DECLS
// Functions defined in dfloat.c:
void dfloat16_add( dfloat16_t *, dfloat16_t * );
void dfloat16_sub( dfloat16_t *, dfloat16_t * );
void dfloat16_mul( dfloat16_t *, dfloat16_t * );
Expand Down Expand Up @@ -78,6 +79,44 @@ dfloat32_t *dfloat128_cast32( dfloat128_t * );
dfloat64_t *dfloat128_cast64( dfloat128_t * );
dfloat128_t *dfloat128_atof( char * );
char *dfloat128_ftoa( dfloat128_t * );

// Functions defined in dfloat_free.c:
dfloat16_t *dfloat16_addf( dfloat16_t *, dfloat16_t * );
dfloat16_t *dfloat16_subf( dfloat16_t *, dfloat16_t * );
dfloat16_t *dfloat16_mulf( dfloat16_t *, dfloat16_t * );
dfloat16_t *dfloat16_divf( dfloat16_t *, dfloat16_t *, int );
int dfloat16_cmpf( dfloat16_t *, dfloat16_t * );
dfloat32_t *dfloat16_cast32f( dfloat16_t * );
dfloat64_t *dfloat16_cast64f( dfloat16_t * );
dfloat128_t *dfloat16_cast128f( dfloat16_t * );
char *dfloat16_ftoaf( dfloat16_t * );
dfloat32_t *dfloat32_addf( dfloat32_t *, dfloat32_t * );
dfloat32_t *dfloat32_subf( dfloat32_t *, dfloat32_t * );
dfloat32_t *dfloat32_mulf( dfloat32_t *, dfloat32_t * );
dfloat32_t *dfloat32_divf( dfloat32_t *, dfloat32_t *, int );
int dfloat32_cmpf( dfloat32_t *, dfloat32_t * );
dfloat16_t *dfloat32_cast16f( dfloat32_t * );
dfloat64_t *dfloat32_cast64f( dfloat32_t * );
dfloat128_t *dfloat32_cast128f( dfloat32_t * );
char *dfloat32_ftoaf( dfloat32_t * );
dfloat64_t *dfloat64_addf( dfloat64_t *, dfloat64_t * );
dfloat64_t *dfloat64_subf( dfloat64_t *, dfloat64_t * );
dfloat64_t *dfloat64_mulf( dfloat64_t *, dfloat64_t * );
dfloat64_t *dfloat64_divf( dfloat64_t *, dfloat64_t *, int );
int dfloat64_cmpf( dfloat64_t *, dfloat64_t * );
dfloat16_t *dfloat64_cast16f( dfloat64_t * );
dfloat32_t *dfloat64_cast32f( dfloat64_t * );
dfloat128_t *dfloat64_cast128f( dfloat64_t * );
char *dfloat64_ftoaf( dfloat64_t * );
dfloat128_t *dfloat128_addf( dfloat128_t *, dfloat128_t * );
dfloat128_t *dfloat128_subf( dfloat128_t *, dfloat128_t * );
dfloat128_t *dfloat128_mulf( dfloat128_t *, dfloat128_t * );
dfloat128_t *dfloat128_divf( dfloat128_t *, dfloat128_t *, int );
int dfloat128_cmpf( dfloat128_t *, dfloat128_t * );
dfloat16_t *dfloat128_cast16f( dfloat128_t * );
dfloat32_t *dfloat128_cast32f( dfloat128_t * );
dfloat64_t *dfloat128_cast64f( dfloat128_t * );
char *dfloat128_ftoaf( dfloat128_t * );
__END_DECLS

#endif
114 changes: 114 additions & 0 deletions dfloat_free.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/****************************************************
* libdfloat, version 0.2 Alpha *
* Description: Implements floating point numbers *
* with exact decimal representations *
* Author: Michael Warren, a.k.a. Psycho Cod3r *
* Date: October 2020 *
* License: Micheal Warren FSL v1.1 *
* Current module: Function versions that free the *
* source operand, making complex *
* expressions easier *
****************************************************/

#include <stdlib.h>
#include "dfloat.h"

#define dfloatM_castNf( M, N )\
dfloat ## N ## _t *dfloat ## M ## _cast ## N ## f( dfloat ## M ## _t *src ){\
dfloat ## N ## _t *dst;\
dst = dfloat ## M ## _cast ## N ( src );\
free( src );\
return dst;\
}

dfloatM_castNf( 16, 32 )
dfloatM_castNf( 16, 64 )
dfloatM_castNf( 16, 128 )

dfloatM_castNf( 32, 16 )
dfloatM_castNf( 32, 64 )
dfloatM_castNf( 32, 128 )

dfloatM_castNf( 64, 16 )
dfloatM_castNf( 64, 32 )
dfloatM_castNf( 64, 128 )

dfloatM_castNf( 128, 16 )
dfloatM_castNf( 128, 32 )
dfloatM_castNf( 128, 64 )

#define dfloatN_addf( N )\
dfloat ## N ## _t *dfloat ## N ## _addf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2 ){\
dfloat ## N ## _add( arg1, arg2 );\
free( arg2 );\
return arg1;\
}

dfloatN_addf( 16 )
dfloatN_addf( 32 )
dfloatN_addf( 64 )
dfloatN_addf( 128 )

#define dfloatN_subf( N )\
dfloat ## N ## _t *dfloat ## N ## _subf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2 ){\
dfloat ## N ## _sub( arg1, arg2 );\
free( arg2 );\
return arg1;\
}

dfloatN_subf( 16 )
dfloatN_subf( 32 )
dfloatN_subf( 64 )
dfloatN_subf( 128 )


#define dfloatN_mulf( N )\
dfloat ## N ## _t *dfloat ## N ## _mulf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2 ){\
dfloat ## N ## _mul( arg1, arg2 );\
free( arg2 );\
return arg1;\
}

dfloatN_mulf( 16 )
dfloatN_mulf( 32 )
dfloatN_mulf( 64 )
dfloatN_mulf( 128 )

#define dfloatN_divf( N )\
dfloat ## N ## _t *dfloat ## N ## _divf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2, int precision ){\
dfloat ## N ## _div( arg1, arg2, precision );\
free( arg2 );\
return arg1;\
}

dfloatN_divf( 16 )
dfloatN_divf( 32 )
dfloatN_divf( 64 )
dfloatN_divf( 128 )

#define dfloatN_cmpf( N )\
int dfloat ## N ## _cmpf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2 ){\
int result;\
result = dfloat ## N ## _cmp( arg1, arg2 );\
free( arg1 );\
free( arg2 );\
return result;\
}

dfloatN_cmpf( 16 )
dfloatN_cmpf( 32 )
dfloatN_cmpf( 64 )
dfloatN_cmpf( 128 )

#define dfloatN_ftoaf( N )\
char *dfloat ## N ## _ftoaf( dfloat ## N ## _t *src ){\
char *dst;\
dst = dfloat ## N ## _ftoa( src );\
free( src );\
return dst;\
}

dfloatN_ftoaf( 16 )
dfloatN_ftoaf( 32 )
dfloatN_ftoaf( 64 )
dfloatN_ftoaf( 128 )
Loading

0 comments on commit c5b2f3d

Please sign in to comment.