Skip to content

Commit 8707ea3

Browse files
committed
lib: Add lib and libm, moving current files from stmhal.
Top-level lib directory is for standard C libraries that we want to provide our own versions of (for efficiency and stand-alone reasons). It currently has libm in it for math functions. Also add atanf and atan2f, which addresses issue adafruit#837.
1 parent 17ae239 commit 8707ea3

File tree

6 files changed

+250
-57
lines changed

6 files changed

+250
-57
lines changed

lib/libm/atan2f.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*****************************************************************************/
2+
/*****************************************************************************/
3+
// atan2f from musl-0.9.15
4+
/*****************************************************************************/
5+
/*****************************************************************************/
6+
7+
/* origin: FreeBSD /usr/src/lib/msun/src/e_atan2f.c */
8+
/*
9+
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
10+
*/
11+
/*
12+
* ====================================================
13+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
14+
*
15+
* Developed at SunPro, a Sun Microsystems, Inc. business.
16+
* Permission to use, copy, modify, and distribute this
17+
* software is freely granted, provided that this notice
18+
* is preserved.
19+
* ====================================================
20+
*/
21+
22+
#include "libm.h"
23+
24+
static const float
25+
pi = 3.1415927410e+00, /* 0x40490fdb */
26+
pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */
27+
28+
float atan2f(float y, float x)
29+
{
30+
float z;
31+
uint32_t m,ix,iy;
32+
33+
if (isnan(x) || isnan(y))
34+
return x+y;
35+
GET_FLOAT_WORD(ix, x);
36+
GET_FLOAT_WORD(iy, y);
37+
if (ix == 0x3f800000) /* x=1.0 */
38+
return atanf(y);
39+
m = ((iy>>31)&1) | ((ix>>30)&2); /* 2*sign(x)+sign(y) */
40+
ix &= 0x7fffffff;
41+
iy &= 0x7fffffff;
42+
43+
/* when y = 0 */
44+
if (iy == 0) {
45+
switch (m) {
46+
case 0:
47+
case 1: return y; /* atan(+-0,+anything)=+-0 */
48+
case 2: return pi; /* atan(+0,-anything) = pi */
49+
case 3: return -pi; /* atan(-0,-anything) =-pi */
50+
}
51+
}
52+
/* when x = 0 */
53+
if (ix == 0)
54+
return m&1 ? -pi/2 : pi/2;
55+
/* when x is INF */
56+
if (ix == 0x7f800000) {
57+
if (iy == 0x7f800000) {
58+
switch (m) {
59+
case 0: return pi/4; /* atan(+INF,+INF) */
60+
case 1: return -pi/4; /* atan(-INF,+INF) */
61+
case 2: return 3*pi/4; /*atan(+INF,-INF)*/
62+
case 3: return -3*pi/4; /*atan(-INF,-INF)*/
63+
}
64+
} else {
65+
switch (m) {
66+
case 0: return 0.0f; /* atan(+...,+INF) */
67+
case 1: return -0.0f; /* atan(-...,+INF) */
68+
case 2: return pi; /* atan(+...,-INF) */
69+
case 3: return -pi; /* atan(-...,-INF) */
70+
}
71+
}
72+
}
73+
/* |y/x| > 0x1p26 */
74+
if (ix+(26<<23) < iy || iy == 0x7f800000)
75+
return m&1 ? -pi/2 : pi/2;
76+
77+
/* z = atan(|y/x|) with correct underflow */
78+
if ((m&2) && iy+(26<<23) < ix) /*|y/x| < 0x1p-26, x < 0 */
79+
z = 0.0;
80+
else
81+
z = atanf(fabsf(y/x));
82+
switch (m) {
83+
case 0: return z; /* atan(+,+) */
84+
case 1: return -z; /* atan(-,+) */
85+
case 2: return pi - (z-pi_lo); /* atan(+,-) */
86+
default: /* case 3 */
87+
return (z-pi_lo) - pi; /* atan(-,-) */
88+
}
89+
}

