From 7ddf7686a37e0434adcdc8536daa033c9dff80fa Mon Sep 17 00:00:00 2001 From: Serge Vakulenko Date: Tue, 3 Nov 2015 21:36:31 -0800 Subject: [PATCH 1/4] Enable mips16e instruction set for all user binaries. --- lib/Makefile | 2 +- lib/libc/Makefile | 2 +- lib/libcurses/Makefile | 2 +- lib/libgpanel/Makefile | 2 +- lib/libm/Makefile | 2 +- lib/libreadline/Makefile | 2 +- lib/libtermlib/Makefile | 2 +- lib/libwiznet/Makefile | 2 +- lib/startup/Makefile | 2 +- src/cmd/Makefile | 2 +- src/cmd/ar/delete.c | 3 ++- src/cmd/ar/move.c | 3 ++- src/cmd/ar/replace.c | 3 ++- src/cmd/ld/ld.c | 3 ++- src/cmd/ranlib/ranlib.c | 3 ++- src/cmd/strip.c | 3 ++- src/cmd/unixbench/Makefile | 2 +- src/games/atc/Makefile | 2 +- src/libm/fmod.c | 28 +++++++++++++++++++++++++++- src/libm/ieee.h | 23 ----------------------- target.mk | 3 +++ 21 files changed, 54 insertions(+), 42 deletions(-) delete mode 100644 src/libm/ieee.h diff --git a/lib/Makefile b/lib/Makefile index 959883f..186d31a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -2,7 +2,7 @@ TOPSRC = $(shell cd ..; pwd) SUBDIR = startup libc libm libcurses libtermlib libwiznet libreadline libgpanel PROG = ar as aout ld nm ranlib size strip -CFLAGS += -std=gnu89 -fno-builtin -g -Werror -Wall -DCROSS -I. \ +CFLAGS = -Os -std=gnu89 -fno-builtin -g -Werror -Wall -DCROSS -I. \ -idirafter $(TOPSRC)/include \ -idirafter $(TOPSRC)/src/cmd/ar \ -idirafter $(TOPSRC)/src/cmd/as diff --git a/lib/libc/Makefile b/lib/libc/Makefile index 7340edb..7a32960 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -8,7 +8,7 @@ vpath %.c $(LIBCDIR)/mips/sys $(LIBCDIR)/gen $(LIBCDIR)/stdio \ $(LIBCDIR)/stdlib $(LIBCDIR)/string $(LIBCDIR)/inet \ $(LIBCDIR)/compat $(LIBCDIR)/runtime -CFLAGS += -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror +CFLAGS = -Os -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror ASFLAGS += -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -I$(LIBCDIR)/mips/sys # modules which can not use SYSCALL and must be assembled from sources. The diff --git a/lib/libcurses/Makefile b/lib/libcurses/Makefile index b2fde22..3a944d9 100644 --- a/lib/libcurses/Makefile +++ b/lib/libcurses/Makefile @@ -3,7 +3,7 @@ include $(TOPSRC)/target.mk vpath %.c $(TOPSRC)/src/libcurses -CFLAGS += -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror +CFLAGS = -Os -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror OBJS = addch.o addstr.o box.o clear.o clrtobot.o clrtoeol.o cr_put.o \ cr_tty.o curses.o delch.o deleteln.o delwin.o endwin.o erase.o \ diff --git a/lib/libgpanel/Makefile b/lib/libgpanel/Makefile index 11c8526..0460d42 100644 --- a/lib/libgpanel/Makefile +++ b/lib/libgpanel/Makefile @@ -3,7 +3,7 @@ include $(TOPSRC)/target.mk vpath %.c $(TOPSRC)/src/libgpanel -CFLAGS += -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror +CFLAGS = -Os -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror OBJS = open.o clear.o pixel.o line.o rect.o fill.o circle.o \ image.o char.o text.o text_width.o diff --git a/lib/libm/Makefile b/lib/libm/Makefile index d8c2e9a..b9ef088 100644 --- a/lib/libm/Makefile +++ b/lib/libm/Makefile @@ -3,7 +3,7 @@ include $(TOPSRC)/target.mk vpath %.c $(TOPSRC)/src/libm -CFLAGS += -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror +CFLAGS = -Os -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror OBJS = asin.o atan.o exp.o erf.o floor.o fmod.o hypot.o j0.o j1.o \ jn.o log.o pow.o sin.o sinh.o sqrt.o tan.o tanh.o diff --git a/lib/libreadline/Makefile b/lib/libreadline/Makefile index 5acadf2..1298bed 100644 --- a/lib/libreadline/Makefile +++ b/lib/libreadline/Makefile @@ -3,7 +3,7 @@ include $(TOPSRC)/target.mk vpath %.c $(TOPSRC)/src/libreadline -CFLAGS += -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror +CFLAGS = -Os -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror OBJS = readline.o diff --git a/lib/libtermlib/Makefile b/lib/libtermlib/Makefile index 2b3fb1a..72e1ea2 100644 --- a/lib/libtermlib/Makefile +++ b/lib/libtermlib/Makefile @@ -3,7 +3,7 @@ include $(TOPSRC)/target.mk vpath %.c $(TOPSRC)/src/libtermlib -CFLAGS += -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror +CFLAGS = -Os -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror OBJS = termcap.o tgoto.o tputs.o tcattr.o diff --git a/lib/libwiznet/Makefile b/lib/libwiznet/Makefile index d7883bc..2fc674b 100644 --- a/lib/libwiznet/Makefile +++ b/lib/libwiznet/Makefile @@ -3,7 +3,7 @@ include $(TOPSRC)/target.mk vpath %.c $(TOPSRC)/src/libwiznet -CFLAGS += -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror +CFLAGS = -Os -B$(TOPSRC)/lib/ $(DEFS) -Wa,-x -Wall -Werror OBJS = w5100.o socket.o ethernet.o client.o server.o udp.o diff --git a/lib/startup/Makefile b/lib/startup/Makefile index a4e47a6..5e34f6a 100644 --- a/lib/startup/Makefile +++ b/lib/startup/Makefile @@ -9,7 +9,7 @@ TOPSRC = $(shell cd ../..; pwd) include $(TOPSRC)/target.mk vpath %.c $(TOPSRC)/src/startup-mips -CFLAGS = -B$(TOPSRC)/lib/ -O -Wa,-x $(DEFS) +CFLAGS = -Os -B$(TOPSRC)/lib/ -Wa,-x $(DEFS) OBJS = ../crt0.o diff --git a/src/cmd/Makefile b/src/cmd/Makefile index 0031d6d..d5784b6 100644 --- a/src/cmd/Makefile +++ b/src/cmd/Makefile @@ -16,7 +16,7 @@ SUBDIR = adb adc-demo aout ar as awk basic calendar cc chflags \ man md5 med more nm passwd pdc picoc portio printf pwm \ ranlib re renice retroforth scm sed setty sh sl smallc \ smlrc smux stty sysctl test uname wiznet xargs \ - zmodem gtest msec unixbench cron compress date2 tip \ + zmodem gtest msec cron compress date2 tip \ talloc uucp # /sbin diff --git a/src/cmd/ar/delete.c b/src/cmd/ar/delete.c index 73d6f90..c9acfd5 100644 --- a/src/cmd/ar/delete.c +++ b/src/cmd/ar/delete.c @@ -91,7 +91,8 @@ delete(argv) SETCF(tfd, tname, afd, archive, NOPAD); copy_ar(&cf, size); (void)close(tfd); - (void)ftruncate(afd, size + SARMAG); + if (ftruncate(afd, size + SARMAG) < 0) + /* ignore */; close_archive(afd); if (*argv) { diff --git a/src/cmd/ar/move.c b/src/cmd/ar/move.c index a44ef3b..7c8b432 100644 --- a/src/cmd/ar/move.c +++ b/src/cmd/ar/move.c @@ -136,7 +136,8 @@ move(argv) cf.rfd = tfd3; copy_ar(&cf, size); - (void)ftruncate(afd, tsize + SARMAG); + if (ftruncate(afd, tsize + SARMAG) < 0) + /* ignore */; close_archive(afd); if (*argv) { diff --git a/src/cmd/ar/replace.c b/src/cmd/ar/replace.c index 51d4702..71878f4 100644 --- a/src/cmd/ar/replace.c +++ b/src/cmd/ar/replace.c @@ -190,7 +190,8 @@ append: while ((file = *argv++) != 0) { cf.rfd = tfd2; copy_ar(&cf, size); - (void)ftruncate(afd, tsize + SARMAG); + if (ftruncate(afd, tsize + SARMAG) < 0) + /* ignore */; close_archive(afd); return(err); } diff --git a/src/cmd/ld/ld.c b/src/cmd/ld/ld.c index d42c73c..b14ded8 100644 --- a/src/cmd/ld/ld.c +++ b/src/cmd/ld/ld.c @@ -1568,7 +1568,8 @@ int main (argc, argv) if (! ofilfnd) { unlink ("a.out"); - link ("l.out", "a.out"); + if (link ("l.out", "a.out") < 0) + perror("a.out"); ofilename = "a.out"; } delarg = errlev; diff --git a/src/cmd/ranlib/ranlib.c b/src/cmd/ranlib/ranlib.c index 0db19c4..8d0d668 100644 --- a/src/cmd/ranlib/ranlib.c +++ b/src/cmd/ranlib/ranlib.c @@ -385,7 +385,8 @@ int build() (void)lseek(tfd, (off_t)0, SEEK_SET); SETCF(tfd, tname, afd, archive, RPAD|WPAD); copy_ar(&cf, size); - (void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR)); + if (ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR)) < 0) + /* ignore */; (void)close(tfd); /* Set the time. */ diff --git a/src/cmd/strip.c b/src/cmd/strip.c index 4db2d96..62ca304 100644 --- a/src/cmd/strip.c +++ b/src/cmd/strip.c @@ -50,7 +50,8 @@ strip(name) head.a_reldata = 0; head.a_syms = 0; (void) lseek(f, (off_t)0, SEEK_SET); - (void) write(f, (char *)&head, sizeof (head)); + if (write(f, (char *)&head, sizeof (head)) != sizeof (head)) + /* ignore */; out: close(f); } diff --git a/src/cmd/unixbench/Makefile b/src/cmd/unixbench/Makefile index eb6c991..4f4d98f 100644 --- a/src/cmd/unixbench/Makefile +++ b/src/cmd/unixbench/Makefile @@ -57,7 +57,7 @@ OPTON = -O2 -fomit-frame-pointer -fforce-addr -ffast-math -Wall #CFLAGS = -DTIME -std1 -verbose -w0 ## generic gcc CFLAGS. -DTIME must be included -CFLAGS = -DTIME -Wall -pedantic -ansi +#CFLAGS = -DTIME -Wall -pedantic -ansi # local directories PROGDIR = ./pgms diff --git a/src/games/atc/Makefile b/src/games/atc/Makefile index 90735f0..61f5189 100644 --- a/src/games/atc/Makefile +++ b/src/games/atc/Makefile @@ -9,7 +9,7 @@ TOPSRC = $(shell cd ../../..; pwd) include $(TOPSRC)/target.mk #include $(TOPSRC)/cross.mk -CFLAGS = -Os -g -Werror -Wall +CFLAGS += -Os -g -Werror -Wall CFLAGS += -DBSD -DDEST=\"/games/lib/atc/\" #CFLAGS += -DSYSV -DDEST=\"games/\" YFLAGS = -d diff --git a/src/libm/fmod.c b/src/libm/fmod.c index cc46ffb..4529fea 100644 --- a/src/libm/fmod.c +++ b/src/libm/fmod.c @@ -1,7 +1,33 @@ #include #include -#include "ieee.h" +typedef union { + double value; + struct { + uint32_t lo; + uint32_t hi; + } uns; +} union64_t; + +/* + * Get two 32 bit ints from a double. + */ +#define EXTRACT_WORDS(high,low,d) {\ + union64_t u = {0}; \ + u.value = d; \ + high = u.uns.hi; \ + low = u.uns.lo; \ + } + +/* + * Set a double from two 32 bit ints. + */ +#define INSERT_WORDS(d,high,low) { \ + union64_t u = {0}; \ + u.uns.hi = high; \ + u.uns.lo = low; \ + d = u.value; \ + } static const double one = 1.0, Zero[] = {0.0, -0.0,}; diff --git a/src/libm/ieee.h b/src/libm/ieee.h deleted file mode 100644 index 20a8eee..0000000 --- a/src/libm/ieee.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Get two 32 bit ints from a double. */ - -#define EXTRACT_WORDS(high,low,d) \ - high = *(unsigned long long*) &d; \ - low = (*(unsigned long long*) &d) >> 32 - - -/* Set a double from two 32 bit ints. */ - -#define INSERT_WORDS(d,high,low) \ - *(unsigned long long*) &(x) = (unsigned long long) (high) << 32 | (low) - - -typedef union -{ - double value; - struct - { - uint32_t lsw; - uint32_t msw; - } parts; -} ieee_double_shape_type; - diff --git a/target.mk b/target.mk index 267f992..14b7e79 100644 --- a/target.mk +++ b/target.mk @@ -85,3 +85,6 @@ CFLAGS = -O LDFLAGS += -N -nostartfiles -fno-dwarf2-cfi-asm -T$(TOPSRC)/src/elf32-mips.ld \ $(TOPSRC)/src/crt0.o -L$(TOPSRC)/src LIBS = -lc + +# Enable mips16e instruction set by default +CFLAGS += -mips16 From a673c028f85d6350824e72c36726dca4979f6ea3 Mon Sep 17 00:00:00 2001 From: Serge Vakulenko Date: Tue, 3 Nov 2015 21:37:50 -0800 Subject: [PATCH 2/4] Add ubw32 board to the autobuild list. --- tools/build/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/build/Makefile b/tools/build/Makefile index 277689d..f1c96d5 100644 --- a/tools/build/Makefile +++ b/tools/build/Makefile @@ -1,5 +1,5 @@ BOARD_LIST = duinomite fubarino max32 maximite maximite-color \ - picadillo pinguino-micro sdxl snadpic + picadillo pinguino-micro sdxl snadpic ubw32 SKEL_SCRATCH = skeleton/linux32/pic32prog \ skeleton/linux64/pic32prog \ From c46bd3c1e9000e723c79c368fef5da1ddc88b784 Mon Sep 17 00:00:00 2001 From: Serge Vakulenko Date: Tue, 3 Nov 2015 22:03:52 -0800 Subject: [PATCH 3/4] Enable optimization by size (-Os) by default for all user binaries. --- src/cmd/adb/format.c | 11 +++++++---- target.mk | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/cmd/adb/format.c b/src/cmd/adb/format.c index e4710ae..9381a49 100644 --- a/src/cmd/adb/format.c +++ b/src/cmd/adb/format.c @@ -91,6 +91,7 @@ exform(fcount, ifp, itype, ptype) struct { long sa; int sb, sc; + double sd; /* assume double = float = 32 bits */ } fw; while (fcount > 0) { @@ -225,18 +226,20 @@ exform(fcount, ifp, itype, ptype) break; case 'f': - *(double *)&fw = 0.0; + case 'F': + fw.sd = 0.0; fw.sa = wx; - print("%-16.9f", *(double *)&fw); + print("%-16.9f", fw.sd); dotinc = 4; break; - +#if 0 +/* 64-bit double not supported */ case 'F': fw.sa = wx; print("%-32.18F", *(double *)&fw); dotinc = 8; break; - +#endif case 'n': case 'N': printc('\n'); dotinc = 0; diff --git a/target.mk b/target.mk index 14b7e79..0ce66d8 100644 --- a/target.mk +++ b/target.mk @@ -80,7 +80,7 @@ TAGSFILE = tags MANROFF = nroff -man -h -Tascii ELF2AOUT = $(TOPSRC)/tools/elf2aout/elf2aout -CFLAGS = -O +CFLAGS = -Os LDFLAGS += -N -nostartfiles -fno-dwarf2-cfi-asm -T$(TOPSRC)/src/elf32-mips.ld \ $(TOPSRC)/src/crt0.o -L$(TOPSRC)/src From 03ed62db5dc2cd0dc451d12c02a3c4e675c93b43 Mon Sep 17 00:00:00 2001 From: Serge Vakulenko Date: Fri, 6 Nov 2015 20:40:45 -0800 Subject: [PATCH 4/4] Libm: add 32-bit variant of fmod() function. --- src/libm/fmod.c | 271 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 201 insertions(+), 70 deletions(-) diff --git a/src/libm/fmod.c b/src/libm/fmod.c index 4529fea..61b2924 100644 --- a/src/libm/fmod.c +++ b/src/libm/fmod.c @@ -12,7 +12,7 @@ typedef union { /* * Get two 32 bit ints from a double. */ -#define EXTRACT_WORDS(high,low,d) {\ +#define UNPACK_DOUBLE(high,low,d) {\ union64_t u = {0}; \ u.value = d; \ high = u.uns.hi; \ @@ -22,116 +22,247 @@ typedef union { /* * Set a double from two 32 bit ints. */ -#define INSERT_WORDS(d,high,low) { \ +#define PACK_DOUBLE(d,high,low) { \ union64_t u = {0}; \ u.uns.hi = high; \ u.uns.lo = low; \ d = u.value; \ } +typedef union { + float value; + uint32_t word; +} union32_t; + +/* + * Get two 32 bit ints from a double. + */ +#define UNPACK_FLOAT(w,f) {\ + union32_t u = {0}; \ + u.value = f; \ + w = u.word; \ + } + +/* + * Set a double from two 32 bit ints. + */ +#define PACK_FLOAT(f,w) { \ + union32_t u = {0}; \ + u.word = w; \ + f = u.value; \ + } + static const double one = 1.0, Zero[] = {0.0, -0.0,}; double fmod(double x, double y) { - int32_t n=0,hx=0,hy=0,hz=0,ix=0,iy=0,sx=0,i=0; - uint32_t lx=0,ly=0,lz=0; + int32_t n=0, hx=0, hy=0, hz=0, ix=0, iy=0, sx=0, i=0; - EXTRACT_WORDS(hx,lx,x); - EXTRACT_WORDS(hy,ly,y); - sx = hx&0x80000000; /* sign of x */ - hx ^=sx; /* |x| */ - hy &= 0x7fffffff; /* |y| */ + if (sizeof(float) == sizeof(double)) { + /* + * Double is 32-bit. + */ + UNPACK_FLOAT(hx,x); + UNPACK_FLOAT(hy,y); + sx = hx & 0x80000000; /* sign of x */ + hx ^= sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ - /* purge off exception values */ - if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ - ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */ - return (x*y)/(x*y); - if(hx<=hy) { - if((hx>31]; /* |x|=|y| return x*0*/ + /* purge off exception values */ + if (hy == 0 || hx >= 0x7f800000 || /* y=0, or x not finite */ + hy > 0x7f800000) /* or y is NaN */ + return (x*y) / (x*y); + if (hx < hy) + return x; /* |x| < |y| return x */ + if (hx == hy) + return Zero[(uint32_t)sx >> 31]; /* |x| = |y| return x*0*/ + + /* determine ix = ilogb(x) */ + if (hx < 0x00800000) { /* subnormal x */ + for (ix= -126, i=hx<<8; i>0; i<<=1) + ix -= 1; + } else + ix = (hx >> 23) - 127; + + /* determine iy = ilogb(y) */ + if (hy < 0x00800000) { /* subnormal y */ + for (iy= -126, i=hy<<8; i>=0; i<<=1) + iy -= 1; + } else iy = (hy >> 23) - 127; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if (ix >= -126) + hx = 0x00800000 | (0x007fffff & hx); + else { /* subnormal x, shift x to normal */ + n = -126 - ix; + hx = hx << n; + } + if (iy >= -126) + hy = 0x00800000 | (0x007fffff & hy); + else { /* subnormal y, shift y to normal */ + n = -126 - iy; + hy = hy << n; } - /* determine ix = ilogb(x) */ - if(hx<0x00100000) { /* subnormal x */ - if(hx==0) { - for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; + /* fix point fmod */ + n = ix - iy; + while (n--) { + hz = hx - hy; + if (hz < 0) { + hx = hx + hx; } else { - for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; + if (hz == 0) /* return sign(x)*0 */ + return Zero[(uint32_t)sx >> 31]; + hx = hz + hz; } - } else ix = (hx>>20)-1023; + } + hz = hx - hy; + if (hz >= 0) { + hx = hz; + } - /* determine iy = ilogb(y) */ - if(hy<0x00100000) { /* subnormal y */ + /* convert back to floating value and restore the sign */ + if (hx == 0) /* return sign(x)*0 */ + return Zero[(uint32_t)sx >> 31]; + while (hx < 0x00800000) { /* normalize x */ + hx = hx + hx; + iy -= 1; + } + if (iy >= -126) { /* normalize output */ + hx = (hx - 0x00800000) | ((iy + 127) << 23); + PACK_FLOAT(x, hx | sx); + } else { /* subnormal output */ + n = -126 - iy; + hx >>= n; + PACK_FLOAT(x, hx | sx); + x *= one; /* create necessary signal */ + } + } else { + /* + * Double is 64-bit. + */ + uint32_t lx=0, ly=0, lz=0; + + UNPACK_DOUBLE(hx, lx, x); + UNPACK_DOUBLE(hy, ly, y); + sx = hx & 0x80000000; /* sign of x */ + hx ^= sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if ((hy | ly) == 0 || hx >= 0x7ff00000 || /* y=0,or x not finite */ + (hy | ((ly | -ly) >> 31)) > 0x7ff00000) /* or y is NaN */ + return (x*y) / (x*y); + if (hx <= hy) { + if (hx < hy || lx < ly) + return x; /* |x| < |y| return x */ + if (lx == ly) + return Zero[(uint32_t)sx >> 31]; /* |x| = |y| return x*0 */ + } + + /* determine ix = ilogb(x) */ + if (hx < 0x00100000) { /* subnormal x */ + if (hx == 0) { + for (ix = -1043, i=lx; i>0; i<<=1) + ix -= 1; + } else { + for (ix = -1022, i=hx<<11; i>0; i<<=1) + ix -= 1; + } + } else + ix = (hx >> 20) - 1023; + + /* determine iy = ilogb(y) */ + if (hy < 0x00100000) { /* subnormal y */ if(hy==0) { - for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; + for (iy = -1043, i=ly; i>0; i<<=1) + iy -= 1; } else { - for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; + for (iy = -1022,i=(hy<<11); i>0; i<<=1) + iy -= 1; } - } else iy = (hy>>20)-1023; + } else + iy = (hy >> 20) - 1023; - /* set up {hx,lx}, {hy,ly} and align y to x */ - if(ix >= -1022) - hx = 0x00100000|(0x000fffff&hx); - else { /* subnormal x, shift x to normal */ - n = -1022-ix; - if(n<=31) { - hx = (hx<>(32-n)); + /* set up {hx,lx}, {hy,ly} and align y to x */ + if (ix >= -1022) + hx = 0x00100000 | (0x000fffff & hx); + else { /* subnormal x, shift x to normal */ + n = -1022 - ix; + if (n <= 31) { + hx = (hx << n) | (lx >> (32 - n)); lx <<= n; } else { - hx = lx<<(n-32); + hx = lx << (n - 32); lx = 0; } } - if(iy >= -1022) - hy = 0x00100000|(0x000fffff&hy); - else { /* subnormal y, shift y to normal */ - n = -1022-iy; - if(n<=31) { - hy = (hy<>(32-n)); + if (iy >= -1022) + hy = 0x00100000 | (0x000fffff & hy); + else { /* subnormal y, shift y to normal */ + n = -1022 - iy; + if (n <= 31) { + hy = (hy << n) | (ly >> (32 - n)); ly <<= n; } else { - hy = ly<<(n-32); + hy = ly << (n - 32); ly = 0; } } - /* fix point fmod */ + /* fix point fmod */ n = ix - iy; - while(n--) { - hz=hx-hy;lz=lx-ly; if(lx>31); lx = lx+lx;} - else { - if((hz|lz)==0) /* return sign(x)*0 */ - return Zero[(uint32_t)sx>>31]; - hx = hz+hz+(lz>>31); lx = lz+lz; + while (n--) { + hz = hx - hy; + lz = lx - ly; + if (lx < ly) + hz -= 1; + + if (hz < 0) { + hx = hx + hx + (lx >> 31); + lx = lx + lx; + } else { + if ((hz | lz) == 0) /* return sign(x)*0 */ + return Zero[(uint32_t)sx >> 31]; + hx = hz + hz + (lz >> 31); + lx = lz + lz; } } - hz=hx-hy;lz=lx-ly; if(lx=0) {hx=hz;lx=lz;} + hz = hx - hy; + lz = lx - ly; + if (lx < ly) + hz -= 1; + if (hz >= 0) { + hx = hz; + lx = lz; + } - /* convert back to floating value and restore the sign */ - if((hx|lx)==0) /* return sign(x)*0 */ - return Zero[(uint32_t)sx>>31]; - while(hx<0x00100000) { /* normalize x */ - hx = hx+hx+(lx>>31); lx = lx+lx; + /* convert back to floating value and restore the sign */ + if ((hx | lx) == 0) /* return sign(x)*0 */ + return Zero[(uint32_t)sx >> 31]; + while (hx < 0x00100000) { /* normalize x */ + hx = hx + hx + (lx >> 31); + lx = lx + lx; iy -= 1; } - if(iy>= -1022) { /* normalize output */ - hx = ((hx-0x00100000)|((iy+1023)<<20)); - INSERT_WORDS(x,hx|sx,lx); - } else { /* subnormal output */ + if (iy >= -1022) { /* normalize output */ + hx = (hx - 0x00100000) | ((iy + 1023) << 20); + PACK_DOUBLE(x, hx | sx, lx); + } else { /* subnormal output */ n = -1022 - iy; - if(n<=20) { - lx = (lx>>n)|((uint32_t)hx<<(32-n)); + if (n <= 20) { + lx = (lx >> n) | ((uint32_t)hx << (32 - n)); hx >>= n; - } else if (n<=31) { - lx = (hx<<(32-n))|(lx>>n); hx = sx; + } else if (n <= 31) { + lx = (hx << (32 - n)) | (lx >> n); + hx = sx; } else { - lx = hx>>(n-32); hx = sx; + lx = hx >> (n - 32); + hx = sx; } - INSERT_WORDS(x,hx|sx,lx); - x *= one; /* create necessary signal */ + PACK_DOUBLE(x, hx | sx, lx); + x *= one; /* create necessary signal */ } - return x; /* exact output */ + } + return x; /* exact output */ }