SphinxBase  5prealpha
bitarr.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2015 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 
38 /*
39  * bitarr.c -- Bit array manipulations implementation.
40  */
41 
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45 #include "sphinxbase/bitarr.h"
46 
47 #define SIGN_BIT (0x80000000)
48 
56 static uint8 get_shift(uint8 bit, uint8 length)
57 {
58 #ifdef WORDS_BIGENDIAN
59  return 64 - length - bit;
60 #else
61  return bit;
62 #endif
63 }
64 
70 static uint64 read_off(bitarr_address_t address)
71 {
72 #if defined(__arm) || defined(__arm__)
73  uint64 value64;
74  const uint8 *base_off = (const uint8 *)(address.base) + (address.offset >> 3);
75  memcpy(&value64, base_off, sizeof(value64));
76  return value64;
77 #else
78  return *(const uint64*)((const uint8 *)(address.base) + (address.offset >> 3));
79 #endif
80 }
81 
82 uint64 bitarr_read_int57(bitarr_address_t address, uint8 length, uint64 mask)
83 {
84  return (read_off(address) >> get_shift(address.offset & 7, length)) & mask;
85 }
86 
87 void bitarr_write_int57(bitarr_address_t address, uint8 length, uint64 value)
88 {
89 #if defined(__arm) || defined(__arm__)
90  uint64 value64;
91  uint8 *base_off = (uint8 *)(address.base) + (address.offset >> 3);
92  memcpy(&value64, base_off, sizeof(value64));
93  value64 |= (value << get_shift(address.offset & 7, length));
94  memcpy(base_off, &value64, sizeof(value64));
95 #else
96  *(uint64 *)((uint8 *)(address.base) + (address.offset >> 3)) |= (value << get_shift(address.offset & 7, length));
97 #endif
98 }
99 
100 uint32 bitarr_read_int25(bitarr_address_t address, uint8 length, uint32 mask)
101 {
102 #if defined(__arm) || defined(__arm__)
103  uint32 value32;
104  const uint8 *base_off = (const uint8*)(address.base) + (address.offset >> 3);
105  memcpy(&value32, base_off, sizeof(value32));
106  return (value32 >> get_shift(address.offset & 7, length)) & mask;
107 #else
108  return (*(const uint32*)((const uint8*)(address.base) + (address.offset >> 3)) >> get_shift(address.offset & 7, length)) & mask;
109 #endif
110 }
111 
112 void bitarr_write_int25(bitarr_address_t address, uint8 length, uint32 value)
113 {
114 #if defined(__arm) || defined(__arm__)
115  uint32 value32;
116  uint8 *base_off = (uint8 *)(address.base) + (address.offset >> 3);
117  memcpy(&value32, base_off, sizeof(value32));
118  value32 |= (value << get_shift(address.offset & 7, length));
119  memcpy(base_off, &value32, sizeof(value32));
120 #else
121  *(uint32 *)((uint8 *)(address.base) + (address.offset >> 3)) |= (value << get_shift(address.offset & 7, length));
122 #endif
123 }
124 
125 void bitarr_mask_from_max(bitarr_mask_t *bit_mask, uint32 max_value)
126 {
127  bit_mask->bits = bitarr_required_bits(max_value);
128  bit_mask->mask = (uint32)((1ULL << bit_mask->bits) - 1);
129 }
130 
131 uint8 bitarr_required_bits(uint32 max_value)
132 {
133  uint8 res;
134 
135  if (!max_value) return 0;
136  res = 1;
137  while (max_value >>= 1) res++;
138  return res;
139 }
SPHINXBASE_EXPORT void bitarr_mask_from_max(bitarr_mask_t *bit_mask, uint32 max_value)
Fills mask for certain int range according to provided max value.
Definition: bitarr.c:125
SPHINXBASE_EXPORT uint8 bitarr_required_bits(uint32 max_value)
Computes amount of bits required ti store integers upto value provided.
Definition: bitarr.c:131
SPHINXBASE_EXPORT void bitarr_write_int25(bitarr_address_t address, uint8 length, uint32 value)
Write specified value into bit array.
Definition: bitarr.c:112
Structure that stores address of certain value in bit array.
Definition: bitarr.h:73
SPHINXBASE_EXPORT uint32 bitarr_read_int25(bitarr_address_t address, uint8 length, uint32 mask)
Read uint32 value from bit array.
Definition: bitarr.c:100
SPHINXBASE_EXPORT void bitarr_write_int57(bitarr_address_t address, uint8 length, uint64 value)
Write specified value into bit array.
Definition: bitarr.c:87
Structure that specifies bits required to efficiently store certain data.
Definition: bitarr.h:65
An implementation bit array - memory efficient storage for digit int and float data.
SPHINXBASE_EXPORT uint64 bitarr_read_int57(bitarr_address_t address, uint8 length, uint64 mask)
Read uint64 value from bit array.
Definition: bitarr.c:82