summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Linehan <patientulysses@gmail.com>2013-12-23 13:33:10 -0500
committerJason Linehan <patientulysses@gmail.com>2013-12-23 13:33:10 -0500
commit4cc5d78993732fe645e1f79242e5c4526dff5953 (patch)
tree841c2916c430d6689c87d3e55022a626aabd5d6b
parent1a7e96f58ff1fb5c6e22a13bd22c1e18e6a0cfd2 (diff)
parentbdba30700dfb25e08124c3bf7a7cb3cdc8c90987 (diff)
downloadlibjdl-master.tar.gz
libjdl-master.tar.bz2
libjdl-master.zip
Merge branch 'master' of linehan.me:jlibmaster
-rw-r--r--src/abst/matrix/matrix.c3
-rw-r--r--src/abst/matrix/matrix.h11
-rw-r--r--src/math/geometry.c55
-rw-r--r--src/text/textutils.c20
-rw-r--r--src/text/wchar.c2
-rw-r--r--src/util/tty.c102
-rw-r--r--src/util/tty.h56
7 files changed, 177 insertions, 72 deletions
diff --git a/src/abst/matrix/matrix.c b/src/abst/matrix/matrix.c
index fcaf79a..d5a4544 100644
--- a/src/abst/matrix/matrix.c
+++ b/src/abst/matrix/matrix.c
@@ -23,9 +23,6 @@
int mx_create(struct mx_t *mx, char type, int h, int w)
{
- int row_offset;
- int i;
-
if (mx == NULL) {
if (!(mx = malloc(sizeof(struct mx_t)))) {
return -1;
diff --git a/src/abst/matrix/matrix.h b/src/abst/matrix/matrix.h
index a281cd0..f678db9 100644
--- a/src/abst/matrix/matrix.h
+++ b/src/abst/matrix/matrix.h
@@ -5,6 +5,15 @@
#include <stdint.h>
#include <stdbool.h>
+#ifdef UNUSED
+#elif defined(__GNUC__)
+#define UNUSED(x) UNUSED_ ## x __attribute__((unused))
+#elif defined(__LCLINT__)
+#define UNUSED(x) /*@unused@*/ x
+#else
+#define UNUSED(x) x
+#endif
+
/******************************************************************************
* DATA STRUCTURES
******************************************************************************/
@@ -32,7 +41,7 @@ struct mx_t {
-static union mx_cell DOPE;
+static union mx_cell UNUSED(DOPE);
/******************************************************************************
diff --git a/src/math/geometry.c b/src/math/geometry.c
index c8fdac6..9eeecf0 100644
--- a/src/math/geometry.c
+++ b/src/math/geometry.c
@@ -5,10 +5,12 @@
/**
* UNSECO World Heritage Site
*
- * The fast inverse square root function from the
+ * The fast inverse square root function from
* id Software's Quake III, preserved here without
* stylistic alteration.
*/
+
+/*
float Q_rsqrt( float number )
{
long i;
@@ -25,6 +27,53 @@ float Q_rsqrt( float number )
return y;
}
+*/
+
+
+/**
+ * inverse_sqrt() - Compute the inverse square root of a double-precision
+ * ``````````````
+ * Here is an example of a Newton-Raphson approximation for the
+ * inverse square of a double-precision floating-point value.
+ *
+ * Unfortunately, with RECIP_ITER=1, benchmarks on my computer put it
+ * slightly slower (~5%) than the simple version 1.0/(x*x). It's faster
+ * (2x as fast) with zero iterations, but then you only get 12 bits
+ * of precision. I don't know if 12 bits is enough for you.
+ *
+ * I think one of the problems here is that this is too small of a micro
+ * optimization; at this scale the compiler writers are on nearly equal
+ * footing with the assembly hackers. Maybe if we had the bigger picture
+ * we could see a way to make it faster.
+ *
+ * For example, you said that -ffast-math caused an undesirable loss of
+ * precision; this may indicate a numerical stability problem in the
+ * algorithm you are using. With the right choice of algorithm, many
+ * problems can be solved with float instead of double. (Of course,
+ * you may just need more than 24 bits. I don't know).
+ *
+ * I suspect the RCPSS method shines if you want to compute several
+ * of these in parallel.
+ */
+static double inverse_sqrt(double x)
+{
+ #define RECIP_ITER 1
+ double y;
+ int i;
+ __asm__ (
+ "cvtpd2ps %1, %0\n\t"
+ "rcpss %0, %0\n\t"
+ "cvtps2pd %0, %0"
+ : "=x"(y)
+ : "x"(x)
+ );
+
+ for (i=0; i<RECIP_ITER; ++i) {
+ y *= 2-x*y;
+ }
+ return y * y;
+}
+
@@ -72,7 +121,7 @@ float fast_normalize(float *a, float *b, float *c)
float norm;
norm = (float)(powf(*a, 2.0f) + powf(*b, 2.0f) + powf(*c, 2.0f));
- norm = Q_rsqrt(norm); /* Hacking */
+ norm = inverse_sqrt(norm); /* Hacking */
*a *= norm;
*b *= norm;
@@ -101,7 +150,7 @@ float fast_norm(float a, float b, float c)
float norm;
norm = (float)(powf(a, 2.0f) + powf(b, 2.0f) + powf(c, 2.0f));
- norm = Q_rsqrt(norm); /* Hacking */
+ norm = inverse_sqrt(norm); /* Hacking */
return norm;
}
diff --git a/src/text/textutils.c b/src/text/textutils.c
index 0ef2fe7..34b498a 100644
--- a/src/text/textutils.c
+++ b/src/text/textutils.c
@@ -359,6 +359,7 @@ int ntok(const char *str, const char *tok)
/*}*/
/*return count;*/
+ return 1;
}
@@ -1300,19 +1301,6 @@ char *sljoinf(char *dst, size_t chunk_size, const char *fmt, ...)
-bool strcomb(char *haystack, char *needle)
-{
- char *tok;
-
- /*tok = memmem(haystack, needle);*/
-
- if (tok != NULL) {
- *tok = 0;
- }
-
- return (tok) ? true : false;
-}
-
char *from_file(char *path)
{
@@ -1370,11 +1358,11 @@ done:
size_t concat(char *dst, size_t max, const char *str, ...)
{
va_list args;
- size_t total;
- size_t bytes;
+ size_t total=0;
+ size_t bytes=0;
size_t len;
char *d;
- char *s;
+ const char *s;
d = dst;
diff --git a/src/text/wchar.c b/src/text/wchar.c
index d5fd612..a843af6 100644
--- a/src/text/wchar.c
+++ b/src/text/wchar.c
@@ -52,7 +52,7 @@ wchar_t *wcs_dup(const wchar_t *wcs)
void wcs_dimensions(const wchar_t *wcs, int *w, int *h)
{
const wchar_t *wc;
- int wtmp;
+ int wtmp=0;
for (wc=wcs; *wc!=L'\0'; wc++) {
if (*wc == L'\n') {
diff --git a/src/util/tty.c b/src/util/tty.c
index 831fbee..640d9a0 100644
--- a/src/util/tty.c
+++ b/src/util/tty.c
@@ -7,11 +7,20 @@
#include <wchar.h>
#include <ncurses.h>
#include <panel.h>
+#include <string.h>
#include "debug.h"
#include "time.h"
#include "tty.h"
+/**
+ * nc_trap()
+ * `````````
+ * Signal handler to ensure the terminal mode is restored.
+ *
+ * @signo: Signal number
+ * Return: Nothing (never return from a signal handler)
+ */
static
void nc_trap(int signo)
{
@@ -20,6 +29,12 @@ void nc_trap(int signo)
raise(signo);
}
+
+/**
+ * nc_setup_trap()
+ * ```````````````
+ * Configure the signal handler.
+ */
static
void nc_setup_trap(void)
{
@@ -34,10 +49,9 @@ void nc_setup_trap(void)
}
-
/**
- * nc_set
- * ``````
+ * nc_set()
+ * ````````
* Provide a uniform interface for ncurses options.
*
* @value : Whether option should be enabled or disabled.
@@ -47,7 +61,7 @@ void nc_setup_trap(void)
int nc_set(bool value, int opt)
{
/* Line-buffered vs character-buffered input */
- if (opt & BUFFER_INPUT) {
+ if (opt & NC_INPUT_BUFFER) {
if (value == true) {
nocbreak();
} else {
@@ -56,7 +70,7 @@ int nc_set(bool value, int opt)
}
/* Input is or isn't echoed to the terminal. */
- if (opt & ECHO_INPUT) {
+ if (opt & NC_INPUT_ECHO) {
if (value == true) {
echo();
} else {
@@ -65,35 +79,35 @@ int nc_set(bool value, int opt)
}
/* Calls to getch() are or aren't blocking. */
- if (opt & BLOCKING_INPUT) {
+ if (opt & NC_INPUT_NONBLOCK) {
nodelay(stdscr, value);
}
- /* Show or hide the terminal cursor. */
- if (opt & SHOW_CURSOR) {
- curs_set(value);
- }
-
/* Enable special keys */
- if (opt & DETECT_KEYPAD) {
+ if (opt & NC_INPUT_KEYPAD) {
keypad(stdscr, value);
}
+ /* Show or hide the terminal cursor. */
+ if (opt & NC_CURSOR) {
+ curs_set(value);
+ }
+
/* Keep access to default terminal colors */
- if (opt & USE_DEFAULT_COLORS) {
+ if (opt & NC_COLOR_DEFAULTS) {
if (value == true) {
use_default_colors();
}
}
/* Use colors when drawing on the tty */
- if (opt & USE_COLOR) {
+ if (opt & NC_COLOR) {
if (!has_colors()) {
nc_stop();
ERROR("Terminal does not support color\n");
return -1;
}
- if (opt & COLOR_PROGRAMMABLE) {
+ if (opt & NC_COLOR_PROGRAMMABLE) {
if (!can_change_color()) {
nc_stop();
ERROR("Terminal does not support programmable colors\n");
@@ -107,7 +121,7 @@ int nc_set(bool value, int opt)
*/
start_color();
- if (opt & COLOR_256) {
+ if (opt & NC_COLOR_256) {
if (COLORS < 255) {
nc_stop();
ERROR("Terminal does not support 256 colors\n");
@@ -120,8 +134,14 @@ int nc_set(bool value, int opt)
}
-
-int nc_start(void)
+/**
+ * nc_start()
+ * ``````````
+ * Initialize the ncurses terminal mode.
+ * @option: "auto" for defaults, NULL for no initial settings
+ * Return : 1
+ */
+int nc_start(const char *option)
{
setlocale(LC_ALL,""); /* Must be set for UTF-8 support */
initscr(); /* Start ncurses */
@@ -138,17 +158,26 @@ int nc_start(void)
ESCDELAY = 25;
}
- nc_set(true, DETECT_KEYPAD);
- nc_set(false, ECHO_INPUT);
- nc_set(false, BLOCKING_INPUT);
- nc_set(false, BUFFER_INPUT);
- nc_set(false, SHOW_CURSOR);
- nc_set(true, USE_COLOR);
- nc_set(true, USE_DEFAULT_COLORS);
+ if (option != NULL && (strncmp(option, "auto", 5) == 0)) {
+ /* DEFAULTS */
+ nc_set(true, NC_INPUT_KEYPAD);
+ nc_set(false, NC_INPUT_ECHO);
+ nc_set(true, NC_INPUT_NONBLOCK);
+ nc_set(false, NC_INPUT_BUFFER);
+ nc_set(false, NC_CURSOR);
+ nc_set(true, NC_COLOR);
+ nc_set(true, NC_COLOR_DEFAULTS);
+ }
return 1;
}
+/**
+ * nc_stop()
+ * `````````
+ * Restore the terminal mode.
+ * Return: 1
+ */
int nc_stop(void)
{
resetty();
@@ -156,17 +185,32 @@ int nc_stop(void)
return 1;
}
-int nc_waitkey(void)
+/**
+ * nc_waitkey()
+ * ````````````
+ * Detect a keypress. Use in a while() loop.
+ * @ns : nanoseconds to sleep before checking again
+ * Return: keycode
+ */
+int nc_waitkey(long ns)
{
int c;
- while (c=getch(), c==ERR) {
- sleep_ns(1000);
- };
+ while ((c=getch()) == ERR) {
+ sleep_ns(ns);
+ }
return c;
}
+
+int nc_colorpair(char pairno, char fg, char bg)
+{
+ init_pair(pairno, fg, bg);
+ return pairno;
+}
+
+
/*WINDOW *win_adjust(WINDOW *win, int lines, int cols, int start_y, int start_x)*/
/*{*/
/*mvwin(win, start_y, start_x);*/
diff --git a/src/util/tty.h b/src/util/tty.h
index 0652387..6900a29 100644
--- a/src/util/tty.h
+++ b/src/util/tty.h
@@ -14,26 +14,44 @@
#error "Missing ncurses headers; you may need to install ncurses-devel"
#endif
+/*
+ * nc_start(): If you want ncurses to be
+ * loaded with "reasonable" defaults
+ * automatically.
+ */
+#define NC_REASONABLE_DEFAULTS "auto"
+/*
+ * nc_start(): If you want ncurses to be
+ * loaded raw, so that it can be configured
+ * explicitly.
+ */
+#define NC_CONFIGURE NULL
+
+
+#define NC_INPUT_BUFFER (1 << 0)
+#define NC_INPUT_ECHO (1 << 1)
+#define NC_INPUT_NONBLOCK (1 << 2)
+#define NC_INPUT_KEYPAD (1 << 3)
+#define NC_CURSOR (1 << 4)
+#define NC_COLOR (1 << 5)
+#define NC_COLOR_DEFAULTS (1 << 6)
+#define NC_COLOR_PROGRAMMABLE (1 << 7)
+#define NC_COLOR_256 (1 << 8)
+
+
+int nc_start (const char *option);
+int nc_stop (void);
+int nc_set (bool value, int option);
+int nc_waitkey(long ns);
+
+int nc_colorpair(char pairno, char fg, char bg);
+
+#define NC_CENTER(y, x, str) \
+ ((LINES/2)+(y)), (((COLS/2)-((strlen((str))-1)/2))+(x)), str
-#define BUFFER_INPUT (1 << 0)
-#define ECHO_INPUT (1 << 1)
-#define BLOCKING_INPUT (1 << 2)
-#define SHOW_CURSOR (1 << 3)
-#define DETECT_KEYPAD (1 << 4)
-#define USE_COLOR (1 << 5)
-#define USE_DEFAULT_COLORS (1 << 6)
-#define COLOR_PROGRAMMABLE (1 << 7)
-#define COLOR_256 (1 << 8)
-
-
-int nc_set(bool value, int option);
-int nc_start(void);
-int nc_stop(void);
-int nc_waitkey(void);
-cchar_t *cch(wchar_t *wch, attr_t attr, short co);
-
-void win_background(WINDOW *win, cchar_t *cch);
-void win_background_set(WINDOW *win, cchar_t *cch);
+//cchar_t *cch(wchar_t *wch, attr_t attr, short co);
+//void win_background(WINDOW *win, cchar_t *cch);
+//void win_background_set(WINDOW *win, cchar_t *cch);
#ifndef pan_refresh
#define pan_refresh() \