Blame source/uds/bits.c

Packit Service 310c69
/*
Packit Service 310c69
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service 310c69
 *
Packit Service 310c69
 * This program is free software; you can redistribute it and/or
Packit Service 310c69
 * modify it under the terms of the GNU General Public License
Packit Service 310c69
 * as published by the Free Software Foundation; either version 2
Packit Service 310c69
 * of the License, or (at your option) any later version.
Packit Service 310c69
 * 
Packit Service 310c69
 * This program is distributed in the hope that it will be useful,
Packit Service 310c69
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 310c69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 310c69
 * GNU General Public License for more details.
Packit Service 310c69
 * 
Packit Service 310c69
 * You should have received a copy of the GNU General Public License
Packit Service 310c69
 * along with this program; if not, write to the Free Software
Packit Service 310c69
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 310c69
 * 02110-1301, USA. 
Packit Service 310c69
 *
Packit Service 310c69
 * $Id: //eng/uds-releases/jasper/src/uds/bits.c#1 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#include "bits.h"
Packit Service 310c69
Packit Service 310c69
#include "compiler.h"
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * This is the largest field size supported by getBigField & setBigField.
Packit Service 310c69
 * Any field that is larger is not guaranteed to fit in a single, byte
Packit Service 310c69
 * aligned uint64_t.
Packit Service 310c69
 **/
Packit Service 310c69
enum { MAX_BIG_FIELD_BITS = (sizeof(uint64_t) - 1) * CHAR_BIT + 1 };
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Get a big bit field from a bit stream
Packit Service 310c69
 *
Packit Service 310c69
 * @param memory  The base memory byte address
Packit Service 310c69
 * @param offset  The bit offset into the memory for the start of the field
Packit Service 310c69
 * @param size    The number of bits in the field
Packit Service 310c69
 *
Packit Service 310c69
 * @return the bit field
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE uint64_t getBigField(const byte *memory,
Packit Service 310c69
                                   uint64_t    offset,
Packit Service 310c69
                                   int         size)
