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.
34 #include <caml/mlvalues.h>
35 #include <caml/fail.h>
37 /* Fastpath functions. These are used in the common case for reading
38 * ints where the following conditions are known to be true:
39 * (a) the int size is a whole number of bytes (eg. 16, 24, 32, etc bits)
40 * (b) the access in the match is byte-aligned
41 * (c) the access in the underlying bitstring is byte-aligned
43 * These functions are all "noalloc" meaning they must not perform
44 * any OCaml allocations. For this reason, when the function returns
45 * an int32 or int64, the OCaml code passes in the pre-allocated pointer
46 * to the return value.
48 * The final offset in the string is calculated by the OCaml (caller)
49 * code. All we need to do is to read the string+offset and byteswap,
50 * sign-extend as necessary.
52 * There is one function for every combination of:
53 * (i) int size: 16, 32, 64 bits
54 * (ii) endian: bigendian, littleendian, nativeendian
55 * (iii) signed and unsigned
57 * XXX Future work: Expand this to 24, 40, 48, 56 bits. This
58 * requires some extra work because sign-extension won't "just happen".
61 #ifdef WORDS_BIGENDIAN
62 #define swap_be(size,v)
63 #define swap_le(size,v) v = bswap_##size (v)
64 #define swap_ne(size,v)
66 #define swap_be(size,v) v = bswap_##size (v)
67 #define swap_le(size,v)
68 #define swap_ne(size,v)
71 #define fastpath1(size,endian,signed,type) \
73 ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed \
74 (value strv, value offv) \
76 type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
78 memcpy(&r, ptr, sizeof(r)); \
79 swap_##endian(size,r); \
83 fastpath1(16,be,unsigned,uint16_t)
84 fastpath1(16,le,unsigned,uint16_t)
85 fastpath1(16,ne,unsigned,uint16_t)
86 fastpath1(16,be,signed,int16_t)
87 fastpath1(16,le,signed,int16_t)
88 fastpath1(16,ne,signed,int16_t)
90 #define fastpath2(size,endian,signed,type,rval) \
92 ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed \
93 (value strv, value offv, value rv) \
95 type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
97 memcpy(&r, ptr, sizeof(r)); \
98 swap_##endian(size,r); \
103 fastpath2(32,be,unsigned,uint32_t,Int32_val)
104 fastpath2(32,le,unsigned,uint32_t,Int32_val)
105 fastpath2(32,ne,unsigned,uint32_t,Int32_val)
106 fastpath2(32,be,signed,int32_t,Int32_val)
107 fastpath2(32,le,signed,int32_t,Int32_val)
108 fastpath2(32,ne,signed,int32_t,Int32_val)
110 /* Special care needs to be taken on ARCH_ALIGN_INT64 platforms
111 (hppa and sparc in Debian). */
113 #ifdef ARCH_ALIGN_INT64
114 #include <caml/memory.h>
115 #include <caml/alloc.h>
116 #define fastpath3(size,endian,signed,type,rval) \
118 ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed \
119 (value strv, value offv, value rv) \
121 type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
123 memcpy(&r, ptr, sizeof(r)); \
124 swap_##endian(size,r); \
125 memcpy(Data_custom_val(rv), &r, sizeof(r)); \
130 #define fastpath3 fastpath2
133 fastpath3(64,be,unsigned,uint64_t,Int64_val)
134 fastpath3(64,le,unsigned,uint64_t,Int64_val)
135 fastpath3(64,ne,unsigned,uint64_t,Int64_val)
136 fastpath3(64,be,signed,int64_t,Int64_val)
137 fastpath3(64,le,signed,int64_t,Int64_val)
138 fastpath3(64,ne,signed,int64_t,Int64_val)