sm64

A Super Mario 64 decompilation
Log | Files | Refs | README | LICENSE

quant.c (1247B)


      1 #include "vadpcm.h"
      2 
      3 /**
      4  * Compute x / scale rounded to the nearest integer, with x.5 (fuzzy with an
      5  * epsilon of 1e-7) rounding towards zero.
      6  */
      7 s16 qsample(f32 x, s32 scale)
      8 {
      9     if (x > 0.0f)
     10     {
     11         return (s16) ((x / scale) + 0.4999999);
     12     }
     13     else
     14     {
     15         return (s16) ((x / scale) - 0.4999999);
     16     }
     17 }
     18 
     19 /**
     20  * Round all ('fs' many) values in 'e' to the nearest 'bits'-bit integer,
     21  * outputting to 'ie'.
     22  */
     23 void clamp(s32 fs, f32 *e, s32 *ie, s32 bits)
     24 {
     25     s32 i;
     26     f32 ulevel;
     27     f32 llevel;
     28 
     29     llevel = -(f32) (1 << (bits - 1));
     30     ulevel = -llevel - 1.0f;
     31     for (i = 0; i < fs; i++)
     32     {
     33         if (e[i] > ulevel)
     34         {
     35             e[i] = ulevel;
     36         }
     37         if (e[i] < llevel)
     38         {
     39             e[i] = llevel;
     40         }
     41 
     42         if (e[i] > 0.0f)
     43         {
     44             ie[i] = (s32) (e[i] + 0.5);
     45         }
     46         else
     47         {
     48             ie[i] = (s32) (e[i] - 0.5);
     49         }
     50     }
     51 }
     52 
     53 /**
     54  * Clamp ix to within [llevel, ulevel].
     55  */
     56 s32 clip(s32 ix, s32 llevel, s32 ulevel)
     57 {
     58     if (ix < llevel || ix > ulevel)
     59     {
     60         if (ix < llevel)
     61         {
     62             return llevel;
     63         }
     64         if (ix > ulevel)
     65         {
     66             return ulevel;
     67         }
     68     }
     69     return ix;
     70 }