lib/libm/atanf.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*****************************************************************************/
2+
/*****************************************************************************/
3+
// atanf from musl-0.9.15
4+
/*****************************************************************************/
5+
/*****************************************************************************/
6+
7+
/* origin: FreeBSD /usr/src/lib/msun/src/s_atanf.c */
8+
/*
9+
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
10+
*/
11+
/*
12+
* ====================================================
13+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
14+
*
15+
* Developed at SunPro, a Sun Microsystems, Inc. business.
16+
* Permission to use, copy, modify, and distribute this
17+
* software is freely granted, provided that this notice
18+
* is preserved.
19+
* ====================================================
20+
*/
21+
22+
23+
#include "libm.h"
24+
25+
static const float atanhi[] = {
26+
4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */
27+
7.8539812565e-01, /* atan(1.0)hi 0x3f490fda */
28+
9.8279368877e-01, /* atan(1.5)hi 0x3f7b985e */
29+
1.5707962513e+00, /* atan(inf)hi 0x3fc90fda */
30+
};
31+
32+
static const float atanlo[] = {
33+
5.0121582440e-09, /* atan(0.5)lo 0x31ac3769 */
34+
3.7748947079e-08, /* atan(1.0)lo 0x33222168 */
35+
3.4473217170e-08, /* atan(1.5)lo 0x33140fb4 */
36+
7.5497894159e-08, /* atan(inf)lo 0x33a22168 */
37+
};
38+
39+
static const float aT[] = {
40+
3.3333328366e-01,
41+
-1.9999158382e-01,
42+
1.4253635705e-01,
43+
-1.0648017377e-01,
44+
6.1687607318e-02,
45+
};
46+
47+
float atanf(float x)
48+
{
49+
float_t w,s1,s2,z;
50+
uint32_t ix,sign;
51+
int id;
52+
53+
GET_FLOAT_WORD(ix, x);
54+
sign = ix>>31;
55+
ix &= 0x7fffffff;
56+
if (ix >= 0x4c800000) { /* if |x| >= 2**26 */
57+
if (isnan(x))
58+
return x;
59+
z = atanhi[3] + 0x1p-120f;
60+
return sign ? -z : z;
61+
}
62+
if (ix < 0x3ee00000) { /* |x| < 0.4375 */
63+
if (ix < 0x39800000) { /* |x| < 2**-12 */
64+
if (ix < 0x00800000)
65+
/* raise underflow for subnormal x */
66+
FORCE_EVAL(x*x);
67+
return x;
68+
}
69+
id = -1;
70+
} else {
71+
x = fabsf(x);
72+
if (ix < 0x3f980000) { /* |x| < 1.1875 */
73+
if (ix < 0x3f300000) { /* 7/16 <= |x| < 11/16 */
74+
id = 0;
75+
x = (2.0f*x - 1.0f)/(2.0f + x);
76+
} else { /* 11/16 <= |x| < 19/16 */
77+
id = 1;
78+
x = (x - 1.0f)/(x + 1.0f);
79+
}
80+
} else {
81+
if (ix < 0x401c0000) { /* |x| < 2.4375 */
82+
id = 2;
83+
x = (x - 1.5f)/(1.0f + 1.5f*x);
84+
} else { /* 2.4375 <= |x| < 2**26 */
85+
id = 3;
86+
x = -1.0f/x;
87+
}
88+
}
89+
}
90+
/* end of argument reduction */
91+
z = x*x;
92+
w = z*z;
93+
/* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
94+
s1 = z*(aT[0]+w*(aT[2]+w*aT[4]));
95+
s2 = w*(aT[1]+w*aT[3]);
96+
if (id < 0)
97+
return x - x*(s1+s2);
98+
z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
99+
return sign ? -z : z;
100+
}

lib/libm/libm.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*****************************************************************************/
2+
/*****************************************************************************/
3+
// portions extracted from musl-0.9.15 libm.h
4+
/*****************************************************************************/
5+
/*****************************************************************************/
6+
7+
/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
8+
/*
9+
* ====================================================
10+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
11+
*
12+
* Developed at SunPro, a Sun Microsystems, Inc. business.
13+
* Permission to use, copy, modify, and distribute this
14+
* software is freely granted, provided that this notice
15+
* is preserved.
16+
* ====================================================
17+
*/
18+
19+
#include <stdint.h>
20+
#include <math.h>
21+
22+
#define FORCE_EVAL(x) do { \
23+
if (sizeof(x) == sizeof(float)) { \
24+
volatile float __x; \
25+
__x = (x); \
26+
(void)__x; \
27+
} else if (sizeof(x) == sizeof(double)) { \
28+
volatile double __x; \
29+
__x = (x); \
30+
(void)__x; \
31+
} else { \
32+
volatile long double __x; \
33+
__x = (x); \
34+
(void)__x; \
35+
} \
36+
} while(0)
37+
38+
/* Get a 32 bit int from a float. */
39+
#define GET_FLOAT_WORD(w,d) \
40+
do { \
41+
union {float f; uint32_t i;} __u; \
42+
__u.f = (d); \
43+
(w) = __u.i; \
44+
} while (0)
45+
46+
/* Set a float from a 32 bit int. */
47+
#define SET_FLOAT_WORD(d,w) \
48+
do { \
49+
union {float f; uint32_t i;} __u; \
50+
__u.i = (w); \
51+
(d) = __u.f; \
52+
} while (0)

