mersenneforum.org  

Go Back   mersenneforum.org > Great Internet Mersenne Prime Search > Software

Reply
 
Thread Tools
Old 2007-12-10, 18:38   #1
ShiningArcanine
 
ShiningArcanine's Avatar
 
Dec 2005

9210 Posts
Default Is there any good reason for why GMP does this?

I was browsing gmp's powm_ui.c file in its generic C library. After dechipering it enough to understand part of what it is doing, I noticed the following lines:

Quote:
Originally Posted by GMP
e = el;
count_leading_zeros (c, e);
e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
c = BITS_PER_MP_LIMB - 1 - c;
Is there any good reason why this is not:

Quote:
count_leading_zeros (c, el);
e = (el << ++c); /* shift the exp bits to the left, lose msb */
c = BITS_PER_MP_LIMB - c;
ShiningArcanine is offline   Reply With Quote
Old 2007-12-11, 05:36   #2
jasonp
Tribal Bullet
 
jasonp's Avatar
 
Oct 2004

32·5·79 Posts
Default

My guess is the code is as written because the C standard does not specify what happens to shifts when the shift amount exceeds the bits in a word. For a 32-bit machine, if x is 1 then c is 31 and (x << c) << 1 will be zero, as expected. However, the result of x << (c+1) depends on the processor. x86 machines only look at the bottom 5 bits of the shift amount, so x << 32 becomes x. 32-bit powerPC looks at the bottom 6 bits, so x << 32 is zero.

Variable shifts are a challenge to portable code that many C programmers are not aware of.
jasonp is offline   Reply With Quote
Old 2007-12-11, 15:11   #3
ShiningArcanine
 
ShiningArcanine's Avatar
 
Dec 2005

22×23 Posts
Default

Wow, you are right. I had no idea that the shifts were different on different platforms.

I guess this makes my question why GMP does not use preprocessor definitions to give x86 machines the slow workaround code and other machines the fast code.
ShiningArcanine is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Good air-cooler good enough for overclocked i7-5820K RienS Hardware 17 2014-11-18 22:58
Norwegians seems to love primes for some reason kaffikanne Lounge 8 2010-05-27 20:51
The crime of reason jasonp Soap Box 1 2009-12-20 15:09
My fatalistic(sp?) reason for believing in God. jasong Soap Box 22 2006-05-04 19:26
Another reason for high attrition... guest PrimeNet 27 2004-03-17 13:50

All times are UTC. The time now is 01:34.


Sat Jan 28 01:34:14 UTC 2023 up 162 days, 23:02, 0 users, load averages: 1.60, 1.25, 1.15

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.

≠ ± ∓ ÷ × · − √ ‰ ⊗ ⊕ ⊖ ⊘ ⊙ ≤ ≥ ≦ ≧ ≨ ≩ ≺ ≻ ≼ ≽ ⊏ ⊐ ⊑ ⊒ ² ³ °
∠ ∟ ° ≅ ~ ‖ ⟂ ⫛
≡ ≜ ≈ ∝ ∞ ≪ ≫ ⌊⌋ ⌈⌉ ∘ ∏ ∐ ∑ ∧ ∨ ∩ ∪ ⨀ ⊕ ⊗ 𝖕 𝖖 𝖗 ⊲ ⊳
∅ ∖ ∁ ↦ ↣ ∩ ∪ ⊆ ⊂ ⊄ ⊊ ⊇ ⊃ ⊅ ⊋ ⊖ ∈ ∉ ∋ ∌ ℕ ℤ ℚ ℝ ℂ ℵ ℶ ℷ ℸ 𝓟
¬ ∨ ∧ ⊕ → ← ⇒ ⇐ ⇔ ∀ ∃ ∄ ∴ ∵ ⊤ ⊥ ⊢ ⊨ ⫤ ⊣ … ⋯ ⋮ ⋰ ⋱
∫ ∬ ∭ ∮ ∯ ∰ ∇ ∆ δ ∂ ℱ ℒ ℓ
𝛢𝛼 𝛣𝛽 𝛤𝛾 𝛥𝛿 𝛦𝜀𝜖 𝛧𝜁 𝛨𝜂 𝛩𝜃𝜗 𝛪𝜄 𝛫𝜅 𝛬𝜆 𝛭𝜇 𝛮𝜈 𝛯𝜉 𝛰𝜊 𝛱𝜋 𝛲𝜌 𝛴𝜎𝜍 𝛵𝜏 𝛶𝜐 𝛷𝜙𝜑 𝛸𝜒 𝛹𝜓 𝛺𝜔