lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [1/2] git commit: refs/heads/0.4 - Optimize and fix encoding of similarity values
Date Sun, 19 Oct 2014 18:20:54 GMT
Repository: lucy
Updated Branches:
  refs/heads/0.4 574ba827c -> 853494bc5


Optimize and fix encoding of similarity values

The previous code was a bit unclear as it extracted the LSB of the
exponent as part of the mantissa. This did actually work because
exponent and mantissa were stored next to each other like in the IEEE
format. For the same reason, the code can be optimized to use a single
shift and mask operation. The actual encoding does not change.

More importantly, this commit fixes a bug where, due to a missing range
check, values smaller than 2^-31 were encoded as 255 and subsequently
decoded as approximately 2^31. Such small values are probably rare but
this could cause significant errors when calculating similarities.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/9e574bd2
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/9e574bd2
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/9e574bd2

Branch: refs/heads/0.4
Commit: 9e574bd2b86147cbdf3060bea257fde22b3b7910
Parents: 574ba82
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sun Oct 19 17:50:25 2014 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Sun Oct 19 20:20:20 2014 +0200

----------------------------------------------------------------------
 core/Lucy/Index/Similarity.c   | 26 +++++++++++++++++++-------
 core/Lucy/Index/Similarity.cfh |  2 +-
 perl/t/504-similarity.t        |  7 ++++++-
 3 files changed, 26 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/9e574bd2/core/Lucy/Index/Similarity.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Index/Similarity.c b/core/Lucy/Index/Similarity.c
index cedb6e6..cceffa8 100644
--- a/core/Lucy/Index/Similarity.c
+++ b/core/Lucy/Index/Similarity.c
@@ -31,6 +31,12 @@
 #include "Lucy/Store/OutStream.h"
 #include "Lucy/Util/Freezer.h"
 
+// The exponent range [-31;32] is mapped to [0;63]. Values outside
+// of the range are clamped resulting in 6 bits for the exponent.
+// The IEEE bias is 127, so we have to subtract 127 and add 31 to
+// the upper bits.
+#define EXP_OFFSET ((127 - 31) << 2)
+
 Similarity*
 Sim_new() {
     Similarity *self = (Similarity*)Class_Make_Obj(SIMILARITY);
@@ -157,15 +163,21 @@ Sim_Encode_Norm_IMP(Similarity *self, float f) {
     }
     else {
         const uint32_t bits = *(uint32_t*)&f;
-        uint32_t mantissa   = (bits & 0xffffff) >> 21;
-        uint32_t exponent   = (((bits >> 24) & 0x7f) - 63) + 15;
 
-        if (exponent > 31) {
-            exponent = 31;
-            mantissa = 7;
-        }
+        // The normalized value contains two bits of mantissa (excluding
+        // the implicit leading bit) in the least significant bits and the
+        // exponent in the upper bits.
+        norm = (bits >> 21) & 0x3ff;
 
-        norm = (exponent << 3) | mantissa;
+        if (norm <= EXP_OFFSET) {
+            norm = 0;
+        }
+        else {
+            norm -= EXP_OFFSET;
+            if (norm > 255) {
+                norm = 255;
+            }
+        }
     }
 
     return norm;

http://git-wip-us.apache.org/repos/asf/lucy/blob/9e574bd2/core/Lucy/Index/Similarity.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Index/Similarity.cfh b/core/Lucy/Index/Similarity.cfh
index 4798fb3..ad07f08 100644
--- a/core/Lucy/Index/Similarity.cfh
+++ b/core/Lucy/Index/Similarity.cfh
@@ -105,7 +105,7 @@ public class Lucy::Index::Similarity nickname Sim inherits Clownfish::Obj
{
     Query_Norm(Similarity *self, float sum_of_squared_weights);
 
     /** encode_norm and decode_norm encode and decode between 32-bit IEEE
-     * floating point numbers and a 5-bit exponent, 3-bit mantissa float.  The
+     * floating point numbers and a 6-bit exponent, 3-bit mantissa float.  The
      * range covered by the single-byte encoding is 7x10^9 to 2x10^-9.  The
      * accuracy is about one significant decimal digit.
      */

http://git-wip-us.apache.org/repos/asf/lucy/blob/9e574bd2/perl/t/504-similarity.t
----------------------------------------------------------------------
diff --git a/perl/t/504-similarity.t b/perl/t/504-similarity.t
index a4d8666..f2f5afe 100644
--- a/perl/t/504-similarity.t
+++ b/perl/t/504-similarity.t
@@ -38,7 +38,7 @@ sub new {
 }
 
 package main;
-use Test::More tests => 9;
+use Test::More tests => 10;
 use Lucy::Test;
 use bytes;
 no bytes;
@@ -79,6 +79,11 @@ for ( 0 .. 255 ) {
 is_deeply( \@transformed, \@floats,
     "using the norm_decoder produces desired results" );
 
+my $small_encoded = $sim->encode_norm(1e-30);
+my $large_encoded = $sim->encode_norm(1e30);
+ok( $small_encoded != $large_encoded,
+    "extremely small and large values are encoded differently" );
+
 my $folder  = Lucy::Store::RAMFolder->new;
 my $indexer = Lucy::Index::Indexer->new(
     index  => $folder,


Mime
View raw message