Guest User

Untitled

a guest
Dec 11th, 2017
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 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 nuke it with a null.
  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 have a string that's only 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 decimal values here we start multiplying by 10? We'll divide this
  65.        back out later. 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 round up to the same number of significant digits after the decimal point that we had
  86.      * (or as close as we can come given floating point math, note this isn't the best way to round but
  87.      *  it works and it's cheap on cycles and space)
  88.      */
  89.     for (i = 0; i > placement; i--) { res /= 10.0; }
  90.  
  91.            
  92.  
  93.     /* And finally, we need to deal with the multiplier */
  94.     switch (multiplier) {
  95.         case 'M':
  96.             res *= 1000000.0;
  97.             break;
  98.         case 'K':
  99.             res *= 1000.0;
  100.             break;
  101.     }
  102.  
  103.  
  104.     /* return the value */
  105.     return ((res/50000000)*16777216);
  106. }
  107.  
  108.  
  109. /* ipow - an integer power function
  110.  *
  111.  * Compliments of Brent York, happyhax0r@gmail.com
  112.  */
  113. int ipow(int b, int x) {
  114.     int res = 1;                        /* Set res to 1, assuming x of 0, because anything to the power of zero is 1 */
  115.  
  116.  
  117.     /* So, if x is > 0 then we'll loop through until it's not zero, multiplying each time by base and accumulating
  118.      * the result in res...
  119.      *
  120.      * The way this algorithm works is as follows:
  121.      *
  122.      * If the first bit of x is set we multiply the result by "base" (as the end value contains a multiple of base
  123.      * for that bit), and shift the bits right by one. If it's a 0, it doesn't get multiplied by base. Each time
  124.      * through the loop, we multiply base by base... The end result is an additive accumulation of the proper
  125.      * number of multiples of base per bit, resulting in our exponentiated value.
  126.      */
  127.     while (x) {
  128.         if (x & 1) res *= b;
  129.         x >>= 1;
  130.         b *= b;
  131.     }
  132.  
  133.     return res;
  134. }
Add Comment
Please, Sign In to add comment