Skip to content

Commit cb4644b

Browse files
committed
move abs() to stdlib.h where it is supposed to be
1 parent 0ba8f60 commit cb4644b

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

source/twr-stdclib/include/math.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
extern "C" {
66
#endif
77

8-
int abs(int n);
98
double acos(double arg);
109
double asin(double arg);
1110
double atan(double arg);

source/twr-stdclib/include/stdlib.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ extern "C" {
1616

1717
/************************/
1818

19-
2019
void *malloc(size_t size);
2120
void free(void *mem);
2221
size_t avail(void);
@@ -35,16 +34,20 @@ void srand(int seed);
3534
#define __min(a,b) (((a) < (b)) ? (a) : (b))
3635
#define __max(a,b) (((a) > (b)) ? (a) : (b))
3736

37+
int abs(int n);
38+
3839
/************************/
3940

40-
int _fcvt_s(
41+
//The fcvt_s() function in C does not use locale information; it always uses . (a dot) as the decimal separator.
42+
__attribute__((import_name("twrFcvtS"))) int _fcvt_s(
4143
char* buffer,
42-
size_t sizeInBytes,
44+
unsigned long sizeInBytes, //size_t
4345
double value,
4446
int fracpart_numdigits,
4547
int *dec,
4648
int *sign
4749
);
50+
4851
double atof(const char* str);
4952
int atoi(const char *str);
5053
long atol( const char *str );

source/twr-stdclib/math.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include <math.h>
22
#include "twr-jsimports.h"
33

4-
int abs(int n) {
5-
if (n<0) return -n;
6-
else return n;
7-
}
4+
// It is not possible to define the math library imports like this:
5+
// __attribute__((import_name("twrASin"))) double sin(double arg);
6+
// as the compiler optimizes math functions like sin, and even at -O0, ignores the custom math.h
7+
// using the clang flag -fno-builtin solves this issue, but doesn't seem like an optimizing solution
88

99
double fabs (double arg) {
1010
return twrFAbs(arg);
@@ -99,9 +99,6 @@ int math_unit_test() {
9999

100100
if (cos(0)!=1) return 0;
101101

102-
if (abs(5)!=5) return 0;
103-
if (abs(-5)!=5) return 0;
104-
105102
if (fabs(1.0)!=1.0) return 0;
106103
if (fabs(-1.0)!=1.0) return 0;
107104

source/twr-stdclib/misc.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
#include <string.h>
33
#include <twr-crt.h>
44

5-
int misc_unit_test() {
6-
if (__min(5, 100)!=5) return 0;
7-
if (__max(5, 100)!=100) return 0;
8-
9-
return 1;
5+
int abs(int n) {
6+
if (n<0) return -n;
7+
else return n;
108
}
119

12-
1310
/**************************************************/
1411

1512
// for internal use, not an export
@@ -23,6 +20,16 @@ void __nstrcopy(char *dest, const int sizeInBytes, const char *src, const int si
2320
else if (sizeInBytes>0) dest[0]=0;
2421
}
2522

23+
/**************************************************/
24+
25+
int misc_unit_test() {
26+
if (__min(5, 100)!=5) return 0;
27+
if (__max(5, 100)!=100) return 0;
2628

29+
if (abs(5)!=5) return 0;
30+
if (abs(-5)!=5) return 0;
31+
32+
return 1;
33+
}
2734

2835

0 commit comments

Comments
 (0)