Updated test coverage report.
[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
33 #include <caml/mlvalues.h>
34 #include <caml/fail.h>
35
36 /* Fastpath functions.  These are used in the common case for reading
37  * ints where the following conditions are known to be true:
38  * (a) the int size is a whole number of bytes (eg. 16, 24, 32, etc bits)
39  * (b) the access in the match is byte-aligned
40  * (c) the access in the underlying bitstring is byte-aligned
41  *
42  * These functions are all "noalloc" meaning they must not perform
43  * any OCaml allocations.  For this reason, when the function returns
44  * an int32 or int64, the OCaml code passes in the pre-allocated pointer
45  * to the return value.
46  *
47  * The final offset in the string is calculated by the OCaml (caller)
48  * code.  All we need to do is to read the string+offset and byteswap,
49  * sign-extend as necessary.
50  *
51  * There is one function for every combination of:
52  * (i) int size: 16, 32, 64 bits
53  * (ii) endian: bigendian, littleendian, nativeendian
54  * (iii) signed and unsigned
55  *
56  * XXX Future work: Expand this to 24, 40, 48, 56 bits.  This
57  * requires some extra work because sign-extension won't "just happen".
58  */
59
60 #ifdef WORDS_BIGENDIAN
61 #define swap_be(size,v)
62 #define swap_le(size,v) v = bswap_##size (v)
63 #define swap_ne(size,v)
64 #else
65 #define swap_be(size,v) v = bswap_##size (v)
66 #define swap_le(size,v)
67 #define swap_ne(size,v)
68 #endif
69
70 #define fastpath1(size,endian,signed,type)                              \
71   CAMLprim value                                                        \
72   ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed      \
73   (value strv, value offv)                                              \
74   {                                                                     \
75     type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
76     type r;                                                             \
77     r = *ptr;                                                           \
78     swap_##endian(size,r);                                              \
79     return Val_int (r);                                                 \
80   }
81
82 fastpath1(16,be,unsigned,uint16_t)
83 fastpath1(16,le,unsigned,uint16_t)
84 fastpath1(16,ne,unsigned,uint16_t)
85 fastpath1(16,be,signed,int16_t)
86 fastpath1(16,le,signed,int16_t)
87 fastpath1(16,ne,signed,int16_t)
88
89 /* XXX This probably doesn't work on ARCH_ALIGN_INT64 platforms. */
90
91 #define fastpath2(size,endian,signed,type,rval)                         \
92   CAMLprim value                                                        \
93   ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed      \
94   (value strv, value offv, value rv)                                    \
95   {                                                                     \
96     type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
97     type r;                                                             \
98     r = *ptr;                                                           \
99     swap_##endian(size,r);                                              \
100     rval(rv) = r;                                                       \
101     return rv;                                                          \
102   }
103
104 fastpath2(32,be,unsigned,uint32_t,Int32_val)
105 fastpath2(32,le,unsigned,uint32_t,Int32_val)
106 fastpath2(32,ne,unsigned,uint32_t,Int32_val)
107 fastpath2(32,be,signed,int32_t,Int32_val)
108 fastpath2(32,le,signed,int32_t,Int32_val)
109 fastpath2(32,ne,signed,int32_t,Int32_val)
110
111 fastpath2(64,be,unsigned,uint64_t,Int64_val)
112 fastpath2(64,le,unsigned,uint64_t,Int64_val)
113 fastpath2(64,ne,unsigned,uint64_t,Int64_val)
114 fastpath2(64,be,signed,int64_t,Int64_val)
115 fastpath2(64,le,signed,int64_t,Int64_val)
116 fastpath2(64,ne,signed,int64_t,Int64_val)