Skip to content

Commit c5b2f3d

Browse files
authored
Version 0.2
1 parent 3fab746 commit c5b2f3d

File tree

6 files changed

+354
-13
lines changed

6 files changed

+354
-13
lines changed

DOC.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
## Documentation for libdfloat
2+
3+
### Data Types:
4+
5+
libdfloat defines the following data types:
6+
7+
`dfloat16_t`: 16-bit decimal float with 8-bit mantissa and exponent
8+
9+
`dfloat32_t`: 32-bit decimal float with 16-bit mantissa and exponent
10+
11+
`dfloat64_t`: 64-bit decimal float with 32-bit mantissa and exponent
12+
13+
`dfloat128_t`: 128-bit decimal float with 64-bit mantissa and exponent
14+
15+
The proper way to declare a `dfloat` is to specify a decimal literal in
16+
string format and convert it to a `dfloat` using one of the `dfloatN_atof()`
17+
functions.
18+
19+
---------------------------------------------------------------------------
20+
21+
### Standard Functions:
22+
23+
In the following function definitions, the capital letters *M* and *N* stand
24+
for integers, either 16, 32, 64, or 128. These numbers can be substituted
25+
as desired, as there are macros that generate the corresponding functions.
26+
27+
(Example: `dfloat64_add()`, no spaces)
28+
29+
libdfloat defines the following groups of functions:
30+
31+
`void dfloatN_add( dfloatN_t *dst, dfloatN_t *src )`
32+
33+
Add *N*-bit `src` and `dst` operands together and store the result in `dst`.
34+
35+
`void dfloatN_sub( dfloatN_t *dst, dfloatN_t *src )`
36+
37+
Subtract *N*-bit `src` from *N*-bit `dst` and store the result in `dst`.
38+
39+
`void dfloatN_mul( dfloatN_t *dst, dfloatN_t *src )`
40+
41+
Multiply *N*-bit `src` and `dst` operands together and store the result in `dst`.
42+
43+
`void dfloatN_div( dfloatN_t *dst, dfloatN_t *src, int precision )`
44+
45+
Divide *N*-bit `dst` by `src` and store the result in `src`, with up to `precision`
46+
digits past the decimal point.
47+
48+
`int dfloatN_cmp( dfloatN_t *df1, dfloatN_t *df2 )`
49+
50+
Compares *N*-bit operands `df1` and `df2`.
51+
52+
Returns:
53+
54+
`1` if `df1 > df2`
55+
56+
`-1` if `df1 < df2`
57+
58+
`0` if `df1 == df2`
59+
60+
`dfloatN_t *dfloatN_atof( char *str )`
61+
62+
Reads an *N*-bit `dfloat` from input string `str`.
63+
64+
Input string must be in the following format (BNF):
65+
66+
`[-]<digit><digit>*[.<digit><digits>*]`
67+
68+
`char *dfloatN_ftoa( dfloatN_t *df )`
69+
70+
Generates a string representation of *N*-bit `dfloat` value `df`.
71+
72+
`void dfloatN_cpy( dfloatN_t *dst, dfloatN_t *src )`
73+
74+
Copies the mantissa and exponent from `src` to `dst`.
75+
76+
`dfloatN_t *dfloatM_castN( dfloatM *src )`
77+
78+
Takes a `dfloat` of size `M` and typecasts it, returning a `dfloat` of size `N`.
79+
80+
Cast functions where `M == N` have not been implemented as the author
81+
saw no need for such functions.
82+
83+
*Note: Overflow errors may occur if the result of an operation is too
84+
large to fit into the designated space. These will not be reported by
85+
the compiler and will simply result in incorrect values. It is up to
86+
the programmer to account for these overflows.*
87+
88+
---------------------------------------------------------------------------
89+
90+
### Free Functions:
91+
92+
As of Version 0.2, libdfloat has versions of some of the above functions
93+
that free their source operands, allowing the user to build more complex
94+
expressions using these functions without having to worry about lost
95+
objects accumulating. These "free versions" are as follows:
96+
97+
`dfloatN_t *dfloatN_addf( dfloatN_t *arg1, dfloatN_t *arg2 )`
98+
99+
Adds `arg1` and `arg2` and returns the result
100+
101+
`dfloatN_t *dfloatN_subf( dfloatN_t *arg1, dfloatN_t *arg2 )`
102+
103+
Subtracts `arg2` from `arg1` and returns the result
104+
105+
`dfloatN_t *dfloatN_mulf( dfloatN_t *arg1, dfloatN_t *arg2 )`
106+
107+
Multiplies `arg1` by `arg2` and returns the result
108+
109+
`dfloatN_t *dfloatN_divf( dfloatN_t *arg1, dfloatN_t *arg2, int precision )`
110+
111+
Divides `arg1` by `arg2` with the given precision and returns the result
112+
113+
`int dfloatN_cmpf( dfloatN_t *arg1, dfloatN_t *arg2 )`
114+
115+
Like `dfloatN_cmp()`, but frees the source operands
116+
117+
`char *dfloatN_ftoaf( dfloat_t *src )`
118+
119+
Like `dfloatN_ftoa()`, but frees the source operand
120+
121+
`dfloatN_t *dfloatM_castNf( dfloatM_t *src )`
122+
123+
Like `dfloatM_castN()`, but frees the source operand
124+
125+
*Note: Because these functions free their operands, they should not be
126+
used on variables. They should only be used on immediate operands,
127+
where the output of one function is directly supplied as a parameter to
128+
another.*
129+
130+
Example:
131+
132+
`dfloat64_t *sum = dfloat64_addf( dfloat64_atof( "1.2" ), dfloat64_atof( "3.4" ) );`
133+
134+
--------------------------------------------------------------------------
135+
136+
Any questions or problems? Feel free to contact me at the following:
137+
138+
Github: github.com/PsychoCod3r
139+
140+
Personal email: [email protected]
141+
142+
Submit issues at github.com/PsychoCod3r/libdfloat

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,24 @@ decimal base.
3636

