Add support for ECL long double
[cffi.git] / grovel / common.h
bloba7703e43d4ccbe244effb49f86c83accba19e307
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <inttypes.h>
6 #include <stddef.h>
8 #ifndef offsetof
9 #define offsetof(type, slot) ((long) ((char *) &(((type *) 0)->slot)))
10 #endif
11 #define getslot(type, slot) (((type *) 0)->slot)
12 #define sizeofslot(type, slot) (sizeof(getslot(type, slot)))
13 #define countofslot(type, slot) \
14 (sizeof(getslot(type, slot)) / sizeof(getslot(type, slot)[0]))
15 #define stringify(x) #x
16 #define indirect_stringify(x) stringify(x)
18 #define TYPE_SIGNED_P(type) (((type)-1)<0LL)
19 #define _64_BIT_VALUE_FITS_SIGNED_P(value) ( (value) <= 0x7FFFFFFFFFFFFFFFLL )
20 #define SLOT_SIGNED_P(result, type, slot) \
21 do { \
22 type slot_signed_p_struct; \
23 slot_signed_p_struct.slot = -1; \
24 (result) = slot_signed_p_struct.slot < 0; \
25 } while (0)
27 void type_name(FILE *output, int signed_p, int size) {
28 if (signed_p) {
29 switch (size) {
30 case 1: fprintf(output, ":int8"); break;
31 case 2: fprintf(output, ":int16"); break;
32 case 4: fprintf(output, ":int32"); break;
33 case 8: fprintf(output, ":int64"); break;
34 default: goto error;
36 } else {
37 switch(size) {
38 case 1: fprintf(output, ":uint8"); break;
39 case 2: fprintf(output, ":uint16"); break;
40 case 4: fprintf(output, ":uint32"); break;
41 case 8: fprintf(output, ":uint64"); break;
42 default: goto error;
46 return;
48 error:
49 fprintf(output, "(cl:error \"No type of size ~D.\" %i)\n", size);
52 char* print_double_for_lisp(double n)
54 static char buf[256];
55 memset(buf, 0, 256);
56 snprintf(buf, 255, "(let ((*read-default-float-format* 'double-float)) (coerce (read-from-string \"%.20E\") 'double-float))", n);
57 return buf;