PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
utility.h
Go to the documentation of this file.
1 
26 #pragma once
27 
28 #include <PropWare/PropWare.h>
29 
30 namespace PropWare {
31 
35 class Utility {
36  public:
44  static uint8_t count_bits (uint32_t par) {
45  // Brian Kernighan's method for counting set bits in a variable
46  uint8_t totalBits = 0;
47 
48  while (par) {
49  par &= par - 1; // clear the least significant bit set
50  ++totalBits;
51  }
52 
53  return totalBits;
54  }
55 
63  static uint8_t count_bits (int32_t par) {
64  return count_bits((uint32_t) par);
65  }
66 
102  static inline uint32_t measure_time_interval (const register uint32_t start) {
103  return (CNT - start) / MICROSECOND;
104  }
105 
109  static inline uint32_t measure_time_interval (const register int32_t start) {
110  return measure_time_interval((uint32_t) start);
111  }
112 
124  static size_t get_largest_free_block_size (const uint8_t precision = 32) {
125  size_t largestSuccess = 0;
126  size_t smallestFailure = 32 * 1024;
127  size_t nextAttempt = 32 * 1024;
128 
129  uint8_t *ptr = NULL;
130 
131  do {
132  ptr = (uint8_t *) malloc(nextAttempt);
133 
134  // If the allocation succeeded, free the memory as quickly as
135  // possible
136  if (NULL != ptr) {
137  free(ptr);
138  largestSuccess = nextAttempt;
139  } else
140  // If the allocation fails, try the next smallest
141  smallestFailure = nextAttempt;
142 
143 
144  nextAttempt = (smallestFailure - largestSuccess) / 2 +
145  largestSuccess;
146  } while (precision < (smallestFailure - largestSuccess));
147 
148  return largestSuccess;
149  }
150 
156  static void to_lower (char string[]) {
157  for (size_t i = 0; i < strlen(string); ++i)
158  string[i] = tolower(string[i]);
159  }
160 
166  static void to_upper (char string[]) {
167  for (size_t i = 0; i < strlen(string); ++i)
168  string[i] = toupper(string[i]);
169  }
170 
176  static const char *to_string (const bool b) {
177  return b ? "true" : "false";
178  }
179 
190  static int rom_log (int x) {
191  int exp;
192  unsigned short *ptr;
193 
194  if (!x)
195  return 0;
196 
197  for (exp = 31; x > 0; exp--)
198  x <<= 1;
199  ptr = (unsigned short *) ((((unsigned int) x) >> 19) + 0xb000);
200  return (exp << 16) | *ptr;
201  }
202 
213  inline static unsigned int reverse (unsigned int x, unsigned int bits = 0) {
214  return __builtin_propeller_rev(x, bits);
215  }
216 
217  static bool empty (const char string[]) {
218  return '\0' == string[0];
219  }
220 
231  template<typename T, size_t N>
232  static inline size_t size_of_array (const T(&array)[N]) {
233  return N;
234  }
235 
242  static inline void reboot () {
243  __builtin_propeller_clkset(0x80);
244  }
245 
246  static Bit to_bit (const uint_fast8_t bitNumber) {
247  if (32 > bitNumber)
248  return (Bit) (1 << bitNumber);
249  else
250  return NULL_BIT;
251  }
252 
253  static bool bit_read (const uint32_t x, const Bit bit) {
254  return 0 != (x & bit);
255  }
256 
257  template<typename T>
258  static void bit_write (T &x, const Bit bit, const bool value) {
259  if (value)
260  bit_set(x, bit);
261  else
262  bit_clear(x, bit);
263  }
264 
265  template<typename T>
266  static void bit_set (T &x, const Bit bit) {
267  x |= bit;
268  }
269 
270  template<typename T>
271  static void bit_clear (T &x, const Bit bit) {
272  x &= ~bit;
273  }
274 
275  private:
280  Utility () {
281  }
282 };
283 
284 }
PropWare::Utility::reverse
static unsigned int reverse(unsigned int x, unsigned int bits=0)
Reverse some of the bits in x
Definition: utility.h:213
PropWare::Utility::get_largest_free_block_size
static size_t get_largest_free_block_size(const uint8_t precision=32)
Determine the size of the largest block of free memory.
Definition: utility.h:124
PropWare::Utility::to_upper
static void to_upper(char string[])
Convert each alphabetical character in a null-terminated character array to uppercase letters.
Definition: utility.h:166
PropWare::Utility::measure_time_interval
static uint32_t measure_time_interval(const register uint32_t start)
Determine the number of microseconds passed since a starting point.
Definition: utility.h:102
PropWare::Utility
Definition: utility.h:35
PropWare::Utility::count_bits
static uint8_t count_bits(int32_t par)
Count the number of set bits in a parameter.
Definition: utility.h:63
PropWare::Utility::measure_time_interval
static uint32_t measure_time_interval(const register int32_t start)
Definition: utility.h:109
PropWare.h
PropWare::Utility::rom_log
static int rom_log(int x)
Compute the mathematical expression log2(x). Result is in fixed-point format (16 digits to the left a...
Definition: utility.h:190
PropWare::Utility::to_lower
static void to_lower(char string[])
Convert each alphabetical character in a null-terminated character array to lowercase letters.
Definition: utility.h:156
x
int x
Definition: 07 Box and Lines.c:13
PropWare::Utility::to_string
static const char * to_string(const bool b)
Convert a boolean to the string-literal either "true" or "false"
Definition: utility.h:176
PropWare::Utility::count_bits
static uint8_t count_bits(uint32_t par)
Count the number of set bits in a parameter.
Definition: utility.h:44
tolower
int tolower(int c)
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
toupper
int toupper(int c)
PropWare::Utility::reboot
static void reboot()
Perform hard reboot.
Definition: utility.h:242
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Utility::size_of_array
static size_t size_of_array(const T(&array)[N])
Determine the size of an array.
Definition: utility.h:232