Skip to content

Commit

Permalink
add initial code to test floating point context switch
Browse files Browse the repository at this point in the history
  • Loading branch information
cjhackillinois authored and Ciesko committed Jul 10, 2023
1 parent 9855b75 commit 870de63
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/basics/qthread_fp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <qthread/qthread.h>


// https://www.geeksforgeeks.org/comparison-float-value-c/
// https://dotnettutorials.net/lesson/taylor-series-using-recursion-in-c/
// https://www.studytonight.com/c/programs/important-concepts/sum-of-taylor-series

struct parts
{
int length;
float exp;
float ans;
aligned_t* cond;
};

// https://www.w3resource.com/c-programming-exercises/math/c-math-exercise-24.php
static float taylor_exponential_core(int n, float x)
{
float exp_sum = 1;
for (int i = n - 1; i > 0; --i)
{
exp_sum = 1 + x * exp_sum / i;
}
return exp_sum;
}

static aligned_t taylor_exponential(void *arg)
{
struct parts* te = (struct parts*) arg;
float exp = te->exp;
float length = te->length;
te->ans = taylor_exponential_core(te->length, te->exp);
return 0;
}

static void checkFloat()
{
void* cond = malloc(QTHREAD_SIZEOF_ALIGNED_T);
struct parts teParts = {250,9.0f,0.0f, cond};
int ret = -1;
qthread_empty(teParts.cond);

ret = qthread_fork(taylor_exponential, &teParts, teParts.cond);
assert(ret == QTHREAD_SUCCESS);

ret = qthread_readFF(NULL, teParts.cond);
assert(ret == QTHREAD_SUCCESS);

printf("value of ans = %f\n", teParts.ans);
}

int main(void)
{
float ans = taylor_exponential_core(250, 9.0);
printf("value of ans = %f\n", ans);
assert(ans == 8103.083984f);

int status = qthread_initialize();
assert(status == QTHREAD_SUCCESS);

checkFloat();
return EXIT_SUCCESS;
}

0 comments on commit 870de63

Please sign in to comment.