OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [libc.c] - Diff between revs 171 and 187

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 171 Rev 187
Line 246... Line 246...
      negate = 1;
      negate = 1;
   }
   }
   text[16] = 0;
   text[16] = 0;
   for(place = 15; place >= 0; --place)
   for(place = 15; place >= 0; --place)
   {
   {
      if(base == 10)
 
         digit = num % base;
 
      else
 
         digit = (unsigned int)num % (unsigned int)base;
         digit = (unsigned int)num % (unsigned int)base;
      if(num == 0 && place < 15 && base == 10 && negate)
      if(num == 0 && place < 15 && base == 10 && negate)
      {
      {
         c = '-';
         c = '-';
         negate = 0;
         negate = 0;
Line 275... Line 272...
            int arg0, int arg1, int arg2, int arg3,
            int arg0, int arg1, int arg2, int arg3,
            int arg4, int arg5, int arg6, int arg7)
            int arg4, int arg5, int arg6, int arg7)
{
{
   int argv[8];
   int argv[8];
   int argc=0, width, length;
   int argc=0, width, length;
   char f, text[20];
   char f, text[20], fill;
 
 
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
 
 
   for(;;)
   for(;;)
Line 288... Line 285...
      if(f == 0)
      if(f == 0)
         return argc;
         return argc;
      else if(f == '%')
      else if(f == '%')
      {
      {
         width = 0;
         width = 0;
 
         fill = ' ';
         f = *format++;
         f = *format++;
         if(f == 0)
         while('0' <= f && f <= '9')
            return argc;
         {
         if('0' <= f && f <= '9')
            width = width * 10 + f - '0';
 
            f = *format++;
 
         }
 
         if(f == '.')
         {
         {
            width = f - '0';
            fill = '0';
            f = *format++;
            f = *format++;
 
         }
            if(f == 0)
            if(f == 0)
               return argc;
               return argc;
            if('0' <= f && f <= '9')
 
               width = width * 10 + f - '0';
 
         }
 
 
 
         if(f == 'd')
         if(f == 'd')
         {
         {
            memset(s, ' ', width);
            memset(s, fill, width);
            itoa(argv[argc++], text, 10);
            itoa(argv[argc++], text, 10);
            length = (int)strlen(text);
            length = (int)strlen(text);
            if(width < length)
            if(width < length)
               width = length;
               width = length;
            strcpy(s + width - length, text);
            strcpy(s + width - length, text);
Line 362... Line 361...
int sscanf(const char *s, const char *format,
int sscanf(const char *s, const char *format,
           int arg0, int arg1, int arg2, int arg3,
           int arg0, int arg1, int arg2, int arg3,
           int arg4, int arg5, int arg6, int arg7)
           int arg4, int arg5, int arg6, int arg7)
{
{
   int argv[8];
   int argv[8];
   int argc=0, length;
   int argc=0;
   char f;
   char f, *ptr;
 
 
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
 
 
   for(;;)
   for(;;)
Line 390... Line 389...
            *(int*)argv[argc++] = strtol(s, &s, 16);
            *(int*)argv[argc++] = strtol(s, &s, 16);
         else if(f == 'c')
         else if(f == 'c')
            *(char*)argv[argc++] = *s++;
            *(char*)argv[argc++] = *s++;
         else if(f == 's')
         else if(f == 's')
         {
         {
            length = 0;
            ptr = (char*)argv[argc];
            while(!isspace(s[length]))
            while(!isspace(*s))
               ++length;
               *ptr++ = *s++;
            strncpy((char*)argv[argc++], s, length);
            *ptr = 0;
            s += length;
 
         }
         }
      }
      }
      else
      else
      {
      {
         if(f == '\\')
         if(f == '\\')
Line 453... Line 451...
}
}
#endif //INCLUDE_DUMP
#endif //INCLUDE_DUMP
 
 
 
 
#ifdef INCLUDE_QSORT
#ifdef INCLUDE_QSORT
 
#define QSORT_SIZE 256
/*********************** qsort ***********************/
/*********************** qsort ***********************/
static void QsortSwap(char *base, long left, long right, long size)
static void QsortSwap(char *base, long left, long right, long size)
{
{
   char buffer[256];
   char buffer[QSORT_SIZE];
   if(size > sizeof(buffer))
 
   {
 
      printf("qsort_error");
 
      return;
 
   }
 
   memcpy(buffer, &base[left*size], size);
   memcpy(buffer, &base[left*size], size);
   memcpy(&base[left*size], &base[right*size], size);
   memcpy(&base[left*size], &base[right*size], size);
   memcpy(&base[right*size], buffer, size);
   memcpy(&base[right*size], buffer, size);
}
}
 
 
Line 473... Line 467...
//Modified from K&R
//Modified from K&R
static void qsort2(void *base, long left, long right, long size,
static void qsort2(void *base, long left, long right, long size,
      int (*cmp)(const void *,const void *))
      int (*cmp)(const void *,const void *))
{
{
   int i, last;
   int i, last;
   char *base2=(char*)base;
   char *base2=(char*)base, *pivot;
   if(left >= right)
   if(left >= right)
      return;
      return;
   QsortSwap(base2, left, (left + right)/2, size);
   QsortSwap(base2, left, (left + right)/2, size);
   last = left;
   last = left;
 
   pivot = &base2[left*size];
   for(i = left + 1; i <= right; ++i)
   for(i = left + 1; i <= right; ++i)
   {
   {
      if(cmp(&base2[i*size], &base2[left*size]) < 0)
      if(cmp(&base2[i*size], pivot) < 0)
         QsortSwap(base2, ++last, i, size);
         QsortSwap(base2, ++last, i, size);
   }
   }
   QsortSwap(base2, left, last, size);
   QsortSwap(base2, left, last, size);
   qsort2(base, left, last-1, size, cmp);
   qsort2(base, left, last-1, size, cmp);
   qsort2(base, last+1, right, size, cmp);
   qsort2(base, last+1, right, size, cmp);
Line 494... Line 489...
void qsort(void *base,
void qsort(void *base,
           long n,
           long n,
           long size,
           long size,
           int (*cmp)(const void *,const void *))
           int (*cmp)(const void *,const void *))
{
{
 
   if(size > QSORT_SIZE)
 
   {
 
      printf("qsort_error");
 
      return;
 
   }
   qsort2(base, 0, n-1, size, cmp);
   qsort2(base, 0, n-1, size, cmp);
}
}
 
 
 
 
void *bsearch(const void *key,
void *bsearch(const void *key,
Line 517... Line 517...
      else if(cond > 0)
      else if(cond > 0)
         low = mid + 1;
         low = mid + 1;
      else
      else
         return &base2[mid * size];
         return &base2[mid * size];
   }
   }
   return(NULL);
   return NULL;
}
}
#endif //INCLUDE_QSORT
#endif //INCLUDE_QSORT
 
 
 
 
#ifdef INCLUDE_TIMELIB
#ifdef INCLUDE_TIMELIB
Line 537... Line 537...
//   int tm_sec;      //(0,59)
//   int tm_sec;      //(0,59)
//   int tm_min;      //(0,59)
//   int tm_min;      //(0,59)
//   int tm_hour;     //(0,23)
//   int tm_hour;     //(0,23)
//   int tm_mday;     //(1,31)
//   int tm_mday;     //(1,31)
//   int tm_mon;      //(0,11)
//   int tm_mon;      //(0,11)
//   int tm_year;     //(0,n) from 1990
//   int tm_year;     //(0,n) from 1900
//   int tm_wday;     //(0,6)     calculated
//   int tm_wday;     //(0,6)     calculated
//   int tm_yday;     //(0,365)   calculated
//   int tm_yday;     //(0,365)   calculated
//   int tm_isdst;    //          calculated
//   int tm_isdst;    //          calculated
//};
//};
static const unsigned short DaysUntilMonth[]=
static const unsigned short DaysUntilMonth[]=
   {0,31,59,90,120,151,181,212,243,273,304,334,365};
   {0,31,59,90,120,151,181,212,243,273,304,334,365};
static const unsigned short DaysInMonth[]=
static const unsigned short DaysInMonth[]=
   {31,28,31,30,31,30,31,31,30,31,30,31};
   {31,28,31,30,31,30,31,31,30,31,30,31};
 
static time_t DstTimeIn, DstTimeOut;
 
 
static int IsLeapYear(int year)
static int IsLeapYear(int year)
{
{
   return(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
   return(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
}
Line 570... Line 571...
         seconds += SEC_PER_DAY;
         seconds += SEC_PER_DAY;
   }
   }
   return seconds;
   return seconds;
}
}
 
 
 
 
void gmtime_r(const time_t *tp, struct tm *out)
void gmtime_r(const time_t *tp, struct tm *out)
{
{
   time_t seconds, delta;
   time_t seconds, delta, secondsIn=*tp;
   int wday, isLeapYear;
   int isLeapYear;
   unsigned long year, month;
   unsigned long year, month;
 
 
   seconds = *tp;
   out->tm_isdst = 0;
 
   if(DstTimeIn <= secondsIn && secondsIn < DstTimeOut)
 
   {
 
      secondsIn -= 60 * 60;
 
      out->tm_isdst = 1;
 
   }
 
   seconds = secondsIn;
   for(year = 0; ; ++year)
   for(year = 0; ; ++year)
   {
   {
      delta = SEC_PER_YEAR + IsLeapYear(1980 + year) * SEC_PER_DAY;
      delta = SEC_PER_YEAR + IsLeapYear(1980 + year) * SEC_PER_DAY;
      if(seconds >= delta)
      if(seconds >= delta)
         seconds -= delta;
         seconds -= delta;
      else
      else
         break;
         break;
   }
   }
   out->tm_year = year;
   out->tm_year = year + 80;
   out->tm_yday = seconds / SEC_PER_DAY;
 
   isLeapYear = IsLeapYear(1980 + year);
   isLeapYear = IsLeapYear(1980 + year);
   for(month = 0; ; ++month)
   for(month = 0; ; ++month)
   {
   {
      delta = SEC_PER_DAY * (DaysInMonth[month] + (isLeapYear && (month == 1)));
      delta = SEC_PER_DAY * (DaysInMonth[month] + (isLeapYear && (month == 1)));
      if(seconds >= delta)
      if(seconds >= delta)
Line 598... Line 605...
      else
      else
         break;
         break;
   }
   }
   out->tm_mon = month;
   out->tm_mon = month;
   out->tm_mday = seconds / SEC_PER_DAY;
   out->tm_mday = seconds / SEC_PER_DAY;
 
   out->tm_yday = DaysUntilMonth[month] + out->tm_mday;
   seconds -= out->tm_mday * SEC_PER_DAY;
   seconds -= out->tm_mday * SEC_PER_DAY;
 
   ++out->tm_mday;
   out->tm_hour = seconds / (60 * 60);
   out->tm_hour = seconds / (60 * 60);
   seconds -= out->tm_hour * (60 * 60);
   seconds -= out->tm_hour * (60 * 60);
   out->tm_min = seconds / 60;
   out->tm_min = seconds / 60;
   seconds -= out->tm_min * 60;
   seconds -= out->tm_min * 60;
   out->tm_sec = seconds;
   out->tm_sec = seconds;
   seconds = *tp % (SEC_PER_DAY * 7);
   seconds = secondsIn % (SEC_PER_DAY * 7);
   out->tm_wday = seconds / SEC_PER_DAY;
   out->tm_wday = (seconds / SEC_PER_DAY + 2) % 7; /* 1/1/80 is a Tue */
   out->tm_wday = (out->tm_wday + 2) % 7;   /* 1/1/80 is a Tue */
   //printf("%4.d/%2.d/%2.d:%2.d:%2.d:%2.d\n", 
 
   //         out->tm_year+1900, out->tm_mon+1, out->tm_mday,
 
   //         out->tm_hour, out->tm_min, out->tm_sec);
 
}
 
 
 
 
 
void gmtimeDst(time_t dstTimeIn, time_t dstTimeOut)
 
{
   /*DST from first Sunday in April to last Sunday in October at 2am*/
   /*DST from first Sunday in April to last Sunday in October at 2am*/
   out->tm_isdst = 0;
   DstTimeIn = dstTimeIn;
   wday = (out->tm_mday % 7) + out->tm_wday;    /* wday of the 1st */
   DstTimeOut = dstTimeOut;
   if(out->tm_mon > 3 || (out->tm_mon == 3 && (wday == 0 || out->tm_wday + wday > 6)))
 
      out->tm_isdst = 1;
 
   if(out->tm_mon > 9 || (out->tm_mon == 9 && (out->tm_mday - wday == 21 ||
 
         out->tm_mday + wday == 34)))
 
      out->tm_isdst = 0;
 
   ++out->tm_mday;
 
   out->tm_year += 80;
 
}
}
#endif //INCLUDE_TIMELIB
#endif //INCLUDE_TIMELIB
 
 
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.