diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..37a9bfd --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,3 @@ +*.o +*.c +*.h diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..aeea3d7 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,11 @@ +CC=gcc +CFLAGS= + +SOURCE_FILES=$(wildcard *.org) + +%.o: %.c.org + - emacs --quick --no-init-file --batch --eval "(require 'org)" --eval '(org-babel-tangle-file "$^")' + - $(CC) $(CFLAGS) -c $*.c -o $@ + - rm -f $*.c $*.h + + diff --git a/src/README.org b/src/README.org new file mode 100644 index 0000000..49abb06 --- /dev/null +++ b/src/README.org @@ -0,0 +1,12 @@ +Source files are written in org-mode format, to provide useful comments and +LaTex formulas around the code. + +The code is extracted from the org files using Emacs in the `Makefile`, and +then he produced files are compiled. + +For a better experience, editing can be done with Emacs. Note that Emacs can +behave like Vim when switched into ``Evil'' mode. + + + + diff --git a/src/qmckl_context.c.org b/src/qmckl_context.c.org new file mode 100644 index 0000000..c21cfd4 --- /dev/null +++ b/src/qmckl_context.c.org @@ -0,0 +1,126 @@ +# -*- mode: org -*- + +#+BEGIN_SRC C :tangle qmckl_context.c +#include /* malloc */ +#include "qmckl_context.h" +#+END_SRC + +* Context + + The context variable is a handle for the state of the library, and + is stored in the following data structure, which can't be seen + outside of the library. + + #+BEGIN_SRC C :tangle qmckl_context.h +#define QMCKL_DEFAULT_PRECISION 53 +#define QMCKL_DEFAULT_RANGE 2 + +typedef long int qmckl_context ; + #+END_SRC + + + #+BEGIN_SRC C :tangle qmckl_context.c +typedef struct qmckl_context_struct_ { + struct qmckl_context_struct_ * prev; + int precision; + int range; +} qmckl_context_struct; + #+END_SRC + + To create a new context, use =qmckl_context_create()=. If the creation + failed, the function returns 0. On success, a pointer to a context + is returned as a long integer. + + #+BEGIN_SRC C :tangle qmckl_context.h +qmckl_context qmckl_context_create(); + #+END_SRC + + #+BEGIN_SRC C :tangle qmckl_context.c +qmckl_context qmckl_context_create() { + + qmckl_context_struct* context; + + context = (qmckl_context_struct*) malloc (sizeof(qmckl_context_struct)); + if (context == NULL) { + return (qmckl_context) 0; + } + + context->prev = NULL; + context->precision = QMCKL_DEFAULT_PRECISION; + context->range = QMCKL_DEFAULT_RANGE; + + return (qmckl_context) context; +} + #+END_SRC + + + #+BEGIN_SRC C :tangle qmckl_context.h +qmckl_context qmckl_context_copy(qmckl_context context); + #+END_SRC + + #+BEGIN_SRC C :tangle qmckl_context.c +qmckl_context qmckl_context_copy(qmckl_context context) { + + qmckl_context_struct* old_context; + qmckl_context_struct* new_context; + + new_context = (qmckl_context_struct*) malloc (sizeof(qmckl_context_struct)); + if (new_context == NULL) { + return (qmckl_context) 0; + } + + old_context = (qmckl_context_struct*) context; + + new_context->prev = old_context; + new_context->precision = old_context->precision; + new_context->range = old_context->range; + + return (qmckl_context) new_context; +} + #+END_SRC + + +* Precision + + The following functions set the expected required precision and range. + =precision= should be an integer between 2 and 53, and =range= should be + an integer between 2 and 11. + These functions return 0 upon success. + + #+BEGIN_SRC C :tangle qmckl_context.h +qmckl_context qmckl_context_set_precision(qmckl_context context, int precision); + #+END_SRC + + #+BEGIN_SRC C :tangle qmckl_context.c +qmckl_context qmckl_context_set_precision(qmckl_context context, int precision) { + qmckl_context_struct* ctx; + + if (precision < 2) return (qmckl_context) 0; + if (precision > 53) return (qmckl_context) 0; + + ctx = (qmckl_context_struct*) qmckl_context_copy(context); + ctx->precision = precision; + return (qmckl_context) ctx; +} + #+END_SRC + + + #+BEGIN_SRC C :tangle qmckl_context.h +qmckl_context qmckl_context_set_range(qmckl_context context, int range); + #+END_SRC + + #+BEGIN_SRC C :tangle qmckl_context.c +qmckl_context qmckl_context_set_range(qmckl_context context, int range) { + qmckl_context_struct* ctx; + + if (range < 2) return (qmckl_context) 0; + if (range > 11) return (qmckl_context) 0; + + ctx = (qmckl_context_struct*) qmckl_context_copy(context); + ctx->range = range; + return (qmckl_context) ctx; +} + #+END_SRC + + +