2 * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
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.
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.
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
19 * $Id: bitstring.ml 146 2008-08-20 16:58:33Z richard.wm.jones $
22 /* This file contains hand-coded, optimized C implementations of
23 * certain very frequently used functions.
33 #include <caml/mlvalues.h>
34 #include <caml/fail.h>
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
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.
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.
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
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".
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)
65 #define swap_be(size,v) v = bswap_##size (v)
66 #define swap_le(size,v)
67 #define swap_ne(size,v)
70 #define fastpath1(size,endian,signed,type) \
72 ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed \
73 (value strv, value offv) \
75 type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
78 swap_##endian(size,r); \
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)
89 /* XXX This probably doesn't work on ARCH_ALIGN_INT64 platforms. */
91 #define fastpath2(size,endian,signed,type,rval) \
93 ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed \
94 (value strv, value offv, value rv) \
96 type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
99 swap_##endian(size,r); \
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)
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)