configure: Don't test for camlp4of.opt, test for camlp4of.
[ocaml-bitstring.git] / bitstring_c.c
1 /* Bitstring library.
2  * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version,
8  * with the OCaml linking exception described in COPYING.LIB.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * $Id: bitstring.ml 146 2008-08-20 16:58:33Z richard.wm.jones $
20  */
21
22 /* This file contains hand-coded, optimized C implementations of
23  * certain very frequently used functions.
24  */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <byteswap.h>
32 #include <string.h>
33
34 #include <caml/mlvalues.h>
35 #include <caml/fail.h>
36 #include <caml/memory.h>
37 #include <caml/alloc.h>
38
39 /* Fastpath functions.  These are used in the common case for reading
40  * ints where the following conditions are known to be true:
41  * (a) the int size is a whole number of bytes (eg. 16, 24, 32, etc bits)
42  * (b) the access in the match is byte-aligned
43  * (c) the access in the underlying bitstring is byte-aligned
44  *
45  * These functions used to all be "noalloc" meaning they must not
46  * perform any OCaml allocations.  However starting with OCaml 4.02, a
47  * compiler optimization means that unforunately we now have to use
48  * ordinary alloc functions in some cases.
49  *
50  * The final offset in the string is calculated by the OCaml (caller)
51  * code.  All we need to do is to read the string+offset and byteswap,
52  * sign-extend as necessary.
53  *
54  * There is one function for every combination of:
55  * (i) int size: 16, 32, 64 bits
56  * (ii) endian: bigendian, littleendian, nativeendian
57  * (iii) signed and unsigned
58  *
59  * XXX Future work: Expand this to 24, 40, 48, 56 bits.  This
60  * requires some extra work because sign-extension won't "just happen".
61  */
62
63 #ifdef WORDS_BIGENDIAN
64 #define swap_be(size,v)
65 #define swap_le(size,v) v = bswap_##size (v)
66 #define swap_ne(size,v)
67 #else
68 #define swap_be(size,v) v = bswap_##size (v)
69 #define swap_le(size,v)
70 #define swap_ne(size,v)
71 #endif
72
73 #define fastpath1(size,endian,signed,type)                              \
74   CAMLprim value                                                        \
75   ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed      \
76   (value strv, value offv)                                              \
77   {                                                                     \
78     type *ptr = (type *) ((char *) String_val (strv) + Int_val (offv)); \
79     type r;                                                             \
80     memcpy(&r, ptr, sizeof(r));                                 \
81     swap_##endian(size,r);                                              \
82     return Val_int (r);                                                 \
83   }
84
85 fastpath1(16,be,unsigned,uint16_t)
86 fastpath1(16,le,unsigned,uint16_t)
87 fastpath1(16,ne,unsigned,uint16_t)
88 fastpath1(16,be,signed,int16_t)
89 fastpath1(16,le,signed,int16_t)
90 fastpath1(16,ne,signed,int16_t)
91
92 #define fastpath2(size,endian,signed,type,copy)                         \
93   CAMLprim value                                                        \
94   ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed      \
95   (value strv, value offv)                                              \
96   {                                                                     \
97     CAMLparam2 (strv, offv);                                            \
98     CAMLlocal1 (rv);                                                    \
99     type *ptr = (type *) ((char *) String_val (strv) + Int_val (offv)); \
100     type r;                                                             \
101     memcpy(&r, ptr, sizeof(r));                                         \
102     swap_##endian(size,r);                                              \
103     rv = copy (r);                                                      \
104     CAMLreturn (rv);                                                    \
105   }
106
107 fastpath2(32,be,unsigned,uint32_t,caml_copy_int32)
108 fastpath2(32,le,unsigned,uint32_t,caml_copy_int32)
109 fastpath2(32,ne,unsigned,uint32_t,caml_copy_int32)
110 fastpath2(32,be,signed,int32_t,caml_copy_int32)
111 fastpath2(32,le,signed,int32_t,caml_copy_int32)
112 fastpath2(32,ne,signed,int32_t,caml_copy_int32)
113
114 fastpath2(64,be,unsigned,uint64_t,caml_copy_int64)
115 fastpath2(64,le,unsigned,uint64_t,caml_copy_int64)
116 fastpath2(64,ne,unsigned,uint64_t,caml_copy_int64)
117 fastpath2(64,be,signed,int64_t,caml_copy_int64)
118 fastpath2(64,le,signed,int64_t,caml_copy_int64)
119 fastpath2(64,ne,signed,int64_t,caml_copy_int64)