在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:keon/awesome-bits开源软件地址:https://github.com/keon/awesome-bits开源编程语言:开源软件介绍:awesome-bits
IntegersSet nth bit
Unset nth bit
Toggle nth bit
Round up to the next power of two
Round down / floor a number
Get the maximum integer
Get the minimum integer
Get the maximum long
Multiply by 2
Divide by 2
Multiply by the mth power of 2
Divide by the mth power of 2
Check Equality This is 35% faster in Javascript
Check if a number is odd
Exchange (swap) two values
Get the absolute value
Get the max of two values
Get the min of two values
Check whether both numbers have the same sign
Flip the sign
Calculate 2n
Whether a number is power of 2
Modulo 2n against m
Get the average
Get the mth bit of n (from low to high)
Set the mth bit of n to 0 (from low to high)
Check if nth bit is set
Isolate (extract) the right-most 1 bit
Isolate (extract) the right-most 0 bit
Set the right-most 0 bit to 1
Set the right-most 1 bit to 0
n + 1
n - 1
Get the negative value of a number
Swap Adjacent bits
Different rightmost bit of numbers m & n
Common rightmost bit of numbers m & n
FloatsThese are techniques inspired by the fast inverse square root method. Most of these are original. Turn a float into a bit-array (unsigned uint32_t) #include <stdint.h>
typedef union {float flt; uint32_t bits} lens_t;
uint32_t f2i(float x) {
return ((lens_t) {.flt = x}).bits;
} Caveat: Type pruning via unions is undefined in C++; use Turn a bit-array back into a float float i2f(uint32_t x) {
return ((lens_t) {.bits = x}).flt;
} Approximate the bit-array of a positive float using
man, exp = frexp(x);
return (uint32_t)((2 * man + exp + 125) * 0x800000); Caveat: This will have at most 2-16 relative error, since man + 125 clobbers the last 8 bits, saving the first 16 bits of your mantissa. Fast Inverse Square Root return i2f(0x5f3759df - f2i(x) / 2); Caveat: We're using the See this Wikipedia article for reference. Fast nth Root of positive numbers via Infinite Series float root(float x, int n) {
#DEFINE MAN_MASK 0x7fffff
#DEFINE EXP_MASK 0x7f800000
#DEFINE EXP_BIAS 0x3f800000
uint32_t bits = f2i(x);
uint32_t man = bits & MAN_MASK;
uint32_t exp = (bits & EXP_MASK) - EXP_BIAS;
return i2f((man + man / n) | ((EXP_BIAS + exp / n) & EXP_MASK));
} See this blog post regarding the derivation. Fast Arbitrary Power return i2f((1 - exp) * (0x3f800000 - 0x5c416) + f2i(x) * exp) Caveat: The See these set of slides for a derivation of this method. Fast Geometric Mean The geometric mean of a set of #include <stddef.h>
float geometric_mean(float* list, size_t length) {
// Effectively, find the average of map(f2i, list)
uint32_t accumulator = 0;
for (size_t i = 0; i < length; i++) {
accumulator += f2i(list[i]);
}
return i2f(accumulator / n);
} See here for its derivation. Fast Natural Logarithm #DEFINE EPSILON 1.1920928955078125e-07
#DEFINE LOG2 0.6931471805599453
return (f2i(x) - (0x3f800000 - 0x66774)) * EPSILON * LOG2 Caveat: The bias term of See here for its derivation. Fast Natural Exp return i2f(0x3f800000 + (uint32_t)(x * (0x800000 + 0x38aa22))) Caveat: The bias term of See here for its derivation. StringsConvert letter to lowercase:
Convert letter to uppercase:
Invert letter's case:
Letter's position in alphabet:
Get letter's position in alphabet (for Uppercase letters only):
Get letter's position in alphabet (for lowercase letters only):
MiscellaneousFast color conversion from R5G5B5 to R8G8B8 pixel format using shifts
Note: using anything other than the English letters will produce garbage results Additional Resources
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论