includes/std: Added first set of documentation to the std library

This commit is contained in:
WerWolv
2023-03-19 16:17:04 +01:00
parent c6f2b57384
commit 8601a6665e
9 changed files with 307 additions and 4 deletions

View File

@@ -2,8 +2,17 @@
#include <std/limits.pat>
/*!
This library contains various helper functions for common bit operations.
*/
namespace std::bit {
/**
Calculates the number of 1 bits in a given number
@param x The number
@return The number of bits set to 1 in `x`
*/
fn popcount(u128 x) {
x = (x & (std::limits::u128_max() / 3)) + ((x >> 1) & (std::limits::u128_max() / 3));
x = (x & (std::limits::u128_max() / 5)) + ((x >> 2) & (std::limits::u128_max() / 5));
@@ -12,10 +21,20 @@ namespace std::bit {
return x % 0xFF;
};
/**
Checks if only a single bit is set to 1 in a given number
@param x The number
@return True if there's a single bit set to 1 in `x`, false otherwise
*/
fn has_single_bit(u128 x) {
return x != 0 && (x & (x - 1)) == 0;
};
/**
Rounds the given number up to the next bigger power of two
@param x The number
@return Next bigger power of two that can fit `x`
*/
fn bit_ceil(u128 x) {
if (x == 0) return 0;
@@ -24,8 +43,13 @@ namespace std::bit {
i = i + 1;
return 1 << i;
};
};¨
/**
Rounds the given number down to the next bigger power of two
@param x The number
@return Next smaller power of two
*/
fn bit_floor(u128 x) {
if (x == 0) return 0;