File: util.c

package info (click to toggle)
libmems 1.6.0%2B4725-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,148 kB
  • sloc: cpp: 21,579; ansic: 4,313; xml: 115; makefile: 107; sh: 26
file content (133 lines) | stat: -rw-r--r-- 2,991 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include "libMems/dmSML/util.h"

#define FMT_BUFFER_SIZE     (32)
#define FMT_MAX_STRING      (1024)

static char FmtBuffer[FMT_BUFFER_SIZE][FMT_MAX_STRING];
static int FmtIdx;

const char * Fmt( const char * fmt, ... ) {
    const char * ret;
    va_list args;
    va_start( args, fmt );
    ret = VFmt( fmt, args );
    va_end( args );
    return( ret );
}


const char * VFmt( const char * fmt, va_list args ) {
    if( ++FmtIdx >= FMT_BUFFER_SIZE ) {
        FmtIdx = 0;
    }
    // silly windows....
#ifdef WIN32
    _vsnprintf( FmtBuffer[FmtIdx], sizeof( FmtBuffer[FmtIdx] ), fmt, args );
#else
    vsnprintf( FmtBuffer[FmtIdx], sizeof( FmtBuffer[FmtIdx] ), fmt, args );
#endif
    FmtBuffer[FmtIdx][FMT_MAX_STRING-1] = '\0';
    return( FmtBuffer[FmtIdx] );
}


/// shifts a 64-bit value (in two 32 bit parts) either right or left.
/// amt negative -> left, positive -> right
void Shift64( int amt, int * hi, int * lo ) {
    if( amt == 0 ) {
        return;
    }
    if( amt > 0 ) {
        *lo >>= amt;
        *lo |= *hi << ((sizeof( *hi ) * 8) - amt);
        *hi >>= amt;
    } else {
        amt = -amt;
        *hi <<= amt;
        *hi |= *lo >> ((sizeof( *lo ) * 8) - amt);
        *lo <<= amt;
    }
}




void AddTo64( unsigned int amt, unsigned int *hi, unsigned int *lo ) {

    int i;
    // holds each byte value.
    int in[8], out[8], tmp[8];
    int carry;

    for( i = 0; i < 8; i++ ) {
        in[i] = out[i] = tmp[i] = 0;
    }

    in[0] = amt & 0xFF;
    in[1] = (amt >> 8) & 0xFF;
    in[2] = (amt >> 16) & 0xFF;
    in[3] = (amt >> 24) & 0xFF;

    tmp[0] = *lo & 0xFF;
    tmp[1] = (*lo >> 8) & 0xFF;
    tmp[2] = (*lo >> 16) & 0xFF;
    tmp[3] = (*lo >> 24) & 0xFF;
    tmp[4] = *hi & 0xFF;
    tmp[5] = (*hi >> 8) & 0xFF;
    tmp[6] = (*hi >> 16) & 0xFF;
    tmp[7] = (*hi >> 24) & 0xFF;
    

    /*
    out[0] = (tmp[0] + in[0]);
    carry = out[0] >> 8;
    out[0] &= 0xFF;

    out[1] = (tmp[1] + in[1] + carry);
    carry = out[1] >> 8;
    out[1] &= 0xFF;

    out[2] = (tmp[2] + in[2] + carry);
    carry = out[2] >> 8;
    out[2] &= 0xFF;

    out[3] = (tmp[3] + in[3] + carry);
    carry = out[3] >> 8;
    out[3] &= 0xFF;
    */

    carry = 0;
    for( i = 0; i < 8; i++ ) {
        out[i] = in[i] + tmp[i] + carry;
        carry = out[i] >> 8;
        out[i] &= 0xFF;
    }

    // convert back to 2-int form.
    *lo = out[0] + (out[1] << 8) + (out[2] << 16) + (out[3] << 24);
    *hi = out[4] + (out[5] << 8) + (out[6] << 16) + (out[7] << 24);

}

/** Utility function to delete a file */
int removeFile( const char* filename, int verbose )
{
#ifdef WIN32
		return remove( filename );
//        return !DeleteFile( filename );
#else
        char* rm_cmd;
        if( verbose )
                rm_cmd = Fmt( "/bin/rm -fv %s", filename );
        else
                rm_cmd = Fmt( "/bin/rm -f %s", filename );
        return system( rm_cmd );
#endif
}