Guest User

Untitled

a guest
Dec 11th, 2017
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. #define CHR_0           0x30
  2. #define CHR_9           0x39
  3. #define UPCASE_BIT      0x20
  4. #define CAP_A           0x41
  5. #define CAP_Z           0x5A
  6.  
  7. /* atofhz returns a float given a buffer containing a floating point value and a multiplier
  8.    in hz.
  9.  
  10.    ex: 18.45
  11.        18.45Hz
  12.        18.45KHz
  13.        18.45MHz
  14.  
  15.    This function does not require the use of the C library which should
  16.    lead to a reduction in size.
  17.  
  18.    Note this is no longer destructive to buffer
  19.  
  20.    Compliments of Brent York, happyhax0r@gmail.com
  21. */
  22.  
  23.  
  24.  
  25. float atofhz(char *buffer) {
  26.     float res = 0.0f;
  27.     char *tmp1 = buffer;
  28.     char multiplier = '\0';
  29.     char placement = 0;
  30.     char tmpplacement = 0;
  31.     char i;
  32.  
  33.  
  34.     /* Loop through the buffer, looking for a character that isn't
  35.          * a numeric or period, note it then short circuit the loop.
  36.          */
  37.     while (*tmp1) {
  38.  
  39.  
  40.         /* This replaces isalpha and toupper... */
  41.         if (((*tmp1 & ~UPCASE_BIT) >= CAP_A) && ((*tmp1 & ~UPCASE_BIT) <= CAP_Z)) {
  42.             multiplier = *tmp1 & ~UPCASE_BIT;
  43.  
  44.             /* This breaks out of the loop saving cycles and further letter testing */
  45.             break;
  46.         }
  47.  
  48.  
  49.         /* Move to the next char */
  50.         tmp1++;
  51.     }
  52.  
  53.  
  54.     /* Now we need to parse the floating point number...
  55.      * First thing we need to do is count the number of characters to the left of the decimal point
  56.      * to get our maximum placement
  57.      */
  58.     tmp1 = buffer;
  59.     placement = 0;
  60.     while (*tmp1 != '.') { placement++; tmp1++; };
  61.  
  62.  
  63.  
  64.     /* Now we start converting, note that for placements < 1 here we start multiplying     by 10? We'll divide this
  65.        back out later and it helps to avoid rounding. This allows us to avoid a call to pow() :) */
  66.     tmp1 = buffer;
  67.     while (*tmp1 != '\0') {
  68.         if ((*tmp1 >= CHR_0) && (*tmp1 <= CHR_9)) {
  69.  
  70.             if (placement < 1) {
  71.                 tmpplacement = 1;
  72.                 res *= 10.0;
  73.             } else {
  74.                 tmpplacement = placement;
  75.             }
  76.  
  77.             res += ((*tmp1 - CHR_0) * ipow(10, tmpplacement - 1));
  78.             placement --;
  79.         }
  80.  
  81.         tmp1++;
  82.     }
  83.  
  84.  
  85.     /* Finally we divide down for each negative placement from the previous
  86.          * multiplications above that gave us the ability to easily calculate pow
  87.          * in integer form. It also has a nice side effect, that is it allows us
  88.          * to avoid floating point rounding...
  89.      */
  90.     for (i = 0; i > placement; i--) { res /= 10.0; }
  91.  
  92.            
  93.  
  94.     /* And finally, we need to deal with the multiplier */
  95.     switch (multiplier) {
  96.         case 'M':
  97.             res *= 1000000.0;
  98.             break;
  99.         case 'K':
  100.             res *= 1000.0;
  101.             break;
  102.     }
  103.  
  104.  
  105.     /* return the value */
  106.     return ((res/50000000)*16777216);
  107. }
  108.  
  109.  
  110. /* ipow - an integer power function
  111.  *
  112.  * Compliments of Brent York, happyhax0r@gmail.com
  113.  */
  114. int ipow(int b, int x) {
  115.     int res = 1;                        /* Set res to 1, assuming x of 0, because anything to the power of zero is 1 */
  116.  
  117.  
  118.     /* So, if x is > 0 then we'll loop through until it's not zero, multiplying each  
  119.      * time by base and accumulating the result in res...
  120.      *
  121.      * The way this algorithm works is as follows:
  122.      *
  123.      * If the first bit of x is set we multiply the result by "base" (as the end value  
  124.      * contains a multiple of base for that bit), and shift the bits right by one. If  
  125.      * it's a 0, res doesn't get multiplied by base as it doesn't contain a base
  126.      * component for that bit. Each time through the loop, we multiply base by base...
  127.      * The end result is an additive accumulation of the proper number of multiples of
  128.      * base per bit, resulting in our exponentiated value.
  129.      */
  130.     while (x) {
  131.         if (x & 1) res *= b;
  132.         x >>= 1;
  133.         b *= b;
  134.     }
  135.  
  136.     return res;
  137. }
Add Comment
Please, Sign In to add comment