From ea71aaeaeeeee91cd91ec4dc68cfa3d963e0664e Mon Sep 17 00:00:00 2001 From: jef Date: Thu, 25 Oct 2012 22:14:02 -0700 Subject: [PATCH] Fixed cjson to not need -lm any more, by adding an ipow() routine to use instead of pow(). --- src/Makefile.am | 4 ++-- src/Makefile.in | 4 ++-- src/cjson.c | 27 ++++++++++++++++++++++----- src/cjson.h | 4 ++-- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 675c530..5fc5c1c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,7 +36,7 @@ libiperf_a_SOURCES = \ # Specify the sources and various flags for the iperf binary iperf3_SOURCES = main.c iperf3_CFLAGS = -g -Wall -iperf3_LDADD = libiperf.a -lm +iperf3_LDADD = libiperf.a iperf3_LDFLAGS = # Specify the sources and various flags for the profiled iperf binary. This @@ -45,7 +45,7 @@ iperf3_profile_SOURCES = main.c \ $(libiperf_a_SOURCES) iperf3_profile_CFLAGS = -pg -Wall -iperf3_profile_LDADD = libiperf.a -lm +iperf3_profile_LDADD = libiperf.a iperf3_profile_LDFLAGS = # Specify the sources and various flags for the test cases diff --git a/src/Makefile.in b/src/Makefile.in index 4f1963f..5ae6bce 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -268,7 +268,7 @@ libiperf_a_SOURCES = \ # Specify the sources and various flags for the iperf binary iperf3_SOURCES = main.c iperf3_CFLAGS = -g -Wall -iperf3_LDADD = libiperf.a -lm +iperf3_LDADD = libiperf.a iperf3_LDFLAGS = # Specify the sources and various flags for the profiled iperf binary. This @@ -277,7 +277,7 @@ iperf3_profile_SOURCES = main.c \ $(libiperf_a_SOURCES) iperf3_profile_CFLAGS = -pg -Wall -iperf3_profile_LDADD = libiperf.a -lm +iperf3_profile_LDADD = libiperf.a iperf3_profile_LDFLAGS = # Specify the sources and various flags for the test cases diff --git a/src/cjson.c b/src/cjson.c index af55f04..bc166c8 100644 --- a/src/cjson.c +++ b/src/cjson.c @@ -112,16 +112,33 @@ void cJSON_Delete( cJSON *c ) } +static double ipow( double n, int exp ) +{ + double r; + + if ( exp < 0 ) + return 1.0 / ipow( n, -exp ); + r = 1; + while ( exp > 0 ) { + if ( exp & 1 ) + r *= n; + exp >>= 1; + n *= n; + } + return r; +} + + /* Parse the input text to generate a number, and populate the result into item. */ static const char *parse_number( cJSON *item, const char *num ) { - double n = 0, sign = 1, scale = 0; - int subscale = 0, signsubscale = 1; + double n = 0; + int sign = 1, scale = 0, subscale = 0, signsubscale = 1; /* Could use sscanf for this? */ if ( *num == '-' ) { /* Has sign. */ - sign=-1; + sign = -1; num++; } if ( *num == '0' ) @@ -156,7 +173,7 @@ static const char *parse_number( cJSON *item, const char *num ) } /* Put it together: number = +/- number.fraction * 10^+/- exponent */ - n = sign * n * pow( 10.0, ( scale + subscale * signsubscale ) ); + n = sign * n * ipow( 10.0, scale + subscale * signsubscale ); item->valuefloat = n; item->valueint = n; @@ -173,7 +190,7 @@ static char *print_number( cJSON *item ) if ( fabs( ( (double) item->valueint ) - d ) <= DBL_EPSILON && d <= INT_MAX && d >= INT_MIN ) { str = (char*) cJSON_malloc( 21 ); /* 2^64+1 can be represented in 21 chars. */ if ( str ) - sprintf( str, "%d", item->valueint ); + sprintf( str, "%lld", item->valueint ); } else { str = (char*) cJSON_malloc( 64 ); /* This is a nice tradeoff. */ if ( str ) { diff --git a/src/cjson.h b/src/cjson.h index dc359ed..e86629f 100644 --- a/src/cjson.h +++ b/src/cjson.h @@ -76,7 +76,7 @@ extern int cJSON_GetArraySize( cJSON *array ); /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ extern cJSON *cJSON_GetArrayItem( cJSON *array, int item ); /* Get item "string" from object. Case insensitive. */ -extern cJSON *cJSON_GetObjectItem( cJSON *object,const char *string ); +extern cJSON *cJSON_GetObjectItem( cJSON *object, const char *string ); /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ extern const char *cJSON_GetErrorPtr( void ); @@ -93,7 +93,7 @@ extern cJSON *cJSON_CreateArray( void ); extern cJSON *cJSON_CreateObject( void ); /* These utilities create an Array of count items. */ -extern cJSON *cJSON_CreateIntArray( long long *numbers,int count ); +extern cJSON *cJSON_CreateIntArray( long long *numbers, int count ); extern cJSON *cJSON_CreateFloatArray( double *numbers, int count ); extern cJSON *cJSON_CreateStringArray( const char **strings, int count );