up

A priority search queue and LRU cache.
git clone git://git.jtobin.io/up.git
Log | Files | Refs

up_peak.c (522B)


      1 #include "all.h"
      2 
      3 u3_noun
      4 u3qdu_peak(u3_noun k, u3_noun l)
      5 {
      6   u3_atom h = u3r_mug(k);
      7   u3_atom i = u3r_mug(l);
      8 
      9   c3_w a = h ^ i;
     10 
     11   // see
     12   //
     13   // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
     14 
     15   a |= a >> 1;
     16   a |= a >> 2;
     17   a |= a >> 4;
     18   a |= a >> 8;
     19   a |= a >> 16;
     20   a ^= a >> 1;
     21 
     22   return a;
     23 }
     24 
     25 u3_noun
     26 u3wdu_peak(u3_noun cor)
     27 {
     28   u3_noun a, b;
     29 
     30   if (c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0))
     31   {
     32     return u3m_bail(c3__exit);
     33   } else {
     34     return u3qdu_peak(a, b);
     35   }
     36 }
     37