# Adding_Math_Functions_To_uClibc_mathlib

Kamikaze's mathlib is missing many C99 math functions. See http://forum.openwrt.org/viewtopic.php?id=4166 for more information. The developers say it is to reduce the math lib's storage footprint. This is a valid point for most embedded systems and most OpenWrt packages don't require these functions. Having said that, if you find the list of included math functions too restrictive, you can add back in those math functions included in the uCLib's math library source but not compiled for OpenWrt. In my experience, adding functions like ceilf or floorf add about 500 bytes each to the resulting math library.

To add back some of the C99 math functions that are present in uClibc's libm but are absent in OpenWrt, do the following:

- Checkout a fresh copy of the OpenWrt source. This has been tested in detail on Kamikaze revision 3535. To fetch that release, use:

- Now, edit toolchain/uClibc/patches/120-more_standard_math.patch - a patch file for uClibc. Look for the line:

+CSRC+= fpmacros.c nan.c s_rint.c e_hypot.c w_hypot.c

- At the end of that line (i.e. line 9) add the source files appropriate for your situation. In my case I needed floorf and ceilf, so the line looks like (again, this is for revision 3535):

+CSRC+= fpmacros.c nan.c s_rint.c e_hypot.c w_hypot.c s_floorf.c s_ceilf.c

- If you are not sure what .c file corresponds to the appropriate function, do some grepping about in the source tree. I did something like the following in a copy of my trunk that had the build directories (i.e. in a tree where I had already built OpenWrt Kamikaze r3535):

cd trunk/openwrt/toolchain_build_mipsel/uClibc-0.9.28/libm

grep floorf *.c

- Build OpenWrt as normal. Enjoy your new math functions.

**Postscript** If you want to include float wrapper functions, like powf (that wraps to (double)pow(double, double) ) then you will have to also patch uClibc-0.9.28/libm/float_wrappers.c to include defines for the functions you want to enable. These are typically in the form L_f as an example powf is L_powf. You can have the toolchain patch apply the uClibc patch by populating files in toolchain/uClibc/patches. E.g. to include powf, put the following in toolchain/uClibc/patches/121-even_more_standard_math.patch :

--- uClibc-0.9.28.orig/libm/float_wrappers.c 2005-08-17 15:49:41.000000000 -0700
+++ uClibc-0.9.28/libm/float_wrappers.c 2006-04-01 21:27:12.000000000 -0800
@@ -24,6 +24,8 @@
#include "math.h"
+#define L_powf
+
/* For the time being, do _NOT_ implement these functions
* that are defined by SuSv3 */
#if 0

- Here is a more complex example. This is what is required for wview 3.2.1 'port' w/ OpenWrt SDK.

--- uClibc-0.9.28.orig/libm/float_wrappers.c 2005-08-17 15:49:41.000000000 -0700
+++ uClibc-0.9.28/libm/float_wrappers.c 2006-04-01 22:41:51.000000000 -0800
@@ -24,6 +24,13 @@
#include "math.h"
+#define L_powf
+float powf(float, float);
+#define L_cosf
+float cosf(float);
+#define L_sinf
+float sinf(float);
+
/* For the time being, do _NOT_ implement these functions
* that are defined by SuSv3 */
#if 0

## Comments (0)

You don't have permission to comment on this page.