/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright by the Board of Trustees of the University of Illinois. * * All rights reserved. * * * * This file is part of HDF5. The full HDF5 copyright notice, including * * terms governing use, modification, and redistribution, is contained in * * the files COPYING and Copyright.html. COPYING can be found at the root * * of the source code distribution tree; Copyright.html can be found at the * * root level of an installed copy of the electronic HDF5 document set and * * is linked from the top-level documents page. It can also be found at * * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have * * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * Module Info: Operations on bit vectors. A bit vector is an array of bytes * with the least-significant bits in the first byte. That is, * the bytes are in little-endian order. */ #define H5T_PACKAGE /*suppress error about including H5Tpkg */ /* Pablo information */ /* (Put before include files to avoid problems with inline functions) */ #define PABLO_MASK H5Tbit_mask #include "H5private.h" /*generic functions */ #include "H5Eprivate.h" /*error handling */ #include "H5Iprivate.h" /*ID functions */ #include "H5MMprivate.h" /* Memory management */ #include "H5Tpkg.h" /*data-type functions */ /* Interface initialization */ static int interface_initialize_g = 0; #define INTERFACE_INIT NULL /*------------------------------------------------------------------------- * Function: H5T_bit_copy * * Purpose: Copies bits from one vector to another. * * Return: void * * Programmer: Robb Matzke * Wednesday, June 10, 1998 * * Modifications: * *------------------------------------------------------------------------- */ void H5T_bit_copy (uint8_t *dst, size_t dst_offset, const uint8_t *src, size_t src_offset, size_t size) { int shift; unsigned mask_lo, mask_hi; int s_idx, d_idx; /* * Normalize the offset to be a byte number and a bit offset within that * byte. */ s_idx = (int)src_offset / 8; d_idx = (int)dst_offset / 8; src_offset %= 8; dst_offset %= 8; /* * Get things rolling. This means copying bits until we're aligned on a * source byte. This the following example, five bits are copied to the * destination. * * src[s_idx] * +---------------+---------------+ * |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0| * +---------------+---------------+ * ... : : : : : | | | | | * ... v v v v v V V V V V * ...+---------------+---------------+ * ...|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0| ...+---------------+---------------+ * dst[d_idx+1] dst[d_idx] */ while (src_offset && size>0) { unsigned nbits = (unsigned)MIN3 (size, 8-dst_offset, 8-src_offset); unsigned mask = (1<>src_offset)&mask) << dst_offset; src_offset += nbits; if (src_offset>=8) { s_idx++; src_offset %= 8; } dst_offset += nbits; if (dst_offset>=8) { d_idx++; dst_offset %= 8; } size -= nbits; } /* * The middle bits. We are aligned on a source byte which needs to be * copied to two (or one in the degenerate case) destination bytes. * * src[s_idx] * +---------------+ * |7 6 5 4 3 2 1 0| * +---------------+ * | | | | | | | | * V V V V V V V V * +---------------+---------------+ * |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0| * +---------------+---------------+ * dst[d_idx+1] dst[d_idx] * * * Calculate shifts and masks. See diagrams below. MASK_LO in this * example is 0x1f (the low five bits) and MASK_HI is 0xe0 (the high three * bits). SHIFT is three since the source must be shifted right three bits * to line up with the destination. */ shift = (int)dst_offset; mask_lo = (1<<(8-shift))-1; mask_hi = (~mask_lo) & 0xff; for (/*void*/; size>8; size-=8, d_idx++, s_idx++) { if (shift) { dst[d_idx+0] &= ~(mask_lo<>(8-shift)); dst[d_idx+1] |= (src[s_idx] & mask_hi) >> (8-shift); } else { dst[d_idx] = src[s_idx]; } } /* Finish up */ while (size>0) { unsigned nbits = (unsigned)MIN3 (size, 8-dst_offset, 8-src_offset); unsigned mask = (1<>src_offset)&mask) << dst_offset; src_offset += nbits; if (src_offset>=8) { s_idx++; src_offset %= 8; } dst_offset += nbits; if (dst_offset>=8) { d_idx++; dst_offset %= 8; } size -= nbits; } } /*------------------------------------------------------------------------- * Function: H5T_bit_shift * * Purpose: Simulation of hardware shifting. Shifts a bit vector * in a way similar to shifting a variable value, like * value <<= 3, or value >>= 16. SHIFT_DIST is positive for * left shift, negative for right shift. * * Return: void * * Programmer: Raymond Lu * Wednesday, Febuary 4, 2004 * * Modifications: * * Can generalize it to handle a bit vector of any START * position and any SIZE. * *------------------------------------------------------------------------- */ void H5T_bit_shift (uint8_t *buf, ssize_t shift_dist, size_t buf_size) { uint8_t *tmp_buf; size_t buf_size_bit = 8*buf_size; FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5T_bit_shift); /* Sanity check */ assert(buf); assert(buf_size); if(!shift_dist) goto done; if(ABS(shift_dist) >= buf_size_bit) { HDmemset(buf,0,buf_size); goto done; } tmp_buf = (uint8_t*)H5MM_calloc(buf_size); assert(tmp_buf); /* Shift vector by making copies */ if(shift_dist > 0) /* left shift */ H5T_bit_copy (tmp_buf, (size_t)shift_dist, buf, 0, (size_t)(buf_size_bit-shift_dist)); else /* right shift */ H5T_bit_copy(tmp_buf, 0, buf, (size_t)-shift_dist, (size_t)(buf_size_bit+shift_dist)); /* Copy back the vector */ HDmemcpy(buf,tmp_buf,buf_size); /* Free temporary buffer */ H5MM_xfree(tmp_buf); done: FUNC_LEAVE_NOAPI_VOID } /*------------------------------------------------------------------------- * Function: H5T_bit_get_d * * Purpose: Return a small bit sequence as a number. Bit vector starts * at OFFSET and is SIZE bits long. * * Return: Success: The bit sequence interpretted as an unsigned * integer. * * Failure: 0 * * Programmer: Robb Matzke * Tuesday, June 23, 1998 * * Modifications: * *------------------------------------------------------------------------- */ hsize_t H5T_bit_get_d (uint8_t *buf, size_t offset, size_t size) { hsize_t val=0; size_t i, hs; hsize_t ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5T_bit_get_d, 0); assert (8*sizeof(val)>=size); H5T_bit_copy ((uint8_t*)&val, 0, buf, offset, size); switch (H5T_native_order_g) { case H5T_ORDER_LE: break; case H5T_ORDER_BE: for (i=0, hs=sizeof(val)/2; i=size); switch (H5T_native_order_g) { case H5T_ORDER_LE: break; case H5T_ORDER_BE: for (i=0, hs=sizeof(val)/2; i=8) { buf[idx++] = value ? 0xff : 0x00; size -= 8; } /* The last partial byte */ if (size) { if (value) { buf[idx] |= (1<0; iu++, size--) { if (value==(hbool_t)((buf[idx]>>iu) & 0x01)) HGOTO_DONE(8*idx+(ssize_t)iu - base); } offset = 0; idx++; } /* Middle */ while (size>=8) { if ((value?0x00:0xff)!=buf[idx]) { for (i=0; i<8; i++) { if (value==(hbool_t)((buf[idx]>>i) & 0x01)) HGOTO_DONE(8*idx+i - base); } } size -= 8; idx++; } /* End */ for (i=0; i<(ssize_t)size; i++) { if (value==(hbool_t)((buf[idx]>>i) & 0x01)) HGOTO_DONE(8*idx+i - base); } break; case H5T_BIT_MSB: /* Calculate index */ idx = (ssize_t)((offset+size-1) / 8); offset %= 8; /* Beginning */ if (size>8-offset && (offset+size)%8) { for (iu=(offset+size)%8; iu>0; --iu, --size) { if (value==(hbool_t)((buf[idx]>>(iu-1)) & 0x01)) HGOTO_DONE(8*idx+(ssize_t)(iu-1) - base); } --idx; } /* Middle */ while (size>=8) { if ((value?0x00:0xff)!=buf[idx]) { for (i=7; i>=0; --i) { if (value==(hbool_t)((buf[idx]>>i) & 0x01)) HGOTO_DONE(8*idx+i - base); } } size -= 8; --idx; } /* End */ if (size>0) { for (iu=offset+size; iu>offset; --iu) { if (value==(hbool_t)((buf[idx]>>(iu-1)) & 0x01)) HGOTO_DONE(8*idx+(ssize_t)(iu-1) - base); } } break; } done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_bit_inc * * Purpose: Increment part of a bit field by adding 1. The bit field * starts with bit position START and is SIZE bits long. * * Return: Success: The carry-out value, one if overflow zero * otherwise. * * Failure: Negative * * Programmer: Robb Matzke * Friday, June 26, 1998 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5T_bit_inc(uint8_t *buf, size_t start, size_t size) { size_t idx = start / 8; unsigned carry = 1; unsigned acc, mask; /* Use FUNC_ENTER_NOAPI_NOINIT_NOFUNC here to avoid performance issues */ FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5T_bit_inc); assert(buf); start %= 8; /* The first partial byte */ if (start) { if (size+start<8) mask = (1<>start) & mask; acc += 1; carry = acc & (1<=8) { acc = buf[idx]; acc += 1; carry = acc & 0x100; buf[idx] = acc & 0xff; idx++; size -= 8; } /* The last bits */ if (carry && size>0) { mask = (1<