3737
**Files included in this repository:**
3838

39-
- README: This file
39+
- README.md - this file
4040

41-
- LICENSE - the Michael Warren Free Software License under which this and
42-
all my other software is released
41+
- LICENSE.md - the Michael Warren Free Software License under which this
42+
and all my other software is released
43+
44+
- CoC.md - the Apolitical Code of Conduct which is used for this project
45+
46+
- TODO.md - list of modifications I want to make to this software in
47+
the future
48+
49+
- DOC.md - Documentation of all types and functions defined in libdfloat
4350

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

46-
- dfloat.c: C module containing all function definitions
53+
- dfloat.c: C module containing standard dfloat functions
4754

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

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

@@ -65,9 +73,9 @@ None
6573

6674
2. Run the following commands:
6775

68-
`gcc -c dfloat.c`
76+
`gcc -c dfloat.c dfloat_free.c`
6977

70-
`ar -rsv libdfloat.a dfloat.o`
78+
`ar -rsv libdfloat.a dfloat.o dfloat_free.o`
7179

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

dfloat.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/****************************************************
2-
* libdfloat, version 0.1.2 Alpha *
2+
* libdfloat, version 0.2 Alpha *
33
* Description: Implements floating point numbers *
44
* with exact decimal representations *
5-
* Current file: All libdfloat function definitions *
65
* Author: Michael Warren, a.k.a. Psycho Cod3r *
76
* Date: October 2020 *
8-
* License: Micheal Warren FSL *
7+
* License: Micheal Warren FSL v1.1 *
8+
* Current module: Standard dfloat functions *
99
****************************************************/
1010

1111
#include <stdio.h>

dfloat.h

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/****************************************************
2-
* libdfloat, version 0.1.2 Alpha *
2+
* libdfloat, version 0.2 Alpha *
33
* Description: Implements floating point numbers *
44
* with exact decimal representations *
5-
* Current file: Header file for entire project *
65
* Author: Michael Warren, a.k.a. Psycho Cod3r *
76
* Date: October 2020 *
8-
* License: Micheal Warren FSL *
7+
* License: Micheal Warren FSL v1.1 *
8+
* Current module: Header file for entire project *
99
****************************************************/
1010

1111
#ifndef _DFLOAT_H_
@@ -34,6 +34,7 @@ typedef struct {
3434
} dfloat128_t;
3535

