* mcgarvey@eng.utah.edu
*/
-#ifndef __COMMON_H__
-#define __COMMON_H__
+#ifndef _COMMON_H_
+#define _COMMON_H_
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+/*
+ * Define a keyword for use while defining small and fast functions.
+ */
+#if EXTRA_INLINE
+#define INLINE_MAYBE static inline
+#else
+#define INLINE_MAYBE static
+#endif
+
+
/*
* Define a type for scalar values, either float or double.
*/
-#ifdef USE_DOUBLE
+#if DOUBLE_FLOAT
typedef double scal_t;
+#define SCALAR_SIZE 8
#define S(K) K
-#define scal_sin sin
-#define scal_cos cos
+#define scal_floor floor
+#define scal_ceil ceil
+#define scal_min fmin
+#define scal_max fmax
+#define scal_pow pow
+#define scal_sqrt sqrt
+#define scal_sin sin
+#define scal_cos cos
+#define scal_tan tan
+#define scal_asin asin
+#define scal_acos acos
+#define scal_atan atan
#else
typedef float scal_t;
+#define SCALAR_SIZE 4
#define S(K) K##f
-#define scal_sin sinf
-#define scal_cos cosf
+#define scal_floor floorf
+#define scal_ceil ceilf
+#define scal_min fminf
+#define scal_max fmaxf
+#define scal_pow powf
+#define scal_sqrt sqrtf
+#define scal_sin sinf
+#define scal_cos cosf
+#define scal_tan tanf
+#define scal_asin asinf
+#define scal_acos acosf
+#define scal_atan atanf
#endif
-#define S_ZERO S(0.0)
+#define scal_min2(A,B,C) scal_min(scal_min(A,B),C)
+#define scal_max2(A,B,C) scal_max(scal_max(A,B),C)
+
+/*
+ * Clamp a scalar between two values.
+ */
+INLINE_MAYBE
+scal_t scal_clamp(scal_t s, scal_t min, scal_t max)
+{
+ if (s < min) {
+ return min;
+ }
+ if (max < s) {
+ return max;
+ }
+ return s;
+}
/*
- * Define a keyword for use while defining small and fast functions.
+ * Define min and max functions for integers.
*/
-#define __fast__ static inline
+INLINE_MAYBE
+int imin(int a, int b)
+{
+ return a < b ? a : b;
+}
+INLINE_MAYBE
+int imax(int a, int b)
+{
+ return a < b ? b : a;
+}
/*
#define UNPACK(W,N) ((uint8_t)((W) >> (8 * (N))) & 0xff)
+/*
+ * Try to execute a statement and print a status message. If the statement
+ * resolves to 0, it is considered to have succeeded; any other evaluation is
+ * an error condition and an abort(3) occurs.
+ * return.
+ */
+#define TRY_DO(L, K, ARGS...) \
+printf("* " L "... ", ##ARGS); \
+fflush(stdout); \
+if ((K) == 0) printf("done!\n"); \
+else abort()
+
+
+/*
+ * Declare a type of destructor functions.
+ */
typedef void (*dtor_t)(void*);
#define DTOR(A) (dtor_t)(A)
int mem_blocks();
+/*
+ * Duplicate a string; like the non-standard strdup(3) but uses mem_alloc.
+ * The result needs to be freed with mem_free.
+ */
+INLINE_MAYBE
+char* mem_strdup(const char* str)
+{
+ size_t size = strlen(str) + 1;
+ return (char*)memcpy(mem_alloc(size), str, size);
+}
+
+/*
+ * Get an allocated string made up of two strings concatenated together.
+ * The result needs to be freed with mem_free.
+ */
+INLINE_MAYBE
+char* mem_strcat(const char* str1, const char* str2)
+{
+ size_t size1 = strlen(str1);
+ size_t size2 = strlen(str2);
+ char* str = (char*)mem_alloc(size1 + size2 + 1);
+ memcpy(str + size1, str2, size2 + 1);
+ return memcpy(str, str1, size1);
+}
+
+
+/*
+ * Cut a string short at the last matching character. The string will be
+ * modified if the character was matched. Either way, str is returned.
+ */
+INLINE_MAYBE
+char* strcut(char* str, int c)
+{
+ char* ptr = strrchr(str, c);
+ if (ptr != NULL) {
+ *ptr = '\0';
+ }
+ return str;
+}
+
/*
* Trim white space off of the right side of a string.
*/
/*
* Trim white space off of both sides of a string.
*/
-__fast__
+INLINE_MAYBE
void trim(char *str)
{
rtrim(str);
}
-#endif // __COMMON_H__
+/*
+ * Initialize the timer.
+ */
+void timer_start();
+
+/*
+ * Stop the timer and return how many microseconds passed after
+ * initialization.
+ */
+long timer_stop();
+
+
+#endif // _COMMON_H_