diff options
Diffstat (limited to 'x68')
-rw-r--r-- | x68/_dtos18.c | 250 | ||||
-rw-r--r-- | x68/_round.c | 45 | ||||
-rw-r--r-- | x68/fconvert.c | 81 | ||||
-rw-r--r-- | x68/select.c | 167 |
4 files changed, 543 insertions, 0 deletions
diff --git a/x68/_dtos18.c b/x68/_dtos18.c new file mode 100644 index 0000000000..67656486ec --- /dev/null +++ b/x68/_dtos18.c @@ -0,0 +1,250 @@ +/* + * PROJECT C Library, X68000 PROGRAMMING INTERFACE DEFINITION + * -------------------------------------------------------------------- + * This file is written by the Project C Library Group, and completely + * in public domain. You can freely use, copy, modify, and redistribute + * the whole contents, without this notice. + * -------------------------------------------------------------------- + * $Id: _dtos18.c,v 1.2 1994/11/27 13:05:20 mura Exp $ + */ + +/* System headers */ +#include <stdlib.h> +#include <sys/xstdlib.h> + +/* +** 本関数は浮動小数点を倍精度整数に変換してから文字列にするため、精度的には +** 倍精度整数に格納できる桁数までしか扱うことができない。したがって最高精度 +** は18桁である。 +*/ + +/* File scope variables */ +static double _pos1[32] = { + 1.0e+17, /* + 0 */ + 1.0e+18, /* + 1 */ + 1.0e+19, /* + 2 */ + 1.0e+20, /* + 3 */ + 1.0e+21, /* + 4 */ + 1.0e+22, /* + 5 */ + 1.0e+23, /* + 6 */ + 1.0e+24, /* + 7 */ + 1.0e+25, /* + 8 */ + 1.0e+26, /* + 9 */ + 1.0e+27, /* +10 */ + 1.0e+28, /* +11 */ + 1.0e+29, /* +12 */ + 1.0e+30, /* +13 */ + 1.0e+31, /* +14 */ + 1.0e+32, /* +15 */ + 1.0e+33, /* +16 */ + 1.0e+34, /* +17 */ + 1.0e+35, /* +18 */ + 1.0e+36, /* +19 */ + 1.0e+37, /* +20 */ + 1.0e+38, /* +21 */ + 1.0e+39, /* +22 */ + 1.0e+40, /* +23 */ + 1.0e+41, /* +24 */ + 1.0e+42, /* +25 */ + 1.0e+43, /* +26 */ + 1.0e+44, /* +27 */ + 1.0e+45, /* +28 */ + 1.0e+46, /* +29 */ + 1.0e+47, /* +30 */ + 1.0e+48, /* +31 */ +}; + +/* File scope variables */ +static double _neg1[32] = { + 1.0e+17, /* - 0 */ + 1.0e+16, /* - 1 */ + 1.0e+15, /* - 2 */ + 1.0e+14, /* - 3 */ + 1.0e+13, /* - 4 */ + 1.0e+12, /* - 5 */ + 1.0e+11, /* - 6 */ + 1.0e+10, /* - 7 */ + 1.0e+9, /* - 8 */ + 1.0e+8, /* - 9 */ + 1.0e+7, /* -10 */ + 1.0e+6, /* -11 */ + 1.0e+5, /* -12 */ + 1.0e+4, /* -13 */ + 1.0e+3, /* -14 */ + 1.0e+2, /* -15 */ + 1.0e+1, /* -16 */ + 1.0e+0, /* -17 */ + 1.0e-1, /* -18 */ + 1.0e-2, /* -19 */ + 1.0e-3, /* -20 */ + 1.0e-4, /* -21 */ + 1.0e-5, /* -22 */ + 1.0e-6, /* -23 */ + 1.0e-7, /* -24 */ + 1.0e-8, /* -25 */ + 1.0e-9, /* -26 */ + 1.0e-10, /* -27 */ + 1.0e-11, /* -28 */ + 1.0e-12, /* -29 */ + 1.0e-13, /* -30 */ + 1.0e-14, /* -31 */ +}; + +/* File scope variables */ +static double _pos2[10] = { + 1.0e+0, /* 000 */ + 1.0e+32, /* 001 */ + 1.0e+64, /* 010 */ + 1.0e+96, /* 011 */ + 1.0e+128, /* 100 */ + 1.0e+160, /* 101 */ + 1.0e+192, /* 110 */ + 1.0e+224, /* 111 */ + 1.0e+256, /* 1000 */ + 1.0e+288, /* 1001 */ +}; + +/* File scope variables */ +static double _neg2[10] = { + 1.0e-0, /* 000 */ + 1.0e-32, /* 001 */ + 1.0e-64, /* 010 */ + 1.0e-96, /* 011 */ + 1.0e-128, /* 100 */ + 1.0e-160, /* 101 */ + 1.0e-192, /* 110 */ + 1.0e-224, /* 111 */ + 1.0e-256, /* 1000 */ + 1.0e-288, /* 1001 */ +}; + +/* File scope functions */ +static int _cmpd (double x, double y) +{ + unsigned long vx, vy, rc; + unsigned long *x_ptr = (unsigned long *) &x; + unsigned long *y_ptr = (unsigned long *) &y; + + /* xの指数ビットを取り出す */ + vx = x_ptr[0] & 0x7FF00000; + + /* yの指数ビットを取り出す */ + vy = y_ptr[0] & 0x7FF00000; + + /* 指数ビットだけで判断する */ + if ((rc = vy - vx) != 0) + return rc; + + /* xの有効数字の上位ビットを取り出す */ + vx = x_ptr[0] & 0x000FFFFF; + + /* yの有効数字の上位ビットを取り出す */ + vy = y_ptr[0] & 0x000FFFFF; + + /* 上位ビットだけで判断する */ + if ((rc = vy - vx) != 0) + return rc; + + /* xの有効数字の下位ビットを取り出す */ + vx = x_ptr[1]; + + /* yの有効数字の下位ビットを取り出す */ + vy = y_ptr[1]; + + /* 最終判断 */ + return vy - vx; +} + +/* Functions */ +void _dtos18 (double x, int *decpt, int *sign, char *buffer) +{ + short e2; + int e, n; + + /* 基数2の指数を求める(バイアスなしの状態) */ + e2 = (((unsigned short *) &x)[0] & 0x7FF0U) >> 4; + + /* 指数が0の場合は±0.0チェック */ + if (e2 == 0) { + + unsigned long hi = ((unsigned long *) &x)[0] & 0xFFFFF; + unsigned long lo = ((unsigned long *) &x)[1]; + + /* 有効数字が全部0かどうか */ + if (hi == 0 && lo == 0) { + + /* 文字列を設定 */ + buffer[0] = '0'; + + /* NULを設定 */ + buffer[1] = '\0'; + + /* 小数点位置を計算 */ + *decpt = 1; + + /* 符号を計算 */ + /* *sign = hi & 0x80000000UL; */ + *sign = 0; + + /* 確定 */ + return; + + } + + } + + /* 2の指数にバイアスをかけてから10の指数を概算 (approx. log10(2)) */ + e = ((int) ((e2 - 1023) * 77)) >> 8; + + /* 指数が正の場合 */ + if (e >= 0) { + + /* 指数が32より小さい場合はテーブル1から */ + if (e < 32) + x *= _neg1[e]; + + /* 指数が32より大きい場合はテーブル1,2から */ + else + x *= _neg1[e & 31] * _neg2[e >> 5]; + + } + + /* 指数が負の場合 */ + else { + + /* 絶対値を計算 */ + n = -e; + + /* 絶対値が32より小さい場合はテーブル1から */ + if (n < 32) + x *= _pos1[n]; + + /* 絶対値が32より大きい場合はテーブル1,2から */ + else { + x *= _pos1[n & 31]; + x *= _pos2[n >> 5]; + } + + } + + /* スケーリングしすぎた場合は戻す */ + if (_cmpd (1.0e+18, x) >= 0) { + e++; + x *= 1.0e-1; + } + + /* スケーリングし足りない場合は追加 */ + else if (_cmpd (1.0e+17, x) < 0) { + e--; + x *= 1.0e+1; + } + + /* 小数点位置を計算 */ + *decpt = e + 1; + + /* 符号を計算 */ + *sign = ((unsigned char *) &x)[0] & 0x80U; + + /* 文字列に変換 */ + _ulltoa ((unsigned long long) x, buffer); +} diff --git a/x68/_round.c b/x68/_round.c new file mode 100644 index 0000000000..73e44ca966 --- /dev/null +++ b/x68/_round.c @@ -0,0 +1,45 @@ +/* + * PROJECT C Library, X68000 PROGRAMMING INTERFACE DEFINITION + * -------------------------------------------------------------------- + * This file is written by the Project C Library Group, and completely + * in public domain. You can freely use, copy, modify, and redistribute + * the whole contents, without this notice. + * -------------------------------------------------------------------- + * $Id: _round.c,v 1.1 1994/11/27 13:05:36 mura Exp $ + */ +/* changed 1997.2.2 by K.Okabe */ + +/* System headers */ +#include <stdlib.h> +#include <sys/xstdlib.h> + +/* Functions */ +int _round (char *top, char *cur, int undig) +{ + char *ptr; + + /* 最後が5未満なら丸めは必要ない */ + if (undig < '5') + return 0; + + /* ポインタ設定 */ + ptr = cur - 1; + + /* 先頭まで戻りながら丸め処理 */ + while (ptr >= top) { + + /* 繰り上がらなければそれで終わり */ + if (++(*ptr) <= '9') + return 0; + + /* その桁を0に戻す */ + *ptr-- = '0'; + + } + + /* 先頭を1にする */ + *++ptr = '1'; + + /* 繰り上がりをしらせる */ + return 1; +} diff --git a/x68/fconvert.c b/x68/fconvert.c new file mode 100644 index 0000000000..05ff1fbd1a --- /dev/null +++ b/x68/fconvert.c @@ -0,0 +1,81 @@ +/* + * PROJECT C Library, X68000 PROGRAMMING INTERFACE DEFINITION + * -------------------------------------------------------------------- + * This file is written by the Project C Library Group, and completely + * in public domain. You can freely use, copy, modify, and redistribute + * the whole contents, without this notice. + * -------------------------------------------------------------------- + * $Id: fconvert.c,v 1.2 1994/11/27 13:06:19 mura Exp $ + */ +/* changed 1997.2.3 by K.Okabe */ + +/* System headers */ +#include <stdlib.h> +#include <sys/xstdlib.h> + +/* Functions */ +char *fconvert (double x, int ndigit, int *decpt, int *sign, char *buffer) +{ + int pos, n; + char *src, *dst; + char string[24]; + int figup; + + /* 18桁の文字列に変換 */ + _dtos18 (x, decpt, sign, string); + + /* コピー元アドレスを設定 */ + src = string; + + /* コピー先アドレスを設定 */ + dst = buffer; + + /* 小数点位置を得る */ + pos = *decpt; + + /* 小数点位置が負なら */ + if (pos < 0) { + + /* 埋める桁数を計算 */ + n = min (-pos, ndigit); + + /* 先頭を0で埋める */ + while (n-- > 0) + *dst++ = '0'; + + /* 小数点位置は0になる */ + *decpt = 0; + + } + + /* 残りのコピー桁数 */ + n = ndigit + pos; + + /* 格納先にコピー */ + while (n-- > 0) { + + /* 足りない部分は0で埋める */ + if (*src == '\0') { + while (n-- >= 0) + *dst++ = '0'; + break; + } + + /* 変換文字列からコピー */ + *dst++ = *src++; + + } + + /* 丸める */ + *decpt += (figup = _round (buffer, dst, *src)); + + /* 繰り上がりがあれば末尾に0を追加する */ + if (figup) + *dst++ = '0'; + + /* 終端に NUL を打つ */ + *dst = '\0'; + + /* アドレスを返す */ + return buffer; +} diff --git a/x68/select.c b/x68/select.c new file mode 100644 index 0000000000..b4bf464032 --- /dev/null +++ b/x68/select.c @@ -0,0 +1,167 @@ +/* + * PROJECT C Library, X68000 PROGRAMMING INTERFACE DEFINITION + * -------------------------------------------------------------------- + * This file is written by the Project C Library Group, and completely + * in public domain. You can freely use, copy, modify, and redistribute + * the whole contents, without this notice. + * -------------------------------------------------------------------- + * $Id$ + */ + +#ifndef __IOCS_INLINE__ +#define __IOCS_INLINE__ +#define __DOS_INLINE__ +#define __DOS_DOSCALL__ +#endif + +/* System headers */ +#include <errno.h> +#include <fcntl.h> +#include <string.h> +#include <sys/dos.h> +#include <sys/iocs.h> +#include <sys/time.h> +#include <sys/types.h> +#if 0 +#include <sys/select.h> +#include <sys/xsocket.h> +#endif +#include <sys/xunistd.h> + +/* Macros */ +#define XFD_ISSET(fd,fds) ((fds) && FD_ISSET ((fd), (fds))) +#define isreadable(mode) ((mode) == O_RDONLY || (mode) == O_RDWR) +#define iswritable(mode) ((mode) == O_WRONLY || (mode) == O_RDWR) +#ifndef _POSIX_FD_SETSIZE +#define _POSIX_FD_SETSIZE OPEN_MAX +#endif + +/* Functions */ +int +select (int fds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) +{ + fd_set oread, owrite, oexcept; + int ticks, start; + int nfds; + + if (fds > _POSIX_FD_SETSIZE) + { + errno = EINVAL; + return -1; + } + + FD_ZERO (&oread); + FD_ZERO (&owrite); + FD_ZERO (&oexcept); + + nfds = 0; + ticks = -1; + + if (timeout) + { + ticks = timeout->tv_sec * 100 + timeout->tv_usec / 10000; + if (ticks < 0) + { + errno = EINVAL; + return -1; + } + } + + start = _iocs_ontime (); + for (;;) + { + { + int fd; + + for (fd = 0; fd < fds; fd++) + { + int accmode; + + if (_fddb[fd].inuse == _FD_NOTUSED) + continue; + + accmode = _fddb[fd].oflag & O_ACCMODE; + + if (isatty (fd)) + { + if (XFD_ISSET (fd, rfds) && isreadable (accmode) && _dos_k_keysns ()) + { + FD_SET (fd, &oread); + nfds++; + } + + if (XFD_ISSET (fd, wfds) && iswritable (accmode)) + { + FD_SET (fd, &owrite); + nfds++; + } + } +#if 0 + else if (_fddb[fd].sockno >= 0) + { + if (XFD_ISSET (fd, rfds) && _socklen (_fddb[fd].sockno, 0)) + { + FD_SET (fd, &oread); + nfds++; + } + + if (XFD_ISSET (fd, wfds) /* && _socklen (_fddb[fd].sockno, 1) == 0 */) + { + FD_SET (fd, &owrite); + nfds++; + } + } +#endif + else + { + if (XFD_ISSET (fd, rfds) && isreadable (accmode) && _dos_ioctrlis (fd)) + { + FD_SET (fd, &oread); + nfds++; + } + + if (XFD_ISSET (fd, wfds) && iswritable (accmode) && _dos_ioctrlos (fd)) + { + FD_SET (fd, &owrite); + nfds++; + } + } + } + } + + { + int rest; + + if ((rest = (_iocs_ontime () - start) % 8640000) < 0) + rest += 8640000; + + if (nfds != 0) + { + if (ticks >= 0) + { + int left; + + if ((left = ticks - rest) < 0) + left = 0; + + timeout->tv_sec = left / 100; + timeout->tv_usec = (left % 100) * 10000; + } + + if (rfds) + *rfds = oread; + if (wfds) + *wfds = owrite; + if (efds) + *efds = oexcept; + + return nfds; + } + + if (ticks >= 0 && rest > ticks) + return 0; + } + + _dos_change_pr (); + } +} |