3636
__BEGIN_DECLS
37+
// Functions defined in dfloat.c:
3738
void dfloat16_add( dfloat16_t *, dfloat16_t * );
3839
void dfloat16_sub( dfloat16_t *, dfloat16_t * );
3940
void dfloat16_mul( dfloat16_t *, dfloat16_t * );
@@ -78,6 +79,44 @@ dfloat32_t *dfloat128_cast32( dfloat128_t * );
7879
dfloat64_t *dfloat128_cast64( dfloat128_t * );
7980
dfloat128_t *dfloat128_atof( char * );
8081
char *dfloat128_ftoa( dfloat128_t * );
82+
83+
// Functions defined in dfloat_free.c:
84+
dfloat16_t *dfloat16_addf( dfloat16_t *, dfloat16_t * );
85+
dfloat16_t *dfloat16_subf( dfloat16_t *, dfloat16_t * );
86+
dfloat16_t *dfloat16_mulf( dfloat16_t *, dfloat16_t * );
87+
dfloat16_t *dfloat16_divf( dfloat16_t *, dfloat16_t *, int );
88+
int dfloat16_cmpf( dfloat16_t *, dfloat16_t * );
89+
dfloat32_t *dfloat16_cast32f( dfloat16_t * );
90+
dfloat64_t *dfloat16_cast64f( dfloat16_t * );
91+
dfloat128_t *dfloat16_cast128f( dfloat16_t * );
92+
char *dfloat16_ftoaf( dfloat16_t * );
93+
dfloat32_t *dfloat32_addf( dfloat32_t *, dfloat32_t * );
94+
dfloat32_t *dfloat32_subf( dfloat32_t *, dfloat32_t * );
95+
dfloat32_t *dfloat32_mulf( dfloat32_t *, dfloat32_t * );
96+
dfloat32_t *dfloat32_divf( dfloat32_t *, dfloat32_t *, int );
97+
int dfloat32_cmpf( dfloat32_t *, dfloat32_t * );
98+
dfloat16_t *dfloat32_cast16f( dfloat32_t * );
99+
dfloat64_t *dfloat32_cast64f( dfloat32_t * );
100+
dfloat128_t *dfloat32_cast128f( dfloat32_t * );
101+
char *dfloat32_ftoaf( dfloat32_t * );
102+
dfloat64_t *dfloat64_addf( dfloat64_t *, dfloat64_t * );
103+
dfloat64_t *dfloat64_subf( dfloat64_t *, dfloat64_t * );
104+
dfloat64_t *dfloat64_mulf( dfloat64_t *, dfloat64_t * );
105+
dfloat64_t *dfloat64_divf( dfloat64_t *, dfloat64_t *, int );
106+
int dfloat64_cmpf( dfloat64_t *, dfloat64_t * );
107+
dfloat16_t *dfloat64_cast16f( dfloat64_t * );
108+
dfloat32_t *dfloat64_cast32f( dfloat64_t * );
109+
dfloat128_t *dfloat64_cast128f( dfloat64_t * );
110+
char *dfloat64_ftoaf( dfloat64_t * );
111+
dfloat128_t *dfloat128_addf( dfloat128_t *, dfloat128_t * );
112+
dfloat128_t *dfloat128_subf( dfloat128_t *, dfloat128_t * );
113+
dfloat128_t *dfloat128_mulf( dfloat128_t *, dfloat128_t * );
114+
dfloat128_t *dfloat128_divf( dfloat128_t *, dfloat128_t *, int );
115+
int dfloat128_cmpf( dfloat128_t *, dfloat128_t * );
116+
dfloat16_t *dfloat128_cast16f( dfloat128_t * );
117+
dfloat32_t *dfloat128_cast32f( dfloat128_t * );
118+
dfloat64_t *dfloat128_cast64f( dfloat128_t * );
119+
char *dfloat128_ftoaf( dfloat128_t * );
81120
__END_DECLS
82121

83122
#endif

