forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Please add new intrinsic functions that use x64 LEADZ,TRAILZ

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Suggestions
View previous topic :: View next topic  
Author Message
mecej4



Joined: 31 Oct 2006
Posts: 1923

PostPosted: Thu Nov 13, 2025 3:26 pm    Post subject: Please add new intrinsic functions that use x64 LEADZ,TRAILZ Reply with quote

Paul, please consider adding new intrinsic functions that make the X64 instructions LEADZ, TRAILZ and POPCNT available to the Fortran programmer. There are several situations where such a function call can replace an entire DO loop. For example, given an integer variable v, if we wish to divide by the largest power of 2 possible, we could do

nz=trailz(v)
if(nz > 0) v = ishft(v,nz)

instead of writing a loop:

WHILE (MOD(v,2).eq.0) DO
v = v/2
END DO

Thank you.
Of course, these new intrinsics are feasible only when targeting X64.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8290
Location: Salford, UK

PostPosted: Fri Nov 14, 2025 8:30 am    Post subject: Reply with quote

mecej4
Thank you for the feedback. I will add these to the wish list.
Back to top
View user's profile Send private message AIM Address
mecej4



Joined: 31 Oct 2006
Posts: 1923

PostPosted: Fri Nov 14, 2025 2:57 pm    Post subject: Reply with quote

Thanks, Paul.
The trailz function is also useful in deciding whether a four digit year is a leap year.
Code:

   integer:: yyyy, ntz
   logical :: cent, leap

    ntz  = trailz(yyyy)
    cent = mod(yyyy,100).eq.0
    if(cent)then
       leap = ntz > 3   !yrs divisible by 400
    else
       leap = ntz > 1   ! yrs divisible by 4
    endif


Surprising information from the past:
Quote:
45 BC, 42 BC, 39 BC, 36 BC, 33 BC, 30 BC, 27 BC, 24 BC, 21 BC, 18 BC, 15 BC, 12 BC, 9 BC, 8 AD, 12 AD were regarded as leap years before the advent of the Gregorian calendar. See
https://astro101.wwu.edu/a101_leapyear.html
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1923

PostPosted: Sun Nov 16, 2025 11:52 am    Post subject: Suport for TRAILZ in other compilers Reply with quote

Gfortran, Intel Fortran, Absoft Fortran and NAG Fortran provide TRAILZ as an intrinsic function.

Another application for TRAILZ is in rapid calculation of Collatz sequences. I am curious to know if anyone on this forum has toyed with the Collatz Conjecture.


Last edited by mecej4 on Sun Nov 16, 2025 3:49 pm; edited 1 time in total
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 858
Location: Lanarkshire, Scotland.

PostPosted: Sun Nov 16, 2025 2:49 pm    Post subject: Reply with quote

The functions TRAILZ, LEADZ, and POPCNT (and also POPPAR) are listed as intrinsic functions in the 2008 standard.
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1923

PostPosted: Sun Nov 16, 2025 3:34 pm    Post subject: Reply with quote

Thanks, Kenneth, and I have no excuse for not being aware of those additions to F2008 in 2025, other than not running into an instance of one of them being beneficial in a Fortran program.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Suggestions All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group