Packit Service 310c69
{
Packit Service 310c69
  const void *addr = memory + offset / CHAR_BIT;
Packit Service 310c69
  return (getUInt64LE(addr) >> (offset % CHAR_BIT)) & ((1UL << size) - 1);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Set a big bit field in a bit stream
Packit Service 310c69
 *
Packit Service 310c69
 * @param value   The value to put into the field
Packit Service 310c69
 * @param memory  The base memory byte address
Packit Service 310c69
 * @param offset  The bit offset into the memory for the start of the field
Packit Service 310c69
 * @param size    The number of bits in the field
Packit Service 310c69
 *
Packit Service 310c69
 * @return the bit field
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE void setBigField(uint64_t value, byte *memory, uint64_t offset,
Packit Service 310c69
                               int size)
Packit Service 310c69
{
Packit Service 310c69
  void *addr = memory + offset / CHAR_BIT;
Packit Service 310c69
  int shift = offset % CHAR_BIT;
Packit Service 310c69
  uint64_t data = getUInt64LE(addr);
Packit Service 310c69
  data &= ~(((1UL << size) - 1) << shift);
Packit Service 310c69
  data |= value << shift;
Packit Service 310c69
  storeUInt64LE(addr, data);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/***********************************************************************/
Packit Service 310c69
void getBytes(const byte *memory, uint64_t offset, byte *destination, int size)
Packit Service 310c69
{
Packit Service 310c69
  const byte *addr = memory + offset / CHAR_BIT;
Packit Service 310c69
  int shift = offset % CHAR_BIT;
Packit Service 310c69
  while (--size >= 0) {
Packit Service 310c69
    *destination++ = getUInt16LE(addr++) >> shift;
Packit Service 310c69
  }
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/***********************************************************************/
Packit Service 310c69
void setBytes(byte *memory, uint64_t offset, const byte *source, int size)
Packit Service 310c69
{
Packit Service 310c69
  byte *addr = memory + offset / CHAR_BIT;
Packit Service 310c69
  int shift = offset % CHAR_BIT;
Packit Service 310c69
  uint16_t mask = ~((uint16_t) 0xFF << shift);
Packit Service 310c69
  while (--size >= 0) {
Packit Service 310c69
    uint16_t data = (getUInt16LE(addr) & mask) | (*source++ << shift);
Packit Service 310c69
    storeUInt16LE(addr++, data);
Packit Service 310c69
  }
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/***********************************************************************/
Packit Service 310c69
void moveBits(const byte *sMemory, uint64_t source, byte *dMemory,
Packit Service 310c69
              uint64_t destination, int size)
Packit Service 310c69
{
Packit Service 310c69
  enum { UINT32_BIT = sizeof(uint32_t) * CHAR_BIT };
Packit Service 310c69
  if (size > MAX_BIG_FIELD_BITS) {
Packit Service 310c69
    if (source > destination) {
Packit Service 310c69
      // This is a large move from a higher to a lower address.  We move
Packit Service 310c69
      // the lower addressed bits first.  Start by moving one field that
Packit Service 310c69
      // ends on a destination int boundary
Packit Service 310c69
      int count
Packit Service 310c69
        = MAX_BIG_FIELD_BITS - (destination + MAX_BIG_FIELD_BITS) % UINT32_BIT;
Packit Service 310c69
      uint64_t field = getBigField(sMemory, source, count);
Packit Service 310c69
      setBigField(field, dMemory, destination, count);
Packit Service 310c69
      source      += count;
Packit Service 310c69
      destination += count;
Packit Service 310c69
      size        -= count;
Packit Service 310c69
      // Now do the main loop to copy 32 bit chunks that are int-aligned
Packit Service 310c69
      // at the destination.
Packit Service 310c69
      int offset = source % UINT32_BIT;
Packit Service 310c69
      const byte *src = sMemory + (source - offset) / CHAR_BIT;
Packit Service 310c69
      byte *dest = dMemory + destination / CHAR_BIT;
Packit Service 310c69
      while (size > MAX_BIG_FIELD_BITS) {
Packit Service 310c69
        storeUInt32LE(dest, getUInt64LE(src) >> offset);
Packit Service 310c69
        src  += sizeof(uint32_t);
Packit Service 310c69
        dest += sizeof(uint32_t);
Packit Service 310c69
        source      += UINT32_BIT;
Packit Service 310c69
        destination += UINT32_BIT;
Packit Service 310c69
        size        -= UINT32_BIT;
Packit Service 310c69
      }
Packit Service 310c69
    } else {
Packit Service 310c69
      // This is a large move from a lower to a higher address.  We move
Packit Service 310c69
      // the higher addressed bits first.  Start by moving one field that
Packit Service 310c69
      // begins on a destination int boundary
Packit Service 310c69
      int count = (destination + size) % UINT32_BIT;
Packit Service 310c69
      if (count > 0) {
Packit Service 310c69
        size -= count;
Packit Service 310c69
        uint64_t field = getBigField(sMemory, source + size, count);
Packit Service 310c69
        setBigField(field, dMemory, destination + size, count);
Packit Service 310c69
      }
Packit Service 310c69
      // Now do the main loop to copy 32 bit chunks that are int-aligned
Packit Service 310c69
      // at the destination.
Packit Service 310c69
      int offset = (source + size) % UINT32_BIT;
Packit Service 310c69
      const byte *src = sMemory + (source + size - offset) / CHAR_BIT;
Packit Service 310c69
      byte *dest = dMemory + (destination + size) / CHAR_BIT;
Packit Service 310c69
      while (size > MAX_BIG_FIELD_BITS) {
Packit Service 310c69
        src  -= sizeof(uint32_t);
Packit Service 310c69
        dest -= sizeof(uint32_t);
Packit Service 310c69
        size -= UINT32_BIT;
Packit Service 310c69
        storeUInt32LE(dest, getUInt64LE(src) >> offset);
Packit Service 310c69
      }
Packit Service 310c69
    }
Packit Service 310c69
  }
Packit Service 310c69
  // Finish up by doing the last chunk, which can have any arbitrary alignment
Packit Service 310c69
  if (size > 0) {
Packit Service 310c69
    uint64_t field = getBigField(sMemory, source, size);
Packit Service 310c69
    setBigField(field, dMemory, destination, size);
Packit Service 310c69
  }
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/***********************************************************************/
Packit Service 310c69
bool sameBits(const byte *mem1, uint64_t offset1, const byte *mem2,
Packit Service 310c69
              uint64_t offset2, int size)
Packit Service 310c69
{
Packit Service 310c69
  while (size >= MAX_FIELD_BITS) {
Packit Service 310c69
    unsigned int field1 = getField(mem1, offset1, MAX_FIELD_BITS);
Packit Service 310c69
    unsigned int field2 = getField(mem2, offset2, MAX_FIELD_BITS);
Packit Service 310c69
    if (field1 != field2) return false;
Packit Service 310c69
    offset1 += MAX_FIELD_BITS;
Packit Service 310c69
    offset2 += MAX_FIELD_BITS;
Packit Service 310c69
    size    -= MAX_FIELD_BITS;
Packit Service 310c69
  }
Packit Service 310c69
  if (size > 0) {
Packit Service 310c69
    unsigned int field1 = getField(mem1, offset1, size);
Packit Service 310c69
    unsigned int field2 = getField(mem2, offset2, size);
Packit Service 310c69
    if (field1 != field2) return false;
Packit Service 310c69
  }
Packit Service 310c69
  return true;
Packit Service 310c69
}