Skip to content

Commit

Permalink
fast_xs: modularize some of the headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Wong committed Feb 20, 2008
1 parent 5b8b45a commit 562e184
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
37 changes: 2 additions & 35 deletions ext/fast_xs/fast_xs.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,14 @@
#define VERSION "0.1"

#include <ruby.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "ruby_1_9_compat.h"

/* I don't trust ctype.h when it comes to locale-independence: */
static __inline__ int is_hex(const int x)
{
return (((x) >= '0' && (x) <= '9') ||
((x) >= 'a' && (x) <= 'f') ||
((x) >= 'A' && (x) <= 'F'));
}

static __inline__ int xtoupper(const int x)
{
return (x >= 'a' && x <= 'f') ? (x & ~0x20) : x;
}

static __inline__ int hexchar_to_int(const int x)
{
return (x < 'A') ? (x - '0') : (xtoupper(x) - 'A' + 10);
}

static __inline__ int hexpair_to_int(const int x1, const int x2)
{
return ((hexchar_to_int(x1) << 4) | hexchar_to_int(x2));
}
#include "fast_xs_type.h"
#include "gcc.h"

static ID unpack_id;
static VALUE U_fmt, C_fmt;

/* give GCC hints for better branch prediction
* (we layout branches so that ASCII characters are handled faster) */
#if defined(__GNUC__) && (__GNUC__ >= 3)
# define likely(x) __builtin_expect (!!(x), 1)
# define unlikely(x) __builtin_expect (!!(x), 0)
#else
# define unlikely(x) (x)
# define likely(x) (x)
#endif

/* pass-through certain characters for CP-1252 */
#define p(x) (x-128)

Expand Down
28 changes: 28 additions & 0 deletions ext/fast_xs/fast_xs_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef FAST_XS_TYPE_H
#define FAST_XS_TYPE_H

/* I don't trust ctype.h when it comes to locale-independence: */

static __inline__ int is_hex(const int x)
{
return (((x) >= '0' && (x) <= '9') ||
((x) >= 'a' && (x) <= 'f') ||
((x) >= 'A' && (x) <= 'F'));
}

static __inline__ int xtoupper(const int x)
{
return (x >= 'a' && x <= 'f') ? (x & ~0x20) : x;
}

static __inline__ int hexchar_to_int(const int x)
{
return (x < 'A') ? (x - '0') : (xtoupper(x) - 'A' + 10);
}

static __inline__ int hexpair_to_int(const int x1, const int x2)
{
return ((hexchar_to_int(x1) << 4) | hexchar_to_int(x2));
}

#endif /* FAST_XS_TYPE_H */
14 changes: 14 additions & 0 deletions ext/fast_xs/gcc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef GCC_H
#define GCC_H

/* give GCC hints for better branch prediction
* (we layout branches so that ASCII characters are handled faster) */
#if defined(__GNUC__) && (__GNUC__ >= 3)
# define likely(x) __builtin_expect (!!(x), 1)
# define unlikely(x) __builtin_expect (!!(x), 0)
#else
# define unlikely(x) (x)
# define likely(x) (x)
#endif

#endif /* GCC_H */

0 comments on commit 562e184

Please sign in to comment.