You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2009/09/10 21:32:40 UTC

svn commit: r813557 - in /lucene/lucy/trunk: core/Lucy/Test/Util/TestNumberUtils.bp core/Lucy/Test/Util/TestNumberUtils.c core/Lucy/Util/NumberUtils.bp core/Lucy/Util/NumberUtils.c perl/lib/Lucy/Test.pm perl/t/core/030-number_utils.t

Author: marvin
Date: Thu Sep 10 19:32:40 2009
New Revision: 813557

URL: http://svn.apache.org/viewvc?rev=813557&view=rev
Log:
Commit LUCY-39, adding the Lucy::Util::NumberUtils module.

Added:
    lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.bp   (with props)
    lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.c   (with props)
    lucene/lucy/trunk/core/Lucy/Util/NumberUtils.bp   (with props)
    lucene/lucy/trunk/core/Lucy/Util/NumberUtils.c   (with props)
    lucene/lucy/trunk/perl/t/core/030-number_utils.t   (with props)
Modified:
    lucene/lucy/trunk/perl/lib/Lucy/Test.pm

Added: lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.bp?rev=813557&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.bp Thu Sep 10 19:32:40 2009
@@ -0,0 +1,22 @@
+parcel Lucy;
+
+inert class Lucy::Test::Util::TestNumberUtils cnick TestNumUtil {
+    inert void
+    run_tests();
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.bp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.c?rev=813557&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.c (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.c Thu Sep 10 19:32:40 2009
@@ -0,0 +1,411 @@
+#define C_LUCY_TESTNUMBERUTILS
+#include "Lucy/Util/ToolSet.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+
+#include "Lucy/Test.h"
+#include "Lucy/Test/Util/TestNumberUtils.h"
+#include "Lucy/Util/NumberUtils.h"
+
+static u64_t*
+S_random_ints(u64_t *buf, size_t count, u64_t min, u64_t limit) 
+{
+    u64_t  range = min < limit ? limit - min : 0;
+    u64_t *ints = buf ? buf : (u64_t*)CALLOCATE(count, sizeof(u64_t));
+    size_t i;
+    for (i = 0; i < count; i++) {
+        u64_t num =    ((u64_t)rand() << 60)
+                     | ((u64_t)rand() << 45)
+                     | ((u64_t)rand() << 30)
+                     | ((u64_t)rand() << 15) 
+                     | ((u64_t)rand() << 0);
+        ints[i] = min + num % range;
+    }
+    return ints;
+}
+
+static void
+test_u1(TestBatch *batch)
+{
+    size_t count   = 64;
+    u64_t *ints    = S_random_ints(NULL, count, 0, 2);
+    size_t amount  = count / 8;
+    u8_t  *bits    = (u8_t*)CALLOCATE(amount, sizeof(u8_t));
+    size_t i;
+
+    for (i = 0; i < count; i++) {
+        if (ints[i]) { NumUtil_u1set(bits, i); }
+    }
+    for (i = 0; i < count; i++) {
+        ASSERT_INT_EQ(batch, NumUtil_u1get(bits, i), ints[i], "u1 set/get");
+    }
+
+    for (i = 0; i < count; i++) {
+        NumUtil_u1flip(bits, i);
+    }
+    for (i = 0; i < count; i++) {
+        ASSERT_INT_EQ(batch, NumUtil_u1get(bits, i), !ints[i], "u1 flip");
+    }
+
+    FREEMEM(bits);
+    FREEMEM(ints);
+}
+
+static void
+test_u2(TestBatch *batch)
+{
+    size_t count = 32;
+    u64_t *ints = S_random_ints(NULL, count, 0, 4);
+    u8_t  *bits = (u8_t*)CALLOCATE((count/4), sizeof(u8_t));
+    size_t i;
+
+    for (i = 0; i < count; i++) {
+        NumUtil_u2set(bits, i, ints[i]);
+    }
+    for (i = 0; i < count; i++) {
+        ASSERT_INT_EQ(batch, NumUtil_u2get(bits, i), ints[i], "u2");
+    }
+
+    FREEMEM(bits);
+    FREEMEM(ints);
+}
+
+static void
+test_u4(TestBatch *batch)
+{
+    size_t count = 128;
+    u64_t *ints = S_random_ints(NULL, count, 0, 16);
+    u8_t  *bits = (u8_t*)CALLOCATE((count/2), sizeof(u8_t));
+    size_t i;
+
+    for (i = 0; i < count; i++) {
+        NumUtil_u4set(bits, i, ints[i]);
+    }
+    for (i = 0; i < count; i++) {
+        ASSERT_INT_EQ(batch, NumUtil_u4get(bits, i), ints[i], "u4");
+    }
+
+    FREEMEM(bits);
+    FREEMEM(ints);
+}
+
+static void
+test_c32(TestBatch *batch)
+{
+    u64_t mins[]   = { 0,   0x4000 - 100, (u32_t)I32_MAX - 100, U32_MAX - 10 };
+    u64_t limits[] = { 500, 0x4000 + 100, (u32_t)I32_MAX + 100, U32_MAX      };
+    u32_t   set_num;
+    u32_t   num_sets  = sizeof(mins) / sizeof(u64_t);
+    size_t  count     = 64;
+    u64_t  *ints      = NULL;
+    size_t  amount    = count * C32_MAX_BYTES;
+    char   *encoded   = (char*)CALLOCATE(amount, sizeof(char));
+    char   *target    = encoded;
+    char   *limit     = target + amount;
+    size_t i;
+
+    for (set_num = 0; set_num < num_sets; set_num++) {
+        char *skip;
+        ints = S_random_ints(ints, count, mins[set_num], limits[set_num]);
+        target = encoded;
+        for (i = 0; i < count; i++) {
+            NumUtil_encode_c32(ints[i], &target);
+        }
+        target = encoded;
+        skip   = encoded;
+        for (i = 0; i < count; i++) {
+            ASSERT_INT_EQ(batch, NumUtil_decode_c32(&target), (long)ints[i], 
+                "c32 %lu", (long)ints[i]);
+            NumUtil_skip_cint(&skip);
+            if (target > limit) { 
+                fprintf(stderr, "overrun\n");
+                exit(1);
+            }
+        }
+        ASSERT_TRUE(batch, skip == target, "skip %lu == %lu", 
+            (unsigned long)skip, (unsigned long)target);
+
+        target = encoded;
+        for (i = 0; i < count; i++) {
+            NumUtil_encode_padded_c32(ints[i], &target);
+        }
+        ASSERT_TRUE(batch, target == limit, 
+            "padded c32 uses 5 bytes (%lu == %lu)", (unsigned long)target, 
+            (unsigned long)limit);
+        target = encoded;
+        skip   = encoded;
+        for (i = 0; i < count; i++) {
+            ASSERT_INT_EQ(batch, NumUtil_decode_c32(&target), (long)ints[i], 
+                "padded c32 %lu", (long)ints[i]);
+            NumUtil_skip_cint(&skip);
+            if (target > limit) { 
+                fprintf(stderr, "overrun\n");
+                exit(1);
+            }
+        }
+        ASSERT_TRUE(batch, skip == target, "skip padded %lu == %lu", 
+            (unsigned long)skip, (unsigned long)target);
+    }
+
+    target = encoded;
+    NumUtil_encode_c32(U32_MAX, &target);
+    target = encoded;
+    ASSERT_INT_EQ(batch, NumUtil_decode_c32(&target), U32_MAX, "c32 U32_MAX");
+
+    FREEMEM(encoded);
+    FREEMEM(ints);
+}
+
+static void
+test_c64(TestBatch *batch)
+{
+    u64_t mins[]   = { 0,   0x4000 - 100, (u64_t)U32_MAX - 100,  U64_MAX - 10 };
+    u64_t limits[] = { 500, 0x4000 + 100, (u64_t)U32_MAX + 1000, U64_MAX      };
+    u32_t   set_num;
+    u32_t   num_sets  = sizeof(mins) / sizeof(u64_t);
+    size_t  count     = 64;
+    u64_t  *ints      = NULL;
+    size_t  amount    = count * C64_MAX_BYTES;
+    char   *encoded   = (char*)CALLOCATE(amount, sizeof(char));
+    char   *target    = encoded;
+    char   *limit     = target + amount;
+    size_t i;
+
+    for (set_num = 0; set_num < num_sets; set_num++) {
+        char *skip;
+        ints = S_random_ints(ints, count, mins[set_num], limits[set_num]);
+        target = encoded;
+        for (i = 0; i < count; i++) {
+            NumUtil_encode_c64(ints[i], &target);
+        }
+        target = encoded;
+        skip   = encoded;
+        for (i = 0; i < count; i++) {
+            u64_t got = NumUtil_decode_c64(&target);
+            ASSERT_TRUE(batch, got == ints[i], 
+                "c64 %" U64P " == %" U64P, got, ints[i]);
+            if (target > limit) { 
+                fprintf(stderr, "overrun\n");
+                exit(1);
+            }
+            NumUtil_skip_cint(&skip);
+        }
+        ASSERT_TRUE(batch, skip == target, "skip %lu == %lu", 
+            (unsigned long)skip, (unsigned long)target);
+    }
+
+    target = encoded;
+    NumUtil_encode_c64(U64_MAX, &target);
+    target = encoded;
+    { 
+        u64_t got = NumUtil_decode_c64(&target);
+        ASSERT_TRUE(batch, got == U64_MAX, "c64 U64_MAX");
+    }
+
+    FREEMEM(encoded);
+    FREEMEM(ints);
+}
+
+static void
+test_bigend_u16(TestBatch *batch)
+{
+    size_t count     = 32;
+    u64_t *ints      = S_random_ints(NULL, count, 0, U16_MAX + 1);
+    size_t amount    = (count + 1) * sizeof(u16_t);
+    char  *allocated = (char*)CALLOCATE(amount, sizeof(char));
+    char  *encoded   = allocated + 1; /* Intentionally misaligned. */
+    char  *target    = encoded;
+    size_t i;
+
+    for (i = 0; i < count; i++) {
+        NumUtil_encode_bigend_u16(ints[i], &target);
+        target += sizeof(u16_t);
+    }
+    target = encoded;
+    for (i = 0; i < count; i++) {
+        u16_t got = NumUtil_decode_bigend_u16(target);
+        ASSERT_INT_EQ(batch, got, ints[i], "bigend u16");
+        target += sizeof(u16_t);
+    }
+
+    target = encoded;
+    NumUtil_encode_bigend_u16(1, &target);
+    ASSERT_INT_EQ(batch, encoded[0], 0, "Truly big-endian u16");
+    ASSERT_INT_EQ(batch, encoded[1], 1, "Truly big-endian u16");
+
+    FREEMEM(allocated);
+    FREEMEM(ints);
+}
+
+static void
+test_bigend_u32(TestBatch *batch)
+{
+    size_t count     = 32;
+    u64_t *ints      = S_random_ints(NULL, count, 0, U64_C(1) + U32_MAX);
+    size_t amount    = (count + 1) * sizeof(u32_t);
+    char  *allocated = (char*)CALLOCATE(amount, sizeof(char));
+    char  *encoded   = allocated + 1; /* Intentionally misaligned. */
+    char  *target    = encoded;
+    size_t i;
+
+    for (i = 0; i < count; i++) {
+        NumUtil_encode_bigend_u32(ints[i], &target);
+        target += sizeof(u32_t);
+    }
+    target = encoded;
+    for (i = 0; i < count; i++) {
+        u32_t got = NumUtil_decode_bigend_u32(target);
+        ASSERT_INT_EQ(batch, got, ints[i], "bigend u32");
+        target += sizeof(u32_t);
+    }
+
+    target = encoded;
+    NumUtil_encode_bigend_u32(1, &target);
+    ASSERT_INT_EQ(batch, encoded[0], 0, "Truly big-endian u32");
+    ASSERT_INT_EQ(batch, encoded[3], 1, "Truly big-endian u32");
+
+    FREEMEM(allocated);
+    FREEMEM(ints);
+}
+
+static void
+test_bigend_u64(TestBatch *batch)
+{
+    size_t count     = 32;
+    u64_t *ints      = S_random_ints(NULL, count, 0, U64_MAX);
+    size_t amount    = (count + 1) * sizeof(u64_t);
+    char  *allocated = (char*)CALLOCATE(amount, sizeof(char));
+    char  *encoded   = allocated + 1; /* Intentionally misaligned. */
+    char  *target    = encoded;
+    size_t i;
+
+    for (i = 0; i < count; i++) {
+        NumUtil_encode_bigend_u64(ints[i], &target);
+        target += sizeof(u64_t);
+    }
+    target = encoded;
+    for (i = 0; i < count; i++) {
+        u64_t got = NumUtil_decode_bigend_u64(target);
+        ASSERT_INT_EQ(batch, got, ints[i], "bigend u64");
+        target += sizeof(u64_t);
+    }
+
+    target = encoded;
+    NumUtil_encode_bigend_u64(1, &target);
+    ASSERT_INT_EQ(batch, encoded[0], 0, "Truly big-endian");
+    ASSERT_INT_EQ(batch, encoded[7], 1, "Truly big-endian");
+
+    FREEMEM(allocated);
+    FREEMEM(ints);
+}
+
+static void
+test_bigend_f32(TestBatch *batch)
+{
+    float source[]  = { -1.3f, 0.0f, 100.2f };
+    size_t count     = 3;
+    size_t amount    = (count + 1) * sizeof(float);
+    u8_t  *allocated = (u8_t*)CALLOCATE(amount, sizeof(u8_t));
+    u8_t  *encoded   = allocated + 1; /* Intentionally misaligned. */
+    u8_t  *target    = encoded;
+    size_t i;
+
+    for (i = 0; i < count; i++) {
+        NumUtil_encode_bigend_f32(source[i], &target);
+        target += sizeof(float);
+    }
+    target = encoded;
+    for (i = 0; i < count; i++) {
+        float got = NumUtil_decode_bigend_f32(target);
+        ASSERT_INT_EQ(batch, got, source[i], "bigend f32");
+        target += sizeof(float);
+    }
+
+    target = encoded;
+    NumUtil_encode_bigend_f32(-2.0f, &target);
+    ASSERT_INT_EQ(batch, (encoded[0] & 0x80), 0x80, 
+        "Truly big-endian (IEEE 754 sign bit set for negative number)");
+    ASSERT_INT_EQ(batch, encoded[0], 0xC0, 
+        "IEEE 754 representation of -2.0f, byte 0");
+    for (i = 1; i < sizeof(float); i++) {
+        ASSERT_INT_EQ(batch, encoded[i], 0, 
+            "IEEE 754 representation of -2.0f, byte %d", (int)i);
+    }
+
+    FREEMEM(allocated);
+}
+
+static void
+test_bigend_f64(TestBatch *batch)
+{
+    double source[]  = { -1.3, 0.0, 100.2 };
+    size_t count     = 3;
+    size_t amount    = (count + 1) * sizeof(double);
+    u8_t  *allocated = (u8_t*)CALLOCATE(amount, sizeof(u8_t));
+    u8_t  *encoded   = allocated + 1; /* Intentionally misaligned. */
+    u8_t  *target    = encoded;
+    size_t i;
+
+    for (i = 0; i < count; i++) {
+        NumUtil_encode_bigend_f64(source[i], &target);
+        target += sizeof(double);
+    }
+    target = encoded;
+    for (i = 0; i < count; i++) {
+        double got = NumUtil_decode_bigend_f64(target);
+        ASSERT_INT_EQ(batch, got, source[i], "bigend f64");
+        target += sizeof(double);
+    }
+
+    target = encoded;
+    NumUtil_encode_bigend_f64(-2.0, &target);
+    ASSERT_INT_EQ(batch, (encoded[0] & 0x80), 0x80, 
+        "Truly big-endian (IEEE 754 sign bit set for negative number)");
+    ASSERT_INT_EQ(batch, encoded[0], 0xC0, 
+        "IEEE 754 representation of -2.0, byte 0");
+    for (i = 1; i < sizeof(double); i++) {
+        ASSERT_INT_EQ(batch, encoded[i], 0, 
+            "IEEE 754 representation of -2.0, byte %d", (int)i);
+    }
+
+    FREEMEM(allocated);
+}
+
+void
+TestNumUtil_run_tests()
+{
+    TestBatch *batch = Test_new_batch("TestNumberUtils", 1196, NULL);
+
+    PLAN(batch);
+    srand((unsigned int)time((time_t*)NULL));
+
+    test_u1(batch);
+    test_u2(batch);
+    test_u4(batch);
+    test_c32(batch);
+    test_c64(batch);
+    test_bigend_u16(batch);
+    test_bigend_u32(batch);
+    test_bigend_u64(batch);
+    test_bigend_f32(batch);
+    test_bigend_f64(batch);
+
+    batch->destroy(batch);
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Util/TestNumberUtils.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Util/NumberUtils.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Util/NumberUtils.bp?rev=813557&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Util/NumberUtils.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Util/NumberUtils.bp Thu Sep 10 19:32:40 2009
@@ -0,0 +1,482 @@
+parcel Lucy;
+
+/** Provide various number-related utilies.
+ *
+ * Provide utilities for dealing with endian issues, sub-byte-width arrays,
+ * compressed integers, and so on.
+ */
+inert class Lucy::Util::NumberUtils cnick NumUtil { 
+
+    inert const u8_t[8] u1masks;
+    inert const u8_t[4] u2masks;
+    inert const u8_t[4] u2shifts;
+    inert const u8_t[2] u4masks;
+    inert const u8_t[2] u4shifts;
+    
+    /** Encode an unsigned 16-bit integer as 2 bytes in the buffer provided,
+     * using big-endian byte order.
+     */
+    inert inline void
+    encode_bigend_u16(u16_t value, void *dest);
+
+    /** Encode an unsigned 32-bit integer as 4 bytes in the buffer provided,
+     * using big-endian byte order.
+     */
+    inert inline void
+    encode_bigend_u32(u32_t value, void *dest);
+
+    /** Encode an unsigned 64-bit integer as 8 bytes in the buffer provided,
+     * using big-endian byte order. 
+     */
+    inert inline void
+    encode_bigend_u64(u64_t value, void *dest);
+
+    /** Interpret a sequence of bytes as a big-endian unsigned 16-bit int.
+     */
+    inert inline u16_t
+    decode_bigend_u16(void *source);
+
+    /** Interpret a sequence of bytes as a big-endian unsigned 32-bit int.
+     */
+    inert inline u32_t
+    decode_bigend_u32(void *source);
+
+    /** Interpret a sequence of bytes as a big-endian unsigned 64-bit int.
+     */
+    inert inline u64_t
+    decode_bigend_u64(void *source);
+
+    /** Encode a 32-bit floating point number as 4 bytes in the buffer
+     * provided, using big-endian byte order. 
+     */
+    inert inline void 
+    encode_bigend_f32(float value, void *dest);
+
+    /** Encode a 64-bit floating point number as 8 bytes in the buffer
+     * provided, using big-endian byte order. 
+     */
+    inert inline void 
+    encode_bigend_f64(double value, void *dest);
+
+    /** Interpret a sequence of bytes as a 32-bit float stored in big-endian
+     * byte order.
+     */
+    inert inline float 
+    decode_bigend_f32(void *source);
+
+    /** Interpret a sequence of bytes as a 64-bit float stored in big-endian
+     * byte order.
+     */
+    inert inline double 
+    decode_bigend_f64(void *source);
+
+    /** Encode a C32 at the space pointed to by <code>dest</code>. As a side
+     * effect, <code>dest</code> will be advanced to immediately after the end
+     * of the C32.
+     */
+    inert inline void
+    encode_c32(u32_t value, char **dest);
+
+    /** Encode a C32 at the space pointed to by <code>dest</code>, but add
+     * "leading zeroes" so that the space consumed will always be 5 bytes.  As
+     * a side effect, <code>dest</code> will be advanced to immediately after
+     * the end of the C32.
+     */
+    inert inline void
+    encode_padded_c32(u32_t value, char **dest);
+
+    /** Encode a C64 at the space pointed to by <code>dest</code>. As a side
+     * effect, <code>dest</code> will be advanced to immediately after the end
+     * of the C64.
+     */
+    inert inline void
+    encode_c64(u64_t value, char **dest);
+
+    /** Read a C32 from the buffer pointed to by <code>source</code>.  As a
+     * side effect, advance the pointer, consuming the bytes occupied by the
+     * C32.
+     */
+    inert inline u32_t
+    decode_c32(char **source);
+
+    /** Read a C64 from the buffer pointed to by <code>source</code>.  As a
+     * side effect, advance the pointer, consuming the bytes occupied by the
+     * C64.
+     */
+    inert inline u64_t
+    decode_c64(char **source);
+
+    /** Advance <code>source</code> past one encoded C32 or C64.
+     */
+    inert inline void 
+    skip_cint(char **source);
+
+    /** Interpret <code>array</code> as an array of bits; return true if the
+     * bit at <code>tick</code> is set, false otherwise.
+     */
+    inert inline bool_t
+    u1get(void *array, u32_t tick);
+
+    /** Interpret <code>array</code> as an array of bits; set the bit at
+     * <code>tick</code>.
+     */
+    inert inline void 
+    u1set(void *array, u32_t tick);
+
+    /** Interpret <code>array</code> as an array of bits; flip the bit at
+     * <code>tick</code>.
+     */
+    inert inline void
+    u1flip(void *array, u32_t tick);
+
+    /** Interpret <code>array</code> as an array of two-bit integers; return
+     * the value at <code>tick</code>.
+     */
+    inert inline u8_t
+    u2get(void *array, u32_t tick);
+
+    /** Interpret <code>array</code> as an array of two-bit integers; set the
+     * element at <code>tick</code> to <code>value</code>.
+     */
+    inert inline void 
+    u2set(void *array, u32_t tick, u8_t value);
+
+    /** Interpret <code>array</code> as an array of four-bit integers; return
+     * the value at <code>tick</code>.
+     */
+    inert inline u8_t
+    u4get(void *array, u32_t tick);
+
+    /** Interpret <code>array</code> as an array of four-bit integers; set the
+     * element at <code>tick</code> to <code>value</code>.
+     */
+    inert inline void 
+    u4set(void *array, u32_t tick, u8_t value);
+}
+
+__C__
+
+static CHY_INLINE void
+lucy_NumUtil_encode_bigend_u16(chy_u16_t num, void *dest_ptr)
+{
+    chy_u8_t *dest = *(chy_u8_t**)dest_ptr;
+#ifdef CHY_BIG_END
+    memcpy(dest, &num, sizeof(chy_u16_t));
+#else /* little endian */
+    chy_u8_t *source = (chy_u8_t*)&num;
+    dest[0] = source[1];
+    dest[1] = source[0];
+#endif /* CHY_BIG_END (and little endian) */
+}
+
+static CHY_INLINE void
+lucy_NumUtil_encode_bigend_u32(chy_u32_t num, void *dest_ptr)
+{
+    chy_u8_t *dest = *(chy_u8_t**)dest_ptr;
+#ifdef CHY_BIG_END
+    memcpy(dest, &num, sizeof(chy_u32_t));
+#else /* little endian */
+    chy_u8_t *source = (chy_u8_t*)&num;
+    dest[0] = source[3];
+    dest[1] = source[2];
+    dest[2] = source[1];
+    dest[3] = source[0];
+#endif /* CHY_BIG_END (and little endian) */
+}
+
+static CHY_INLINE void
+lucy_NumUtil_encode_bigend_u64(chy_u64_t num, void *dest_ptr)
+{
+    chy_u8_t *dest = *(chy_u8_t**)dest_ptr;
+#ifdef CHY_BIG_END
+    memcpy(dest, &num, sizeof(chy_u64_t));
+#else /* little endian */
+    chy_u8_t *source = (chy_u8_t*)&num;
+    dest[0] = source[7];
+    dest[1] = source[6];
+    dest[2] = source[5];
+    dest[3] = source[4];
+    dest[4] = source[3];
+    dest[5] = source[2];
+    dest[6] = source[1];
+    dest[7] = source[0];
+#endif /* CHY_BIG_END (and little endian) */
+}
+
+static CHY_INLINE chy_u16_t
+lucy_NumUtil_decode_bigend_u16(void *source)
+{
+    chy_u8_t *const buf = (chy_u8_t*)source;
+    return  (buf[0] << 8) |
+            (buf[1]);
+}
+
+static CHY_INLINE chy_u32_t
+lucy_NumUtil_decode_bigend_u32(void *source)
+{
+    chy_u8_t *const buf = (chy_u8_t*)source;
+    return  (buf[0]  << 24) |
+            (buf[1]  << 16) |
+            (buf[2]  << 8)  |
+            (buf[3]);
+}
+
+static CHY_INLINE chy_u64_t
+lucy_NumUtil_decode_bigend_u64(void *source)
+{
+    chy_u8_t *const buf = (chy_u8_t*)source;
+    chy_u64_t high_bits = (buf[0]  << 24) |
+                          (buf[1]  << 16) |
+                          (buf[2]  << 8)  |
+                          (buf[3]);
+    chy_u32_t low_bits  = (buf[4]  << 24) |
+                          (buf[5]  << 16) |
+                          (buf[6]  << 8)  |
+                          (buf[7]);
+    chy_u64_t retval = high_bits << 32;
+    retval |= low_bits;
+    return retval;
+}
+
+static CHY_INLINE void 
+lucy_NumUtil_encode_bigend_f32(float value, void *dest_ptr)
+{
+    chy_u8_t *dest = *(chy_u8_t**)dest_ptr;
+#ifdef CHY_BIG_END
+    memcpy(dest, &value, sizeof(float));
+#else 
+    union { float f; chy_u32_t u32; } duo; 
+    duo.f = value;
+    lucy_NumUtil_encode_bigend_u32(duo.u32, &dest);
+#endif
+}
+
+static CHY_INLINE void 
+lucy_NumUtil_encode_bigend_f64(double value, void *dest_ptr)
+{
+    chy_u8_t *dest = *(chy_u8_t**)dest_ptr;
+#ifdef CHY_BIG_END
+    memcpy(dest, &value, sizeof(double));
+#else 
+    union { double d; chy_u64_t u64; } duo; 
+    duo.d = value;
+    lucy_NumUtil_encode_bigend_u64(duo.u64, &dest);
+#endif
+}
+
+static CHY_INLINE float 
+lucy_NumUtil_decode_bigend_f32(void *source)
+{
+    union { float f; chy_u32_t u32; } duo;
+    memcpy(&duo, source, sizeof(float));
+#ifdef CHY_LITTLE_END 
+    duo.u32 = lucy_NumUtil_decode_bigend_u32(&duo.u32);
+#endif
+    return duo.f;
+}
+
+static CHY_INLINE double 
+lucy_NumUtil_decode_bigend_f64(void *source)
+{
+    union { double d; chy_u64_t u64; } duo;
+    memcpy(&duo, source, sizeof(double));
+#ifdef CHY_LITTLE_END 
+    duo.u64 = lucy_NumUtil_decode_bigend_u64(&duo.u64);
+#endif
+    return duo.d;
+}
+
+#define LUCY_NUMUTIL_C32_MAX_BYTES  ((sizeof(chy_u32_t) * 8 / 7) + 1) /* 5 */
+#define LUCY_NUMUTIL_C64_MAX_BYTES ((sizeof(chy_u64_t) * 8 / 7) + 1)  /* 10 */
+
+static CHY_INLINE void
+lucy_NumUtil_encode_c32(chy_u32_t number, char **out_buf)
+{
+    chy_u8_t   buf[LUCY_NUMUTIL_C32_MAX_BYTES];
+    chy_u32_t  aU32        = number;
+    chy_u8_t  *const limit = buf + sizeof(buf);
+    chy_u8_t  *ptr         = limit - 1;
+    int        num_bytes;
+    /* Write last byte first, which has no continue bit. */
+    *ptr = aU32 & 0x7f;
+    aU32 >>= 7;
+    while (aU32) {
+        /* Work backwards, writing bytes with continue bits set. */
+        *--ptr = ((aU32 & 0x7f) | 0x80);
+        aU32 >>= 7;
+    }
+    num_bytes = limit - ptr;
+    memcpy(*out_buf, ptr, num_bytes);
+    *out_buf += num_bytes;
+}
+
+static CHY_INLINE void
+lucy_NumUtil_encode_c64(chy_u64_t number, char **out_buf)
+{
+    chy_u8_t   buf[LUCY_NUMUTIL_C64_MAX_BYTES];
+    chy_u64_t  aU64        = number;
+    chy_u8_t  *const limit = buf + sizeof(buf);
+    chy_u8_t  *ptr         = limit - 1;
+    int        num_bytes;
+    /* Write last byte first, which has no continue bit. */
+    *ptr = aU64 & 0x7f;
+    aU64 >>= 7;
+    while (aU64) {
+        /* Work backwards, writing bytes with continue bits set. */
+        *--ptr = ((aU64 & 0x7f) | 0x80);
+        aU64 >>= 7;
+    }
+    num_bytes = limit - ptr;
+    memcpy(*out_buf, ptr, num_bytes);
+    *out_buf += num_bytes;
+}
+
+static CHY_INLINE void
+lucy_NumUtil_encode_padded_c32(chy_u32_t number, char **out_buf)
+{
+    chy_u8_t buf[LUCY_NUMUTIL_C32_MAX_BYTES]
+        = { 0x80, 0x80, 0x80, 0x80, 0x80 };
+    chy_u32_t aU32        = number;
+    chy_u8_t *const limit = buf + sizeof(buf);
+    chy_u8_t *ptr         = limit - 1;
+    /* Write last byte first, which has no continue bit. */
+    *ptr = aU32 & 0x7f;
+    aU32 >>= 7;
+    while (aU32) {
+        /* Work backwards, writing bytes with continue bits set. */
+        *--ptr = ((aU32 & 0x7f) | 0x80);
+        aU32 >>= 7;
+    }
+    memcpy(*out_buf, buf, LUCY_NUMUTIL_C32_MAX_BYTES);
+    *out_buf += sizeof(buf);
+}
+
+/* Decode a compressed integer up to size of 'var', advancing 'source' */
+#define LUCY_NUMUTIL_DECODE_CINT(var, source) \
+    do { \
+        var = (*source & 0x7f); \
+        while (*source++ & 0x80) { \
+            var = (*source & 0x7f) | (var << 7); \
+        }  \
+    } while (0)
+
+static CHY_INLINE chy_u32_t
+lucy_NumUtil_decode_c32(char **source_ptr)
+{
+    char *source = *source_ptr;
+    chy_u32_t decoded = (*source & 0x7f); 
+    LUCY_NUMUTIL_DECODE_CINT(decoded, source);
+    *source_ptr = source;
+    return decoded;
+}
+
+static CHY_INLINE chy_u64_t
+lucy_NumUtil_decode_c64(char **source_ptr)
+{
+    char *source = *source_ptr;
+    chy_u64_t decoded = (*source & 0x7f); 
+    LUCY_NUMUTIL_DECODE_CINT(decoded, source);
+    *source_ptr = source;
+    return decoded;
+}
+
+static CHY_INLINE void
+lucy_NumUtil_skip_cint(char **source_ptr)
+{
+    chy_u8_t *ptr = *(chy_u8_t**)source_ptr;
+    while ((*ptr++ & 0x80) != 0) { }
+    *source_ptr = (char*)ptr;
+}
+
+static CHY_INLINE chy_bool_t
+lucy_NumUtil_u1get(void *array, chy_u32_t tick) 
+{
+    chy_u8_t *const u8bits      = (chy_u8_t*)array;
+    const chy_u32_t byte_offset = tick >> 3;
+    const chy_u8_t  mask        = lucy_NumUtil_u1masks[tick & 0x7];
+    return !((u8bits[byte_offset] & mask) == 0);
+}
+
+static CHY_INLINE void 
+lucy_NumUtil_u1set(void *array, chy_u32_t tick) 
+{
+    chy_u8_t *const u8bits      = (chy_u8_t*)array;
+    const chy_u32_t byte_offset = tick >> 3;
+    const chy_u8_t  mask        = lucy_NumUtil_u1masks[tick & 0x7];
+    u8bits[byte_offset] |= mask;
+}
+
+static CHY_INLINE void
+lucy_NumUtil_u1flip(void *array, chy_u32_t tick) 
+{
+    chy_u8_t *const u8bits      = (chy_u8_t*)array;
+    const chy_u32_t byte_offset = tick >> 3;
+    const chy_u8_t  mask        = lucy_NumUtil_u1masks[tick & 0x7];
+    u8bits[byte_offset] ^= mask;
+}
+
+static CHY_INLINE chy_u8_t
+lucy_NumUtil_u2get(void *array, chy_u32_t tick) 
+{
+    chy_u8_t *ints  = (chy_u8_t*)array;
+    chy_u8_t  byte  = ints[ (tick >> 2) ];
+    int       shift = lucy_NumUtil_u2shifts[ tick & 0x3 ];
+    return (byte >> shift) & 0x3;
+}
+
+static CHY_INLINE void 
+lucy_NumUtil_u2set(void *array, chy_u32_t tick, chy_u8_t value) 
+{
+    chy_u8_t *ints     = (chy_u8_t*)array;
+    unsigned  sub_tick = tick & 0x3;
+    int       shift    = lucy_NumUtil_u2shifts[sub_tick];
+    chy_u8_t  mask     = lucy_NumUtil_u2masks[sub_tick];
+    chy_u8_t  new_val  = value & 0x3;
+    chy_u8_t  new_bits = new_val << shift;
+    ints[(tick >> 2)]  = (ints[(tick >> 2)] & ~mask) | new_bits;
+}
+
+
+static CHY_INLINE chy_u8_t
+lucy_NumUtil_u4get(void *array, chy_u32_t tick) 
+{
+    chy_u8_t *ints  = (chy_u8_t*)array;
+    chy_u8_t  byte  = ints[ (tick >> 1) ];
+    int       shift = lucy_NumUtil_u4shifts[(tick & 1)]; 
+    return (byte >> shift) & 0xF;
+}
+
+static CHY_INLINE void 
+lucy_NumUtil_u4set(void *array, chy_u32_t tick, chy_u8_t value) 
+{
+    chy_u8_t *ints     = (chy_u8_t*)array;
+    unsigned  sub_tick = tick & 0x1;
+    int       shift    = lucy_NumUtil_u4shifts[sub_tick];
+    chy_u8_t  mask     = lucy_NumUtil_u4masks[sub_tick];
+    chy_u8_t  new_val  = value & 0xF;
+    chy_u8_t  new_bits = new_val << shift;
+    ints[(tick >> 1)]  = (ints[(tick >> 1)] & ~mask) | new_bits;
+}
+
+#ifdef LUCY_USE_SHORT_NAMES
+  #define C32_MAX_BYTES                LUCY_NUMUTIL_C32_MAX_BYTES
+  #define C64_MAX_BYTES                LUCY_NUMUTIL_C64_MAX_BYTES
+#endif
+
+__END_C__
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Util/NumberUtils.bp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Util/NumberUtils.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Util/NumberUtils.c?rev=813557&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Util/NumberUtils.c (added)
+++ lucene/lucy/trunk/core/Lucy/Util/NumberUtils.c Thu Sep 10 19:32:40 2009
@@ -0,0 +1,33 @@
+#define C_LUCY_NUMBERUTILS
+#define LUCY_USE_SHORT_NAMES
+#define CHY_USE_SHORT_NAMES
+
+#include <string.h>
+
+#include "Lucy/Util/NumberUtils.h"
+
+const chy_u8_t NumUtil_u1masks[8] = {
+    0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
+};
+
+const u8_t NumUtil_u2shifts[4] = { 0x0, 0x2, 0x4,  0x6  };
+const u8_t NumUtil_u2masks[4]  = { 0x3, 0xC, 0x30, 0xC0 };
+
+const u8_t NumUtil_u4shifts[2] = { 0x00, 0x04 };
+const u8_t NumUtil_u4masks[2]  = { 0x0F, 0xF0 };
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Util/NumberUtils.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: lucene/lucy/trunk/perl/lib/Lucy/Test.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Test.pm?rev=813557&r1=813556&r2=813557&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Test.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy/Test.pm Thu Sep 10 19:32:40 2009
@@ -15,7 +15,10 @@
 PPCODE:
 {
     /* Lucy::Util */
-    if (strEQ(package, "TestStringHelper")) {
+    if (strEQ(package, "TestNumberUtils")) {
+        lucy_TestNumUtil_run_tests();
+    }
+    else if (strEQ(package, "TestStringHelper")) {
         lucy_TestStrHelp_run_tests();
     }
     else {

Added: lucene/lucy/trunk/perl/t/core/030-number_utils.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/t/core/030-number_utils.t?rev=813557&view=auto
==============================================================================
--- lucene/lucy/trunk/perl/t/core/030-number_utils.t (added)
+++ lucene/lucy/trunk/perl/t/core/030-number_utils.t Thu Sep 10 19:32:40 2009
@@ -0,0 +1,6 @@
+use strict;
+use warnings;
+
+use Lucy::Test;
+Lucy::Test::run_tests("TestNumberUtils");
+

Propchange: lucene/lucy/trunk/perl/t/core/030-number_utils.t
------------------------------------------------------------------------------
    svn:eol-style = native