~ubuntu-branches/ubuntu/wily/ruby-ferret/wily

« back to all changes in this revision

Viewing changes to ext/helper.c

  • Committer: Bazaar Package Importer
  • Author(s): Antonio Terceiro
  • Date: 2011-07-28 00:02:49 UTC
  • Revision ID: james.westby@ubuntu.com-20110728000249-v0443y69ftcpxwi6
Tags: upstream-0.11.6
ImportĀ upstreamĀ versionĀ 0.11.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "helper.h"
 
2
 
 
3
int hlp_string_diff(register const char *const s1,
 
4
                           register const char *const s2)
 
5
{
 
6
    register int i = 0;
 
7
    while (s1[i] && (s1[i] == s2[i])) {
 
8
        i++;
 
9
    }
 
10
    return i;
 
11
}
 
12
 
 
13
f_i32 float2int(float f)
 
14
{
 
15
    union { f_i32 i; float f; } tmp;
 
16
    tmp.f = f;
 
17
    return tmp.i;
 
18
}
 
19
 
 
20
float int2float(f_i32 i32)
 
21
{
 
22
    union { f_i32 i; float f; } tmp;
 
23
    tmp.i = i32;
 
24
    return tmp.f;
 
25
}
 
26
 
 
27
float byte2float(unsigned char b)
 
28
{
 
29
    if (b == 0) {
 
30
        return 0.0;
 
31
    }
 
32
    else {
 
33
        f_u32 mantissa = b & 0x07;
 
34
        f_u32 exponent = (b >> 3) & 0x1f;
 
35
 
 
36
        return int2float((mantissa << 21) | ((exponent + 48) << 24));
 
37
    }
 
38
}
 
39
 
 
40
unsigned char float2byte(float f)
 
41
{
 
42
    if (f <= 0.0) {
 
43
        return 0;
 
44
    }
 
45
    else {
 
46
        /* correctly order the bytes for encoding */
 
47
        f_u32 i32 = float2int(f);
 
48
        int mantissa = (i32 & 0xEf0000) >> 21;
 
49
        int exponent = ((i32 >> 24) - 48);
 
50
 
 
51
        if (exponent > 0x1f) {
 
52
            exponent = 0x1f;   /* 0x1f = 31 = 0b00011111 */
 
53
            mantissa = 0x07;   /* 0x07 =  7 = 0b00000111 */
 
54
        }
 
55
 
 
56
        if (exponent < 0) {
 
57
            exponent = 0;
 
58
            mantissa = 1;
 
59
        }
 
60
        return ((exponent<<3) | mantissa);
 
61
    }
 
62
}