This patch adds the framework for including C code in bitstring.
[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 <stdio.h>
27 #include <stdlib.h>
28
29 #include <caml/mlvalues.h>
30 #include <caml/fail.h>
31
32 /* Return a mask of 0-31 bits wide. */
33 CAMLprim value
34 ocaml_bitstring_I_mask (value bitsv)
35 {
36   int bits = Int_val (bitsv);
37
38   if (bits <= 31)
39     return Val_int ((1 << bits) - 1);
40   else
41     caml_invalid_argument ("Bitstring.I.mask");
42 }