/***************************************************************************/
/*                             Druckersoftware                             */
/*                    Copyright (C) Carl Valentin GmbH                     */
/***************************************************************************/

/*
 * KM_bit_utils.h
 *
 *  Created on: 22.07.2022
 *      Author: preitinger
 */

#ifndef KM_BIT_UTILS_H_
#define KM_BIT_UTILS_H_

#include "KM_CvKernelAccess.h"

#include <linux/types.h>
#include <linux/bits.h>


static inline void replaceBits(uint16_t* dst, int dstLo, int src, int srcLo, int len) {
    int diff = len - 1;
    int dstHi = dstLo + diff;
    int srcHi = srcLo + diff;

    ASSERT(GENMASK(0, 0) == 1);
    ASSERT(GENMASK(2, 1) == 6);
    *dst &= ~GENMASK(dstHi, dstLo);
    *dst |= (((src & GENMASK(srcHi, srcLo)) >> srcLo) << dstLo);
}

static inline void setBits(uint16_t* dst, int dstLo, int src, int srcLo, int len) {
    int diff = len - 1;
    int srcHi = srcLo + diff;

    ASSERT(GENMASK(0, 0) == 1);
    ASSERT(GENMASK(2, 1) == 6);
    *dst |= (((src & GENMASK(srcHi, srcLo)) >> srcLo) << dstLo);
}


#endif /* KM_BIT_UTILS_H_ */
