9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: cinap_lenrek@felloff.net
To: 9fans@9fans.net
Subject: [9fans] subtracting pointers on amd64 6c
Date: Tue,  5 Jan 2016 07:36:34 +0100	[thread overview]
Message-ID: <8edba5d726ee6a032d8521c7de33f499@felloff.net> (raw)

in the function /sys/src/cmd/cc/sub.c:/^arith we emit code
that cast the 64-bit subtraction result to 32-bit LONG *before*
doing the division of the sizeof of the pointer type.

so when the pointers are more than 4gb apart, we will calculate
the wrong result, even tho the result would have fit into a
32-bit LONG *after* the division.

questions:

1) why do we cast to long?
2) if a pointer subtraction has to yield a long, why dont we cast *after* the division?

	if(n->op == OSUB)
	if(i == TIND && j == TIND) {
		w = n->right->type->link->width;
		if(w < 1 || n->left->type->link == T || n->left->type->link->width < 1)
			goto bad;
		n->type = types[ewidth[TIND] <= ewidth[TLONG]? TLONG: TVLONG];
		if(1 && ewidth[TIND] > ewidth[TLONG]){	<------- here
			n1 = new1(OXXX, Z, Z);
			*n1 = *n;
			n->op = OCAST;
			n->left = n1;
			n->right = Z;
			n->type = types[TLONG];
		}
		if(w > 1) {
			n1 = new1(OXXX, Z, Z);
			*n1 = *n;
			n->op = ODIV;
			n->left = n1;
			n1 = new1(OCONST, Z, Z);
			n1->vconst = w;
			n1->type = n->type;
			n->right = n1;
			w = vlog(n1);
			if(w >= 0) {
				n->op = OASHR;
				n1->vconst = w;
			}
		}
		return;
	}

--
cinap



             reply	other threads:[~2016-01-05  6:36 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-05  6:36 cinap_lenrek [this message]
2016-01-05  7:53 ` cinap_lenrek
2016-01-05 10:17   ` Charles Forsyth
2016-01-05 10:15 ` Charles Forsyth
2016-01-05 10:28   ` Charles Forsyth
2016-01-05 19:01     ` Devon H. O'Dell
2016-01-05 19:46       ` Charles Forsyth
2016-01-05 20:40         ` erik quanstrom
2016-01-05 21:53           ` Devon H. O'Dell
2016-01-05 23:27             ` erik quanstrom
2016-01-05 23:37       ` Dave Eckhardt
2016-01-05 21:03     ` erik quanstrom
2016-01-05 22:32     ` cinap_lenrek
2016-01-05 22:57       ` Devon H. O'Dell
2016-01-05 23:17         ` Devon H. O'Dell
2016-01-05 23:14       ` erik quanstrom
2016-01-06  6:58       ` cinap_lenrek
2016-01-06 19:10         ` erik quanstrom
2016-01-07  1:08           ` cinap_lenrek
2016-01-08 22:23             ` erik quanstrom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8edba5d726ee6a032d8521c7de33f499@felloff.net \
    --to=cinap_lenrek@felloff.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).