delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/06/20/12:57:33

From: Martin Str|mberg <ams AT ludd DOT luth DOT se>
Message-Id: <200106201656.SAA22736@father.ludd.luth.se>
Subject: Re: More signed/unsigned issues
In-Reply-To: <3028-Wed20Jun2001102214+0300-eliz@is.elta.co.il> from Eli Zaretskii at "Jun 20, 2001 10:22:15 am"
To: djgpp-workers AT delorie DOT com
Date: Wed, 20 Jun 2001 18:56:13 +0200 (MET DST)
X-Mailer: ELM [version 2.4ME+ PL54 (25)]
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

According to Eli Zaretskii:
> >        while (s > 0) {
> > -       if (--f->_cnt > -f->_bufsiz && *(const char *)ptr != '\n')
> > +       if (--f->_cnt > -(long signed int)f->_bufsiz && *(const char *)ptr != 'n')
> >           *f->_ptr++ = *(const char *)ptr++;
> 
> Isn't it better to introduce a new `long signed int' variable, assign
> to it the value of f->_bufsiz, and then use that new variable instead
> of f->_bufsiz?  Let the optimizer do the dirty work ;-)

Like this?:

Index: src/libc/ansi/stdio/fwrite.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/ansi/stdio/fwrite.c,v
retrieving revision 1.6
diff -p -u -r1.6 fwrite.c
--- src/libc/ansi/stdio/fwrite.c        2001/06/09 10:57:08     1.6
+++ src/libc/ansi/stdio/fwrite.c        2001/06/20 16:50:25
@@ -12,7 +12,7 @@ size_t
 fwrite(const void *vptr, size_t size, size_t count, FILE *f)
 {
   const char *ptr = (const char *)vptr;
-  size_t s;
+  ssize_t s;

   if (__libc_write_termios_hook
       && (f->_flag & (_IOTERM | _IONTERM)) == 0)
@@ -29,7 +29,9 @@ fwrite(const void *vptr, size_t size, si
   {
     if (f->_flag & _IOLBF)
       while (s > 0) {
-       if (--f->_cnt > -f->_bufsiz && *(const char *)ptr != '\n')
+       long signed int negative_bufsiz = -f->_bufsiz;
+
+       if (--f->_cnt > negative_bufsiz && *(const char *)ptr != '\n')
          *f->_ptr++ = *(const char *)ptr++;
        else if (_flsbuf(*(const char *)ptr++, f) == EOF)
          break;
@@ -60,15 +62,17 @@ fwrite(const void *vptr, size_t size, si
     if (f->_flag & _IOLBF)
     {
       while (s > 0) {
+       long signed int negative_bufsiz = -f->_bufsiz;
+
         if (*ptr=='\n')
         {
-          if (--f->_cnt > -f->_bufsiz)
+          if (--f->_cnt > negative_bufsiz)
             *f->_ptr++ = '\r';
           else
             if (_flsbuf('\r', f) == EOF)
              break;
         }
-       if (--f->_cnt > -f->_bufsiz && *ptr != '\n')
+       if (--f->_cnt > negative_bufsiz && *ptr != '\n')
          *f->_ptr++ = *ptr++;
        else if (_flsbuf(*(const unsigned char *)ptr++, f) == EOF)
          break;

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019