dfloat_free.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/****************************************************
2+
* libdfloat, version 0.2 Alpha *
3+
* Description: Implements floating point numbers *
4+
* with exact decimal representations *
5+
* Author: Michael Warren, a.k.a. Psycho Cod3r *
6+
* Date: October 2020 *
7+
* License: Micheal Warren FSL v1.1 *
8+
* Current module: Function versions that free the *
9+
* source operand, making complex *
10+
* expressions easier *
11+
****************************************************/
12+
13+
#include <stdlib.h>
14+
#include "dfloat.h"
15+
16+
#define dfloatM_castNf( M, N )\
17+
dfloat ## N ## _t *dfloat ## M ## _cast ## N ## f( dfloat ## M ## _t *src ){\
18+
dfloat ## N ## _t *dst;\
19+
dst = dfloat ## M ## _cast ## N ( src );\
20+
free( src );\
21+
return dst;\
22+
}
23+
24+
dfloatM_castNf( 16, 32 )
25+
dfloatM_castNf( 16, 64 )
26+
dfloatM_castNf( 16, 128 )
27+
28+
dfloatM_castNf( 32, 16 )
29+
dfloatM_castNf( 32, 64 )
30+
dfloatM_castNf( 32, 128 )
31+
32+
dfloatM_castNf( 64, 16 )
33+
dfloatM_castNf( 64, 32 )
34+
dfloatM_castNf( 64, 128 )
35+
36+
dfloatM_castNf( 128, 16 )
37+
dfloatM_castNf( 128, 32 )
38+
dfloatM_castNf( 128, 64 )
39+
40+
#define dfloatN_addf( N )\
41+
dfloat ## N ## _t *dfloat ## N ## _addf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2 ){\
42+
dfloat ## N ## _add( arg1, arg2 );\
43+
free( arg2 );\
44+
return arg1;\
45+
}
46+
47+
dfloatN_addf( 16 )
48+
dfloatN_addf( 32 )
49+
dfloatN_addf( 64 )
50+
dfloatN_addf( 128 )
51+
52+
#define dfloatN_subf( N )\
53+
dfloat ## N ## _t *dfloat ## N ## _subf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2 ){\
54+
dfloat ## N ## _sub( arg1, arg2 );\
55+
free( arg2 );\
56+
return arg1;\
57+
}
58+
59+
dfloatN_subf( 16 )
60+
dfloatN_subf( 32 )
61+
dfloatN_subf( 64 )
62+
dfloatN_subf( 128 )
63+
64+
65+
#define dfloatN_mulf( N )\
66+
dfloat ## N ## _t *dfloat ## N ## _mulf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2 ){\
67+
dfloat ## N ## _mul( arg1, arg2 );\
68+
free( arg2 );\
69+
return arg1;\
70+
}
71+
72+
dfloatN_mulf( 16 )
73+
dfloatN_mulf( 32 )
74+
dfloatN_mulf( 64 )
75+
dfloatN_mulf( 128 )
76+
77+
#define dfloatN_divf( N )\
78+
dfloat ## N ## _t *dfloat ## N ## _divf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2, int precision ){\
79+
dfloat ## N ## _div( arg1, arg2, precision );\
80+
free( arg2 );\
81+
return arg1;\
82+
}
83+
84+
dfloatN_divf( 16 )
85+
dfloatN_divf( 32 )
86+
dfloatN_divf( 64 )
87+
dfloatN_divf( 128 )
88+
89+
#define dfloatN_cmpf( N )\
90+
int dfloat ## N ## _cmpf( dfloat ## N ## _t *arg1, dfloat ## N ## _t *arg2 ){\
91+
int result;\
92+
result = dfloat ## N ## _cmp( arg1, arg2 );\
93+
free( arg1 );\
94+
free( arg2 );\
95+
return result;\
96+
}
97+
98+
dfloatN_cmpf( 16 )
99+
dfloatN_cmpf( 32 )
100+
dfloatN_cmpf( 64 )
101+
dfloatN_cmpf( 128 )
102+
103+
#define dfloatN_ftoaf( N )\
104+
char *dfloat ## N ## _ftoaf( dfloat ## N ## _t *src ){\
105+
char *dst;\
106+
dst = dfloat ## N ## _ftoa( src );\
107+
free( src );\
108+
return dst;\
109+
}
110+
111+
dfloatN_ftoaf( 16 )
112+
dfloatN_ftoaf( 32 )
113+
dfloatN_ftoaf( 64 )
114+
dfloatN_ftoaf( 128 )

0 commit comments

Comments
 (0)