]> Savannah Git Hosting - gnulib.git/commitdiff
dfa: shrink constraints from 4 bits to 3
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 10 Jan 2017 06:48:46 +0000 (22:48 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 10 Jan 2017 10:13:13 +0000 (02:13 -0800)
* lib/dfa.c (newline_constraint, letter_constraint)
(other_constraint, prev_newline_dependent)
(prev_letter_dependent, NO_CONSTRAINT, BEGLINE_CONSTRAINT)
(ENDLINE_CONSTRAINT, BEGWORD_CONSTRAINT, ENDWORD_CONSTRAINT)
(LIMWORD_CONSTRAINT, NOTLIMWORD_CONSTRAINT):
Constraints need only 3 bits, not 4.  Using smaller integers
shrinks the code a bit and makes grep a tad faster on x86-64.

ChangeLog
lib/dfa.c

index 91cdc6d31e6cc98799039dd12cca4f3fb473693e..e0b73b50be1d99453467f4506da2bf89293ef9fe 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2017-01-09  Paul Eggert  <eggert@cs.ucla.edu>
 
+       dfa: shrink constraints from 4 bits to 3
+       * lib/dfa.c (newline_constraint, letter_constraint)
+       (other_constraint, prev_newline_dependent)
+       (prev_letter_dependent, NO_CONSTRAINT, BEGLINE_CONSTRAINT)
+       (ENDLINE_CONSTRAINT, BEGWORD_CONSTRAINT, ENDWORD_CONSTRAINT)
+       (LIMWORD_CONSTRAINT, NOTLIMWORD_CONSTRAINT):
+       Constraints need only 3 bits, not 4.  Using smaller integers
+       shrinks the code a bit and makes grep a tad faster on x86-64.
+
        dfa: omit unnecessary ptrdiff_t check
        * lib/dfa.c (alloc_position_set): Do not worry about ptrdiff_t
        overflow, since xnmalloc does that now.
index 509d6d1cbd23de9e6969a54e27b4c8a35ba6dd80..28678c2327331323d171badb71331de1688ec38c 100644 (file)
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -137,13 +137,13 @@ enum
 /* Sometimes characters can only be matched depending on the surrounding
    context.  Such context decisions depend on what the previous character
    was, and the value of the current (lookahead) character.  Context
-   dependent constraints are encoded as 12-bit integers.  Each bit that
+   dependent constraints are encoded as 9-bit integers.  Each bit that
    is set indicates that the constraint succeeds in the corresponding
    context.
 
-   bit 8-11 - valid contexts when next character is CTX_NEWLINE
-   bit 4-7  - valid contexts when next character is CTX_LETTER
-   bit 0-3  - valid contexts when next character is CTX_NONE
+   bit 6-8  - valid contexts when next character is CTX_NEWLINE
+   bit 3-5  - valid contexts when next character is CTX_LETTER
+   bit 0-2  - valid contexts when next character is CTX_NONE
 
    succeeds_in_context determines whether a given constraint
    succeeds in a particular context.  Prev is a bitmask of possible
@@ -152,17 +152,17 @@ enum
 static int
 newline_constraint (int constraint)
 {
-  return (constraint >> 8) & 0xf;
+  return (constraint >> 6) & 7;
 }
 static int
 letter_constraint (int constraint)
 {
-  return (constraint >> 4) & 0xf;
+  return (constraint >> 3) & 7;
 }
 static int
 other_constraint (int constraint)
 {
-  return constraint & 0xf;
+  return constraint & 7;
 }
 
 static bool
@@ -178,12 +178,12 @@ succeeds_in_context (int constraint, int prev, int curr)
 static bool
 prev_newline_dependent (int constraint)
 {
-  return ((constraint ^ constraint >> 2) & 0x111) != 0;
+  return ((constraint ^ constraint >> 2) & 0111) != 0;
 }
 static bool
 prev_letter_dependent (int constraint)
 {
-  return ((constraint ^ constraint >> 1) & 0x111) != 0;
+  return ((constraint ^ constraint >> 1) & 0111) != 0;
 }
 
 /* Tokens that match the empty string subject to some constraint actually
@@ -192,13 +192,13 @@ prev_letter_dependent (int constraint)
    the constraints corresponding to the special tokens previously defined.  */
 enum
   {
-    NO_CONSTRAINT = 0x777,
-    BEGLINE_CONSTRAINT = 0x444,
-    ENDLINE_CONSTRAINT = 0x700,
-    BEGWORD_CONSTRAINT = 0x050,
-    ENDWORD_CONSTRAINT = 0x202,
-    LIMWORD_CONSTRAINT = 0x252,
-    NOTLIMWORD_CONSTRAINT = 0x525
+    NO_CONSTRAINT = 0777,
+    BEGLINE_CONSTRAINT = 0444,
+    ENDLINE_CONSTRAINT = 0700,
+    BEGWORD_CONSTRAINT = 0050,
+    ENDWORD_CONSTRAINT = 0202,
+    LIMWORD_CONSTRAINT = 0252,
+    NOTLIMWORD_CONSTRAINT = 0525
   };
 
 /* The regexp is parsed into an array of tokens in postfix form.  Some tokens