stmhal/math.c renamed to lib/libm/math.c

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <stdint.h>
28-
#include <math.h>
27+
#include "libm.h"
2928

3029
typedef float float_t;
3130
typedef union {
@@ -117,11 +116,8 @@ float tanhf(float x) { return sinhf(x) / coshf(x); }
117116
float acoshf(float x) { return 0.0; }
118117
float asinhf(float x) { return 0.0; }
119118
float atanhf(float x) { return 0.0; }
120-
float tanf(float x) { return 0.0; }
121119
float acosf(float x) { return 0.0; }
122120
float asinf(float x) { return 0.0; }
123-
float atanf(float x) { return 0.0; }
124-
float atan2f(float x, float y) { return 0.0; }
125121
float fmodf(float x, float y) { return 0.0; }
126122
float tgammaf(float x) { return 0.0; }
127123
float lgammaf(float x) { return 0.0; }
@@ -131,56 +127,6 @@ float modff(float x, float *y) { return 0.0; }
131127
float frexpf(float x, int *exp) { return 0.0; }
132128
float ldexpf(float x, int exp) { return 0.0; }
133129

134-
/*****************************************************************************/
135-
/*****************************************************************************/
136-
// from musl-0.9.15 libm.h
137-
/*****************************************************************************/
138-
/*****************************************************************************/
139-
140-
/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
141-
/*
142-
* ====================================================
143-
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
144-
*
145-
* Developed at SunPro, a Sun Microsystems, Inc. business.
146-
* Permission to use, copy, modify, and distribute this
147-
* software is freely granted, provided that this notice
148-
* is preserved.
149-
* ====================================================
150-
*/
151-
152-
#define FORCE_EVAL(x) do { \
153-
if (sizeof(x) == sizeof(float)) { \
154-
volatile float __x; \
155-
__x = (x); \
156-
(void)__x; \
157-
} else if (sizeof(x) == sizeof(double)) { \
158-
volatile double __x; \
159-
__x = (x); \
160-
(void)__x; \
161-
} else { \
162-
volatile long double __x; \
163-
__x = (x); \
164-
(void)__x; \
165-
} \
166-
} while(0)
167-
168-
/* Get a 32 bit int from a float. */
169-
#define GET_FLOAT_WORD(w,d) \
170-
do { \
171-
union {float f; uint32_t i;} __u; \
172-
__u.f = (d); \
173-
(w) = __u.i; \
174-
} while (0)
175-
176-
/* Set a float from a 32 bit int. */
177-
#define SET_FLOAT_WORD(d,w) \
178-
do { \
179-
union {float f; uint32_t i;} __u; \
180-
__u.i = (w); \
181-
(d) = __u.f; \
182-
} while (0)
183-
184130
/*****************************************************************************/
185131
/*****************************************************************************/
186132
// __fpclassifyf from musl-0.9.15
File renamed without changes.

stmhal/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ endif
6060
# uncomment this if you want libgcc
6161
#LIBS += $(shell $(CC) -print-libgcc-file-name)
6262

63+
SRC_LIB = $(addprefix lib/,\
64+
libm/math.c \
65+
libm/mathsincos.c \
66+
libm/atanf.c \
67+
libm/atan2f.c \
68+
)
69+
6370
SRC_C = \
6471
main.c \
6572
string0.c \
@@ -84,8 +91,6 @@ SRC_C = \
8491
uart.c \
8592
usb.c \
8693
printf.c \
87-
math.c \
88-
mathsincos.c \
8994
gccollect.c \
9095
pybstdio.c \
9196
readline.c \
@@ -193,6 +198,7 @@ SRC_CC3K = $(addprefix $(CC3K_DIR)/,\
193198

194199
OBJ =
195200
OBJ += $(PY_O)
201+
OBJ += $(addprefix $(BUILD)/, $(SRC_LIB:.c=.o))
196202
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
197203
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
198204
OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy