1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-09-20 04:24:48 +00:00

Moves bitops to separate file

This commit is contained in:
Matthew Kilgore 2024-02-13 01:11:29 -05:00
parent cd8a388d62
commit def3ada041
5 changed files with 115 additions and 108 deletions

View file

@ -13,6 +13,7 @@
#endif
#include "audio.h"
#include "bitops.h"
#include "cmem.h"
#include "completion.h"
#include "command.h"
@ -54,30 +55,6 @@ uint32 rotateLeft(uint32 word, uint32 shift) { return (word << shift) | (word >>
#ifndef QB64_WINDOWS
void ZeroMemory(void *ptr, int64 bytes) { memset(ptr, 0, bytes); }
#endif
// bit-array access functions (note: used to be included through 'bit.cpp')
uint64 getubits(uint32 bsize, uint8 *base, ptrszint i) {
int64 bmask;
bmask = ~(-(((int64)1) << bsize));
i *= bsize;
return ((*(uint64 *)(base + (i >> 3))) >> (i & 7)) & bmask;
}
int64 getbits(uint32 bsize, uint8 *base, ptrszint i) {
int64 bmask, bval64;
bmask = ~(-(((int64)1) << bsize));
i *= bsize;
bval64 = ((*(uint64 *)(base + (i >> 3))) >> (i & 7)) & bmask;
if (bval64 & (((int64)1) << (bsize - 1)))
return bval64 | (~bmask);
return bval64;
}
void setbits(uint32 bsize, uint8 *base, ptrszint i, int64 val) {
int64 bmask;
uint64 *bptr64;
bmask = (((uint64)1) << bsize) - 1;
i *= bsize;
bptr64 = (uint64 *)(base + (i >> 3));
*bptr64 = (*bptr64 & (((bmask << (i & 7)) ^ -1))) | ((val & bmask) << (i & 7));
}
#ifdef QB64_UNIX
# include <libgen.h> //required for dirname()

View file

@ -1,6 +1,7 @@
libqb-objs-y += $(PATH_LIBQB)/src/threading.o
libqb-objs-y += $(PATH_LIBQB)/src/buffer.o
libqb-objs-y += $(PATH_LIBQB)/src/bitops.o
libqb-objs-y += $(PATH_LIBQB)/src/command.o
libqb-objs-y += $(PATH_LIBQB)/src/filepath.o
libqb-objs-y += $(PATH_LIBQB)/src/filesystem.o

View file

@ -0,0 +1,78 @@
#pragma once
#include <limits.h>
#include <stdint.h>
uint64_t getubits(uint32_t bsize, uint8_t *base, intptr_t i);
int64_t getbits(uint32_t bsize, uint8_t *base, intptr_t i);
void setbits(uint32_t bsize, uint8_t *base, intptr_t i, int64_t val);
// a740g: ROR & ROL additions start
// The rotation functions below are the way they are for a couple of reasons:
// 1. They are safer (well folks seem to think so; see https://en.wikipedia.org/wiki/Circular_shift#Implementing_circular_shifts)
// 2. We are using C library constants and there is just 1 numeric literal - '1'
// 3. GGC recognizes the 'pattern' and will optimize it out to 'roX' and 3 more instructions when using O2
static inline uint8_t func__rol8(uint8_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
static inline uint8_t func__ror8(uint8_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
static inline uint16_t func__rol16(uint16_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
static inline uint16_t func__ror16(uint16_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
static inline uint32_t func__rol32(uint32_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
static inline uint32_t func__ror32(uint32_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
static inline uint64_t func__rol64(uint64_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
static inline uint64_t func__ror64(uint64_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
// a740g: ROR & ROL additions end
// bit-shifting
static inline uint64_t func__shl(uint64_t a1, int b1) { return a1 << b1; }
static inline uint64_t func__shr(uint64_t a1, int b1) { return a1 >> b1; }
static inline int64_t func__readbit(uint64_t a1, int b1) {
if (a1 & 1ull << b1)
return -1;
else
return 0;
}
static inline uint64_t func__setbit(uint64_t a1, int b1) { return a1 | 1ull << b1; }
static inline uint64_t func__resetbit(uint64_t a1, int b1) { return a1 & ~(1ull << b1); }
static inline uint64_t func__togglebit(uint64_t a1, int b1) { return a1 ^ 1ull << b1; }

View file

@ -0,0 +1,33 @@
#include "libqb-common.h"
#include <math.h>
#include "bitops.h"
// bit-array access functions (note: used to be included through 'bit.cpp')
uint64_t getubits(uint32_t bsize, uint8_t *base, intptr_t i) {
int64_t bmask;
bmask = ~(-(((int64_t)1) << bsize));
i *= bsize;
return ((*(uint64_t *)(base + (i >> 3))) >> (i & 7)) & bmask;
}
int64_t getbits(uint32_t bsize, uint8_t *base, intptr_t i) {
int64_t bmask, bval64;
bmask = ~(-(((int64_t)1) << bsize));
i *= bsize;
bval64 = ((*(uint64_t *)(base + (i >> 3))) >> (i & 7)) & bmask;
if (bval64 & (((int64_t)1) << (bsize - 1)))
return bval64 | (~bmask);
return bval64;
}
void setbits(uint32_t bsize, uint8_t *base, intptr_t i, int64_t val) {
int64_t bmask;
uint64_t *bptr64;
bmask = (((uint64_t)1) << bsize) - 1;
i *= bsize;
bptr64 = (uint64_t *)(base + (i >> 3));
*bptr64 = (*bptr64 & (((bmask << (i & 7)) ^ -1))) | ((val & bmask) << (i & 7));
}

View file

@ -1,4 +1,5 @@
#include "audio.h"
#include "bitops.h"
#include "common.h"
#include "compression.h"
#include "command.h"
@ -480,21 +481,11 @@ extern int32 print_using_double(qbs *format, double value, int32 start,
qbs *output);
extern int32 print_using_float(qbs *format, long double value, int32 start,
qbs *output);
// Cobalt(aka Dave) added the next 2 lines
uint64 func__shr(uint64 a1, int b1);
uint64 func__shl(uint64 a1, int b1);
int64 func__readbit(uint64 a1, int b1);
uint64 func__setbit(uint64 a1, int b1);
uint64 func__resetbit(uint64 a1, int b1);
uint64 func__togglebit(uint64 a1, int b1);
#ifndef QB64_WINDOWS
extern void ZeroMemory(void *ptr, int64 bytes);
#endif
extern uint64 getubits(uint32 bsize, uint8 *base, ptrszint i);
extern int64 getbits(uint32 bsize, uint8 *base, ptrszint i);
extern void setbits(uint32 bsize, uint8 *base, ptrszint i, int64 val);
// shared global variables
extern int32 sleep_break;
extern int64 exit_code;
@ -758,79 +749,6 @@ inline int32 func_sgn(long double v) {
return -1;
return 0;
}
// a740g: ROR & ROL additions start
// The rotation functions below are the way they are for a couple of reasons:
// 1. They are safer (well folks seem to think so; see https://en.wikipedia.org/wiki/Circular_shift#Implementing_circular_shifts)
// 2. We are using C library constants and there is just 1 numeric literal - '1'
// 3. GGC recognizes the 'pattern' and will optimize it out to 'roX' and 3 more instructions when using O2
inline uint8_t func__rol8(uint8_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
inline uint8_t func__ror8(uint8_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
inline uint16_t func__rol16(uint16_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
inline uint16_t func__ror16(uint16_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
inline uint32_t func__rol32(uint32_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
inline uint32_t func__ror32(uint32_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
inline uint64_t func__rol64(uint64_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value << count) | (value >> (-count & mask));
}
inline uint64_t func__ror64(uint64_t value, unsigned int count) {
const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
count &= mask;
return (value >> count) | (value << (-count & mask));
}
// a740g: ROR & ROL additions end
// bit-shifting
inline uint64 func__shl(uint64 a1, int b1) { return a1 << b1; }
inline uint64 func__shr(uint64 a1, int b1) { return a1 >> b1; }
inline int64 func__readbit(uint64 a1, int b1) {
if (a1 & 1ull << b1)
return -1;
else
return 0;
}
inline uint64 func__setbit(uint64 a1, int b1) { return a1 | 1ull << b1; }
inline uint64 func__resetbit(uint64 a1, int b1) { return a1 & ~(1ull << b1); }
inline uint64 func__togglebit(uint64 a1, int b1) { return a1 ^ 1ull << b1; }
// Working with 32bit colors:
inline uint32 func__rgb32(int32 r, int32 g, int32 b, int32 a) {
if (r < 0)