9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] subtracting pointers on amd64 6c
@ 2016-01-05  6:36 cinap_lenrek
  2016-01-05  7:53 ` cinap_lenrek
  2016-01-05 10:15 ` Charles Forsyth
  0 siblings, 2 replies; 20+ messages in thread
From: cinap_lenrek @ 2016-01-05  6:36 UTC (permalink / raw)
  To: 9fans

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



^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2016-01-08 22:23 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-05  6:36 [9fans] subtracting pointers on amd64 6c cinap_lenrek
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

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).