Skip to content

C99 #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

C99 #47

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CCFLAGS = -ansi -Wall -Wshadow -O2
CCFLAGS = -O2 -std=c99 -Wall -Wextra -Wshadow -Wcast-align -Wmissing-declarations -Wmissing-prototypes -Wconversion -Wno-long-long -pedantic
LFLAGS = -lm

.PHONY = all clean
Expand Down
28 changes: 14 additions & 14 deletions benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@

typedef double (*function1)(double);

void bench(const char *expr, function1 func) {
static void bench(const char *expr, function1 func) {
int i, j;
volatile double d;
double tmp;
static double tmp;
clock_t start;

te_variable lk = {"a", &tmp};
te_variable lk = {"a", {&tmp}, TE_VARIABLE, NULL};

printf("Expression: %s\n", expr);

Expand All @@ -53,12 +53,12 @@ void bench(const char *expr, function1 func) {
tmp = i;
d += func(tmp);
}
const int nelapsed = (clock() - start) * 1000 / CLOCKS_PER_SEC;
const long int nelapsed = (clock() - start) * 1000 / CLOCKS_PER_SEC;

/*Million floats per second input.*/
printf(" %.5g", d);
if (nelapsed)
printf("\t%5dms\t%5dmfps\n", nelapsed, loops * loops / nelapsed / 1000);
printf("\t%5ldms\t%5ldmfps\n", nelapsed, loops * loops / nelapsed / 1000);
else
printf("\tinf\n");

Expand All @@ -74,47 +74,47 @@ void bench(const char *expr, function1 func) {
tmp = i;
d += te_eval(n);
}
const int eelapsed = (clock() - start) * 1000 / CLOCKS_PER_SEC;
const long int eelapsed = (clock() - start) * 1000 / CLOCKS_PER_SEC;
te_free(n);

/*Million floats per second input.*/
printf(" %.5g", d);
if (eelapsed)
printf("\t%5dms\t%5dmfps\n", eelapsed, loops * loops / eelapsed / 1000);
printf("\t%5ldms\t%5ldmfps\n", eelapsed, loops * loops / eelapsed / 1000);
else
printf("\tinf\n");


printf("%.2f%% longer\n", (((double)eelapsed / nelapsed) - 1.0) * 100.0);
printf("%.2f%% longer\n", (((double)eelapsed / (double)nelapsed) - 1.0) * 100.0);


printf("\n");
}


double a5(double a) {
static double a5(double a) {
return a+5;
}

double a52(double a) {
static double a52(double a) {
return (a+5)*2;
}

double a10(double a) {
static double a10(double a) {
return a+(5*2);
}

double as(double a) {
static double as(double a) {
return sqrt(pow(a, 1.5) + pow(a, 2.5));
}

double al(double a) {
static double al(double a) {
return (1/(a+1)+2/(a+2)+3/(a+3));
}

int main(int argc, char *argv[])
{

(void)argc; (void)argv;
bench("sqrt(a^1.5+a^2.5)", as);
bench("a+5", a5);
bench("a+(5*2)", a10);
Expand Down
95 changes: 95 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright (C) 2016-2018 |Meso|Star>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.8)
project(tinyexpr C)
enable_testing()

option(LIB_ONLY "Do not compile the test pograms nor the benchmark" OFF)

set(TINYEXPR_SOURCE_DIR ${PROJECT_SOURCE_DIR}/..)

include_directories(${TINYEXPR_SOURCE_DIR})

################################################################################
# Configure and define targets
################################################################################
set(VERSION_MAJOR 1)
set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})

if(CMAKE_COMPILER_IS_GNUCC)
set(MATH_LIB m)
endif()

if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
elseif(CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wshadow -Wcast-align -Wmissing-declarations -Wmissing-prototypes -Wconversion -Wno-long-long -pedantic")
endif()

ADD_LIBRARY(tinyexpr STATIC
${TINYEXPR_SOURCE_DIR}/tinyexpr.c ${TINYEXPR_SOURCE_DIR}/tinyexpr.h)
target_compile_features(tinyexpr PUBLIC c_std_99)
set_target_properties(tinyexpr PROPERTIES
VERSION ${VERSION}
SOVERSION ${VERSION_MAJOR})

################################################################################
# Define tests
################################################################################
if(NOT LIB_ONLY)
add_executable(bench ${TINYEXPR_SOURCE_DIR}/benchmark.c)
target_link_libraries(bench tinyexpr ${MATH_LIB})

add_executable(example ${TINYEXPR_SOURCE_DIR}/example.c)
target_link_libraries(example tinyexpr ${MATH_LIB})
add_test(example example)

add_executable(example2 ${TINYEXPR_SOURCE_DIR}/example2.c)
target_link_libraries(example2 tinyexpr ${MATH_LIB})
add_test(example2 example2)

add_executable(example3 ${TINYEXPR_SOURCE_DIR}/example3.c)
target_link_libraries(example3 tinyexpr ${MATH_LIB})
add_test(example3 example3)

add_executable(test_pow_left ${TINYEXPR_SOURCE_DIR}/test.c)
target_link_libraries(test_pow_left tinyexpr ${MATH_LIB})
add_test(test_pow_left test_pow_left)

file(READ ${TINYEXPR_SOURCE_DIR}/tinyexpr.c tinyexpr_c_file_content)
file(WRITE ${PROJECT_BINARY_DIR}/tinyexpr_pow_right.c
"#define TE_POW_FROM_RIGHT 1
#define TE_NAT_LOG 1
${tinyexpr_c_file_content}")

file(READ ${TINYEXPR_SOURCE_DIR}/test.c test_c_file_content)
file(WRITE ${PROJECT_BINARY_DIR}/test_pow_right.c
"#define TE_POW_FROM_RIGHT 1
#define TE_NAT_LOG 1
${test_c_file_content}")

ADD_LIBRARY(tinyexpr_pow_right STATIC
${PROJECT_BINARY_DIR}/tinyexpr_pow_right.c ${TINYEXPR_SOURCE_DIR}/tinyexpr.h)
target_compile_features(tinyexpr_pow_right PUBLIC c_std_99)
set_target_properties(tinyexpr_pow_right PROPERTIES VERSION ${VERSION})

add_executable(test_pow_right ${PROJECT_BINARY_DIR}/test_pow_right.c)
add_dependencies(test_pow_right tinyexpr_pow_right)
target_link_libraries(test_pow_right tinyexpr_pow_right ${MATH_LIB})
add_test(test_pow_right test_pow_right)
endif(NOT LIB_ONLY)
1 change: 1 addition & 0 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

int main(int argc, char *argv[])
{
(void)argc; (void)argv;
const char *c = "sqrt(5^2+7^2+11^2+(8-2)^2)";
double r = te_interp(c, 0);
printf("The expression:\n\t%s\nevaluates to:\n\t%f\n", c, r);
Expand Down
5 changes: 3 additions & 2 deletions example2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

int main(int argc, char *argv[])
{
(void)argc; (void)argv;
if (argc < 2) {
printf("Usage: example2 \"expression\"\n");
return 0;
Expand All @@ -13,8 +14,8 @@ int main(int argc, char *argv[])

/* This shows an example where the variables
* x and y are bound at eval-time. */
double x, y;
te_variable vars[] = {{"x", &x}, {"y", &y}};
static double x, y;
te_variable vars[] = {{"x", {&x}, TE_VARIABLE, NULL}, {"y", {&y}, TE_VARIABLE, NULL}};

/* This will compile the expression and check for errors. */
int err;
Expand Down
5 changes: 3 additions & 2 deletions example3.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@


/* An example of calling a C function. */
double my_sum(double a, double b) {
static double my_sum(double a, double b) {
printf("Called C function with %f and %f.\n", a, b);
return a + b;
}


int main(int argc, char *argv[])
{
(void)argc; (void)argv;
te_variable vars[] = {
{"mysum", my_sum, TE_FUNCTION2}
{"mysum", {.f2=my_sum}, TE_FUNCTION2, NULL}
};

const char *expression = "mysum(5, 6)";
Expand Down
Loading