Thanks,
Nate
I would say the easiest is direct translation either the C source,
or the pseudocode in, for example, the wikipedia page.
You have to make a few assumptions that the Fortran standard does
not make, such as the existence of (at least) a 32 bit INTEGER type.
It is much easier if you can assume twos complement arithmetic,
and that carries are ignored (that is, wrap on overflow).
The rotate operation can be done with combinations of left shift,
right shift, AND and OR.
-- glen
> The rotate operation can be done with combinations of left shift,
> right shift, AND and OR.
Is this the same as ishftc()? I think that is a standard intrinsic,
and has been so since 1979 if you count the milstd standard, and
since f90 otherwise.
$.02 -Ron Shepard
It is, with the appropriate third argument. Well, since Fortran
makes no guarantee on the maximum bit width of INTEGER types,
it is hard to use ishftc() without specifying the width.
SHA1 uses a 32 bit rotation in a few places.
In one check, it seems that gfortran generates IOR, IAND, IEOR,
and ISHFT inline, but ISHFTC with a subroutine call. Two calls
to ISHFT, in that case, is likely faster than ISHFTC.
-- glen
> In one check, it seems that gfortran generates IOR, IAND, IEOR,
> and ISHFT inline, but ISHFTC with a subroutine call. Two calls
> to ISHFT, in that case, is likely faster than ISHFTC.
>
Source code?
Given the right source gfortran won't produce anything except
the answer (hint, constant folding).
--
steve
! test the generated code for some simple functions
integer i,j
read(*,*) i,j
write(*,*) iand(i,j),ior(i,j),ieor(i,j),ishft(i,-4),ishftc(i,4,32)
end
> Given the right source gfortran won't produce anything except
> the answer (hint, constant folding).
Yes, to avoid that I read in i and j. I used constants for
the second and third argument of ishftc(), as that is a common use.
The problem of benchmarks and constant folding is an old one.
-- glen
Dick Hendrickson
> True, there's no guarantee. But I think you can use the various
> inquiry functions to force either a compile time or run time abort
> if the integer size isn't 32 bits if it is important to the algorithm.
Most algorithms that use bitwise logical operations only need a wide
enough data type. Circular shift usually needs the exact width.
Since ISHFT guarantees to shift in zeros, you can do the equivalent
of circular shift with two ISHFT and one IOR. That is likely
faster than the overhead of a subroutine call.
-- glen
That's also the kind of micro-optimization that I recommend people avoid
in most situations. It is completely compiler dependent whether it is
even an optimization at all. If you have observed that there is a
consistently faster way than what a compiler does on its own, perhaps a
performance bug report to the compiler developers would be in order.
Hacking around a performance problem in one compiler is reasonably
likely to slow things down in another (or even a subsequent release of
the same compiler). That kind of thing happens regularly.
Unless you can observe (not just predict) the performance difference in
an actual application, and also see that the performance difference is
important, I recommend coding for clarity and simplicity. It often ends
up faster in the long run anyway (even more so when you add
consideration of debugging time). This is classic advice from
prehistoric days, but it still applies - if anything more so now than
then.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain