From owner-freebsd-arch@FreeBSD.ORG Mon Jun 16 03:48:32 2003 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 579BB37B401 for ; Mon, 16 Jun 2003 03:48:32 -0700 (PDT) Received: from gw.catspoiler.org (217-ip-163.nccn.net [209.79.217.163]) by mx1.FreeBSD.org (Postfix) with ESMTP id AB97443FA3 for ; Mon, 16 Jun 2003 03:48:31 -0700 (PDT) (envelope-from truckman@FreeBSD.org) Received: from FreeBSD.org (mousie.catspoiler.org [192.168.101.2]) by gw.catspoiler.org (8.12.9/8.12.9) with ESMTP id h5GAmMM7048782; Mon, 16 Jun 2003 03:48:27 -0700 (PDT) (envelope-from truckman@FreeBSD.org) Message-Id: <200306161048.h5GAmMM7048782@gw.catspoiler.org> Date: Mon, 16 Jun 2003 03:48:22 -0700 (PDT) From: Don Lewis To: bde@zeta.org.au In-Reply-To: <20030616125941.G26874@gamplex.bde.org> MIME-Version: 1.0 Content-Type: TEXT/plain; charset=us-ascii cc: iedowse@maths.tcd.ie cc: freebsd-arch@FreeBSD.org Subject: Re: Message buffer and printf reentrancy patch X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 16 Jun 2003 10:48:32 -0000 On 16 Jun, Bruce Evans wrote: > On Sun, 15 Jun 2003, Ian Dowse wrote: > >> In message <200306151826.h5FIPvM7046944@gw.catspoiler.org>, Don Lewis writes: >> > >> >> +#define MSGBUF_SEQNORM(mbp, seq) ((seq) % (mbp)->msg_seqmod + ((seq) < 0 ? >> >\ >> >> + (mbp)->msg_seqmod : 0)) >> >> +#define MSGBUF_SEQ_TO_POS(mbp, seq) ((int)((u_int)(seq) % \ >> >> + (u_int)(mbp)->msg_size)) >> >> +#define MSGBUF_SEQSUB(mbp, seq1, seq2) (MSGBUF_SEQNORM(mbp, (seq1) - (seq2) >> >)) >> >> + > > Sorry I didn't reply to Ian's provate mail about all this last month. I'll > try to get back to it. > >> >According to my copy of K&R, there is no guarantee that ((negative_int % >> >postive_int) <= 0) on all platforms, though this is generally true. > > C99 guarantees this perfect brokenness of the % operator. Division should > give remainders that have the same sign as the divisor, which corresponds > to rounding towards minus infinity for positive divisors, but is now > specified to be bug for bug compatible with most hardware and most C > implementations (round towards zero). > > MSGBUF_SEQ_TO_POS() does extra work to get nonnegative remainders. > > This problem and many casts could be avoided by using unsigned types > for most of the msgbuf fields. I forget the details of why we changed > them back to signed. The log message for msgbuf.h 1.19 says that this > is because we perform signed arithmetic on them. The details for this, > can probably be handled by the macros now. Using unsigned types was the first thing that I thought of. I was wondering if the reason that this wasn't done was some sort of portability problem with the atomic operations. It looks like MSGBUF_SEQNORM() could avoid the conditional code and any questions about signed remainders if it was defined like this: #define MSGBUF_SEQNORM(mbp, seq) (((seq) + (mbp)->msg_seqmod) % \ (mbp)->msg_seqmod) as long as msg_seqmod < INT_MAX/2. MSGBUF_SEQNORM() could be simplified further if msg_seqmod was added by the caller (such as MSGBUF_SEQSUB()) if the argument could be negative.