suggested improvements to D

Warren D Smith wds at math.temple.edu
Mon Jan 8 17:40:25 PST 2007


Here are some suggested improvements for D.

a <--> b;
swaps a and b, same as making a tempvar t and then  t=a; a=b; b=t;
As was pointed out by Robert W. Floyd ages ago, in his Turing award
lecture (but nobody listened) this comes up so
often it is worth having special syntax for it.


labeled break and continue statements:
that was a fine idea to eliminate a lot of gotos.
HOWEVER, the most common way in which gotos happen that's not covered by D's
break and continue is this:

for(some loop condition){
  if(something){ goto A; }
}
fallthru_code;
A:
break_out_code;

is there a nice way to handle these without gotos?   One possibility
is to add a "default"
block at ends of loops:
for(some loop condition){
  if(something){ break; }
}default{
  fallthru_code;
}
break_out_code;


popcount and first1:
Often one represents a set by means of an array of booleans, or
equivalently by the bits of a machine word.
Now, the cray used to have a wonderful "popcount" machine-instruction
 a = popcount(b);
which would cause the number of 1-bits in the uint b, to be placed in
the integer a.
E.g. popcount(21)=3.
Also, some machines had a "first1" instruction which would cause, e.g,
  c=0;
  printf("here are the locations of the 1bits in b: ");
  for(a = first1(b); b!=0; b >>= a){
     c += a;
     printf("%d,", c);
  }
  printf("\n");
to work as a way to loop thru the elements of the set.

This was all very fine.  It led to TREMENDOUS speedup and
simplification of the right kinds of programs.
BUT, later hardware got rid of the instruction and languages
did not have these as features.  Vicious circle:
  Hardware designers:  no language has popcount and first1, so we see no
reason to support it in hardware.
  Language designers: hardware does not support these, so why should
our language?
Break the cycle - put these in D!   This'll speed up chess programs by
a factor of 2.

[Aside:
Another excellent hardware instruction that as far as I know was never
implemented is
the "dovetail" and "undovetail" instructions
which place the even-indexed bits of a word into a halfword, and the
odd-index ones
into another (undovetail) and dovetail is the reverse.]


Undenying access to arithmetic:
hardware goes to a great amount of trouble to provide add-with-carry,
multiply-two-singlewords-to-get-a-doubleword,
long-division-with-quotient&remainder
(if a is a doubleword divide a by b to get quotient q and remainder r,
all singlewords), etc. to us.
Another good thing available in a lot of hardware is shift-with-carry,
and circular shift with and without carry.
Why the hell does the language then tell the user to go screw
themselves - we intend to deny
you access to that power??!!!
Hello?  This makes it very painful or
difficult or slow
to build a bignum package.  It is just gratuitously asinine.  If D
delivered this it'd (a) be veyr easy, (b) instantly attract a lot of converts
and you could build a portable bignum code that ran about 4
times faster.

Warren D. Smith
http://RangeVoting.org  <-- add your endorsement (by clicking
"endorse" as 1st step)
and
math.temple.edu/~wds/homepage/works.html



More information about the Digitalmars-d mailing list