Skip to content
Snippets Groups Projects
Commit 07513581 authored by David Lanzendörfer's avatar David Lanzendörfer
Browse files

Adding functions as separate files

parent b0d6979c
No related branches found
No related tags found
No related merge requests found
Showing with 2331 additions and 41 deletions
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <ctype.h>
#include <stdlib.h>
int atoi(const char *str)
{
int acc = 0;
for(; str && isdigit(*str); ++str) {
acc *= 10;
acc += *str - 0x30;
}
return acc;
}
......@@ -17,13 +17,18 @@
*
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "rnn.h"
#include "io.h"
#include "string.h"
#define MEM_TOTAL 0x20000
#define MSGBUF 40
#define MAX_NUM_TOKENS 20
#define MAX_NUM_TOKENS 40
#define LRFACTOR 2000000000
// a pointer to this is a null pointer, but the compiler does not
// know that because "sram" is a linker symbol from sections.lds.
......@@ -71,6 +76,7 @@ void main()
// Training the network
TRAIN,
TRAIN_STORE_TOKENS,
TRAIN_STORE_LEARNING_RATE,
TRAIN_PROCESS
} command_mode;
command_mode = START;
......@@ -179,20 +185,20 @@ void main()
}
else if(!strcmp(msg,"BIAS")) {
weight_transferred = get_layer_weight(current_layer, LAYER_VALUE_TYPE_BIAS, neuron_idx, 0);
response = citoa(weight_transferred, weight_string,10);
response = itoa(weight_transferred, numstr, 10);
}
else {
neuron_idx = atoi(numstr,10);
neuron_idx = atoi(numstr);
response = (neuron_idx<current_num_neurons)?msg:"END";
}
break;
case READ_LAYER_WEIGHTS:
weight_idx = atoi(numstr,10);
weight_idx = atoi(numstr);
response = "END";
if(weight_idx<current_max_num_values) {
weight_transferred = get_layer_weight(current_layer, current_layer_value, neuron_idx, weight_idx);
response = citoa(weight_transferred, weight_string,10);
response = itoa(weight_transferred, numstr, 10);
}
break;
......@@ -234,7 +240,7 @@ void main()
value_write_counter = 0;
}
else {
neuron_idx = atoi(numstr,10);
neuron_idx = atoi(numstr);
response = (neuron_idx<current_num_neurons)?msg:"END";
value_write_counter = 0;
}
......@@ -244,7 +250,7 @@ void main()
response = "END";
if(value_write_counter<current_max_num_values) {
response = numstr;
new_value = atoi(numstr,10);
new_value = atoi(numstr);
set_layer_weight(current_layer, current_layer_value, neuron_idx, value_write_counter, new_value);
value_write_counter++;
}
......@@ -256,15 +262,19 @@ void main()
response = "OK";
token_counter = 0;
}
else if(!strcmp(msg,"RUN")) {
command_mode = TRAIN_PROCESS;
else if(!strcmp(msg,"LEARNING_RATE")) {
response = "OK";
command_mode = TRAIN_STORE_LEARNING_RATE;
}
/*else if(!strcmp(msg,"RUN")) {
command_mode = TRAIN_PROCESS;
response = "OK";
}*/
break;
case TRAIN_STORE_TOKENS:
if(token_counter<MAX_NUM_TOKENS) {
new_token = atoi(numstr,10);
new_token = atoi(numstr);
token_series[token_counter] = new_token;
token_counter++;
response = "OK";
......@@ -273,6 +283,11 @@ void main()
}
break;
case TRAIN_STORE_LEARNING_RATE:
new_token = atoi(numstr);
response = "OK";
break;
}
write_response(response);
}
......
......@@ -7,17 +7,20 @@
#ifndef MINLIBC_STDLIB_H
#define MINLIBC_STDLIB_H
#define NULL 0
//#define NULL 0
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
typedef unsigned long int size_t;
//typedef unsigned long int size_t;
#include <stddef.h>
double atof(const char *nptr);
int atoi(const char *nptr);
long int strtol(const char *nptr, char **endptr, int base);
unsigned long long strtoull (const char *__restrict, char **__restrict, int);
char * itoa ( int value, char * str, int base );
char *getenv(const char *name);
void abort(void) __attribute__((noreturn));
void exit(int status) __attribute__((noreturn));
......
#include <stdint.h>
#include <stdbool.h>
/*
* Implementation of citoa()
*
* Converts a string into a signed integer
*/
int atoi(char* str, int base);
/*
* Converts an integer into a string
*/
char* citoa(int num, char* str, int base);
/*
* Compare strings
* 0: if strings are equal
* 0: if the first non-matching character in str1 is greater (in ASCII) than that of str2.
* 0: if the first non-matching character in str1 is lower (in ASCII) than that of str2.
*/
int strcmp(const char* s1, const char* s2);
/*
* A small implementation of strcpy
*/
void strcpy(char *s2, char *s1);
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#ifndef MINLIBC_STRING_H
#define MINLIBC_STRING_H
//#include <sys/types.h>
#include <stddef.h>
size_t strlen(const char *s);
size_t strnlen(const char *s, size_t maxlen);
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
int strcmp(const char *s1, const char *s2);
char *strdup(const char *s);
int strncmp(const char *s1, const char *s2, size_t n);
char *strrchr(const char *s, int c);
char *strerror(int errnum);
char *strstr(const char *, const char *);
char *strchr(const char *, int c);
void *memcpy(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
void *memmove(void *dest, const void *src, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
void *memchr(const void *s, int c, size_t n);
#endif
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <ctype.h>
int isdigit(int c)
{
return ((c >= '0') && (c <= '9'));
}
#include "string.h"
/*
* A small implementation of strcpy
*/
void strcpy(char *s2, char *s1)
{
while(*s1)
{
*s2 = *s1;
s1++;
s2++;
}
}
// C program to implement itoa()
#include <stdbool.h>
#include <stdio.h>
// A utility function to reverse a string
void reverse(char str[], int length)
{
......@@ -28,44 +16,7 @@ void reverse(char str[], int length)
}
}
/*
* Implementation of citoa()
*
* Converts a string into a signed integer
*/
int atoi(char* str, int base)
{
int result = 0;
int sign = 1;
int i = 0;
// Check for leading sign character
if (str[0] == '-') {
sign = -1;
i++;
}
else if (str[0] == '+') {
i++;
}
// Convert digits to integer value
while (str[i] != '\0') {
if (str[i] < '0' || str[i] > (base+'0') ) {
break;
}
result = result * base + (str[i] - '0');
i++;
}
return sign * result;
}
/*
* Implementation of citoa()
*
* Converts an integer into a string
*/
char* citoa(int num, char* str, int base)
char * itoa ( int num, char * str, int base )
{
int i = 0;
bool isNegative = false;
......@@ -104,20 +55,3 @@ char* citoa(int num, char* str, int base)
return str;
}
/*
* Compare strings
* 0: if strings are equal
* 0: if the first non-matching character in str1 is greater (in ASCII) than that of str2.
* 0: if the first non-matching character in str1 is lower (in ASCII) than that of str2.
*/
int strcmp(const char* s1, const char* s2)
{
while(*s1 && (*s1 == *s2))
{
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
This diff is collapsed.
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
#include <stdlib.h>
void *memchr(const void *s, int c, size_t n)
{
size_t i;
for(i = 0; i < n; i++)
if(((unsigned char*)s)[i] == (unsigned char)c)
return (void*)((unsigned long)s + i);
return NULL;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *str1 = (void*)s1;
unsigned char *str2 = (void*)s2;
size_t pos;
for(pos = 0; pos < n; pos++) {
if(str1[pos] < str2[pos])
return 1;
if(str1[pos] > str2[pos])
return -1;
}
return 0;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
void *memcpy(void *dest, const void *src, size_t count)
{
/* This would be a prime candidate for reimplementation in assembly */
char *in_src = (char*)src;
char *in_dest = (char*)dest;
while(count--)
*in_dest++ = *in_src++;
return dest;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
void *memmove(void *dest, const void *src, size_t n)
{
void *d0 = dest;
char *d = (char *) dest;
char *s = (char *) src;
if (s < d)
for (s += n, d += n; 0 != n; --n)
*--d = *--s;
else if (s != d)
for (; 0 != n; --n)
*d++ = *s++;
return d0;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
void *memset(void *s,int c, size_t count)
{
char *xs = (char *) s;
while (count--)
*xs++ = c;
return s;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <ctype.h>
#include <strings.h>
int strcasecmp(const char *s1, const char *s2)
{
int result;
while (1) {
result = tolower(*s1) - tolower(*s2);
if (result != 0 || *s1 == '\0')
break;
++s1;
++s2;
}
return result;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
#include <stdlib.h>
char *strchr(const char *s, int c)
{
const char *cur;
for (cur = s; *cur; cur++) {
if (*cur == c) {
return ((char*)cur);
}
}
return NULL;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
int strcmp(const char *cs, const char *ct)
{
register signed char __res;
while (1) {
if ((__res = *cs - *ct++) != 0 || !*cs++)
break;
}
return __res;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <stdlib.h>
#include <string.h>
char *strcpy(char *dest, const char *src)
{
char *retval = dest;
// FIXME: Make higher-speed version?
while(*src) *dest++ = *src++;
return retval;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
#include <stdlib.h>
char *strdup(const char *s)
{
size_t bufsize = strlen(s) + 1;
char *retval = malloc(bufsize);
if(retval) memcpy(retval, s, bufsize);
return retval;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <errno.h>
#include <string.h>
#include <stdio.h>
char *strerror(int errnum)
{
static char buf[1024];
switch(errnum) {
case EBADF: return "Bad file.\n";
case EACCES: return "Access prohibited.\n";
}
sprintf(buf, "Unkown error: %d", errnum);
return buf;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
size_t strlen(const char *s)
{
const char *sc;
for (sc = s; *sc != '\0'; ++sc)
/* nothing */;
return sc - s;
}
/*
* Copyright (C) 2014, Galois, Inc.
* This sotware is distributed under a standard, three-clause BSD license.
* Please see the file LICENSE, distributed with this software, for specific
* terms and conditions.
*/
#include <string.h>
int strncmp(const char *str1, const char *str2, size_t count)
{
register signed char __res = 0;
while(count--) {
if ((__res = *str1 - *str2) != 0 || !*str1++ || !*str2++)
break;
}
return __res;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment