2020-10-14 00:52:50 +02:00
|
|
|
# -*- mode: org -*-
|
|
|
|
|
2020-10-15 08:57:01 +02:00
|
|
|
#+TITLE: Context
|
2020-10-14 09:54:12 +02:00
|
|
|
|
2020-10-14 01:43:13 +02:00
|
|
|
This file is written in C because it is more natural to express the context in
|
2020-10-14 09:54:12 +02:00
|
|
|
C than in Fortran.
|
2020-10-14 01:43:13 +02:00
|
|
|
|
|
|
|
|
2020-10-14 00:52:50 +02:00
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
|
|
|
#include <stdlib.h> /* malloc */
|
|
|
|
#include "qmckl_context.h"
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
* Context
|
2020-10-14 01:43:13 +02:00
|
|
|
|
2020-10-14 00:52:50 +02:00
|
|
|
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.
|
|
|
|
|
2020-10-15 08:57:01 +02:00
|
|
|
|
2020-10-14 00:52:50 +02:00
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.h
|
|
|
|
#define QMCKL_DEFAULT_PRECISION 53
|
|
|
|
#define QMCKL_DEFAULT_RANGE 2
|
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
/* 64-bit integer */
|
2020-10-14 01:43:13 +02:00
|
|
|
typedef long long int qmckl_context ;
|
2020-10-14 00:52:50 +02:00
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
|
|
|
typedef struct qmckl_context_struct_ {
|
|
|
|
struct qmckl_context_struct_ * prev;
|
2020-10-14 01:43:13 +02:00
|
|
|
int precision;
|
|
|
|
int range;
|
2020-10-14 00:52:50 +02:00
|
|
|
} qmckl_context_struct;
|
|
|
|
#+END_SRC
|
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
** =qmckl_context_create=
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
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 64-bit integer.
|
|
|
|
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.h
|
2020-10-14 00:52:50 +02:00
|
|
|
qmckl_context qmckl_context_create();
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
2020-10-14 00:52:50 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
2020-10-14 01:43:13 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
** =qmckl_context_copy=
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.h
|
2020-10-15 09:22:24 +02:00
|
|
|
qmckl_context qmckl_context_copy(const qmckl_context context);
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
2020-10-15 09:22:24 +02:00
|
|
|
qmckl_context qmckl_context_copy(const qmckl_context context) {
|
2020-10-14 00:52:50 +02:00
|
|
|
|
|
|
|
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;
|
2020-10-14 01:43:13 +02:00
|
|
|
new_context->precision = old_context->precision;
|
2020-10-14 00:52:50 +02:00
|
|
|
new_context->range = old_context->range;
|
|
|
|
|
|
|
|
return (qmckl_context) new_context;
|
|
|
|
}
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
2020-10-14 01:43:13 +02:00
|
|
|
|
2020-10-15 09:00:22 +02:00
|
|
|
** =qmckl_context_destroy=
|
2020-10-15 07:56:43 +02:00
|
|
|
|
2020-10-15 09:00:22 +02:00
|
|
|
To delete a new context, use =qmckl_context_destroy()=. If the deletion
|
2020-10-15 07:56:43 +02:00
|
|
|
failed, the function returns =0=. On success, the function returns =1=
|
|
|
|
implying that the context has been freed.
|
|
|
|
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.h
|
2020-10-15 09:00:22 +02:00
|
|
|
qmckl_context qmckl_context_destroy(qmckl_context context);
|
2020-10-15 07:56:43 +02:00
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
2020-10-15 09:00:22 +02:00
|
|
|
qmckl_context qmckl_context_destroy(qmckl_context context) {
|
2020-10-15 07:56:43 +02:00
|
|
|
|
|
|
|
if (context == NULL) {
|
|
|
|
return (qmckl_context) 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(context);
|
|
|
|
return (qmckl_context) 1;
|
|
|
|
}
|
|
|
|
#+END_SRC
|
2020-10-14 00:52:50 +02:00
|
|
|
|
|
|
|
* Precision
|
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
The following functions set and get 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.
|
|
|
|
The setter functions functions return a new context as a 64-bit integer.
|
|
|
|
The getter functions return the value, as a 32-bit integer.
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
** =qmckl_context_set_precision=
|
|
|
|
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.h
|
2020-10-15 09:22:24 +02:00
|
|
|
qmckl_context qmckl_context_set_precision(const qmckl_context context, int precision);
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
2020-10-15 09:22:24 +02:00
|
|
|
qmckl_context qmckl_context_set_precision(const qmckl_context context, int precision) {
|
2020-10-14 00:52:50 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
** =qmckl_context_set_range=
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.h
|
2020-10-15 09:22:24 +02:00
|
|
|
qmckl_context qmckl_context_set_range(const qmckl_context context, int range);
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
2020-10-15 09:22:24 +02:00
|
|
|
qmckl_context qmckl_context_set_range(const qmckl_context context, int range) {
|
2020-10-14 00:52:50 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
** =qmckl_context_get_precision=
|
2020-10-14 00:52:50 +02:00
|
|
|
|
2020-10-14 09:54:12 +02:00
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.h
|
2020-10-15 09:22:24 +02:00
|
|
|
int qmckl_context_get_precision(const qmckl_context context);
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
2020-10-15 09:22:24 +02:00
|
|
|
int qmckl_context_get_precision(const qmckl_context context) {
|
2020-10-14 09:54:12 +02:00
|
|
|
qmckl_context_struct* ctx;
|
|
|
|
ctx = (qmckl_context_struct*) context;
|
|
|
|
return ctx->precision;
|
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
** =qmckl_context_get_range=
|
|
|
|
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.h
|
2020-10-15 09:22:24 +02:00
|
|
|
int qmckl_context_get_range(const qmckl_context context);
|
2020-10-14 09:54:12 +02:00
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
#+BEGIN_SRC C :tangle qmckl_context.c
|
2020-10-15 09:22:24 +02:00
|
|
|
int qmckl_context_get_range(const qmckl_context context) {
|
2020-10-14 09:54:12 +02:00
|
|
|
qmckl_context_struct* ctx;
|
|
|
|
ctx = (qmckl_context_struct*) context;
|
|
|
|
return ctx->range;
|
|
|
|
}
|
|
|
|
#+END_SRC
|
2020-10-14 00:52:50 +02:00
|
|
|
|