summaryrefslogtreecommitdiffstats
path: root/baseline/source/ammunition/ammunition_libc.c
blob: 4cce6e9c9b1b3b82c1773bc222ffb115f908f2fb (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
  Include section
*/

#include "ammunition_string.h"
#include "ammunition_stdio.h"
#include "ammunition_stdlib.h"


/*
  Standard library functions
*/

char ammunition_isdigit( unsigned char c )
{
  if ( ( c >= '0' ) & ( c <= '9' ) )
    return 1;
  else
    return 0;
}

int ammunition_isspace( int c )
{
  return ( c == ' ' ) | ( c == '\t' ) | ( c == '\n' ) | ( c == '\r' );
}

void *ammunition_memcpy( void *dest, const void *src, size_x size )
{
  size_x i;
  _Pragma( "loopbound min 2 max 6" )
  for ( i = 0; i < size; i++ )
    ( ( unsigned char * )dest )[i] = ( ( unsigned char * )src )[i];

  return dest;
}


void *ammunition_memset( void *s, int c, size_x n )
{
  size_x i;
  _Pragma( "loopbound min 0 max 4" )
  for ( i = 0; i < n; i++ )
    ( ( unsigned char * )s )[i] = ( unsigned char )c;

  return s;
}


int ammunition_memcmp ( const void *mem1, const void *mem2, size_x size )
{
  const unsigned char *p1 = (const unsigned char *) mem1,
                      *p2 = (const unsigned char *) mem2;
  _Pragma( "loopbound min 0 max 4" )
  while ( size-- )
    if ( *p1 != *p2 )
      return ( *p1 - *p2 );
    else
      p1++, p2++;
  return 0;
}


/* The following function is an analog of standard C function
   `memmove'.  The function returns the first operand. */

void *ammunition_memmove ( void *s1, const void *s2, size_x n )
{
  int i;

  if ( ( ( char * ) s1 < ( char * ) s2 && ( char * ) s1 + n <= ( char * ) s2 )
       || ( ( char * ) s2 < ( char * ) s1
            && ( char * ) s2 + n <= ( char * ) s1 ) )
    return ( void * ) ammunition_memcpy ( s1, s2, n );
  if ( ( char * ) s1 < ( char * ) s2 && ( char * ) s1 + n > ( char * ) s2 ) {
    _Pragma( "loopbound min 0 max 4" )
    for ( i = 0; ( size_x ) i < n; i++ )
      ( ( char * ) s1 ) [i] = ( ( char * ) s2 ) [i];
  } else {
    _Pragma( "loopbound min 0 max 4" )
    for ( i = n - 1; i >= 0; i-- )
      ( ( char * ) s1 )[i] = ( ( char * ) s2 ) [i];
  }
  return s1;
}

int ammunition_strcmp ( const char *str1, const char *str2 )
{
  _Pragma( "loopbound min 1 max 4008" )
  while ( *str1 && ( *str1 == *str2 ) )
    str1++, str2++;
  return *( const unsigned char * )str1 - *( const unsigned char * )str2;
}

int ammunition_atoi ( const char *str )
{
  int result = 0;
  int sign = ( str[0] == '-' ? -1 : 1 );

  int readingPos = 0;
  if ( str[0] == '-' || str[0] == '+' )
    readingPos++;
  _Pragma( "loopbound min 1 max 1" )
  do {
    result *= 10;
    result += str[readingPos++] - 48;
  } while ( str[readingPos] != 0 );

  return sign * result;
}


int ammunition_sprintf_d( char *s, int number )
{
  /* How many decimal digits do we need? */
  char digits = 0;
  unsigned char writePos = 0;
  long long copyOfNumber = number;
  _Pragma( "loopbound min 1 max 10" )
  do {
    digits++;
    copyOfNumber /= 10;
  } while ( copyOfNumber != 0 );

  writePos = digits;
  if ( number < 0 ) {
    writePos++;
    s[0] = '-';
  }
  s[writePos] = 0;

  copyOfNumber = number;
  _Pragma( "loopbound min 1 max 10" )
  do {
    s[--writePos] = 48 + ( ( copyOfNumber >= 0 ?
                             copyOfNumber : -copyOfNumber ) % 10 );
    copyOfNumber /= 10;
  } while ( copyOfNumber != 0 );

  return digits + ( number < 0 ? 1 : 0 );
}


int ammunition_sprintf_u( char *s, unsigned int number )
{
  /* How many decimal digits do we need? */
  char digits = 0;
  unsigned char writePos = 0;
  unsigned long copyOfNumber = number;
  _Pragma( "loopbound min 1 max 10" )
  do {
    digits++;
    copyOfNumber /= 10;
  } while ( copyOfNumber != 0 );

  writePos = digits;
  s[writePos] = 0;

  copyOfNumber = number;
  _Pragma( "loopbound min 1 max 10" )
  do {
    s[--writePos] = 48 + ( copyOfNumber % 10 );
    copyOfNumber /= 10;
  } while ( copyOfNumber != 0 );

  return digits;
}