mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Glenn Weinberg <glenn@cognitive-electronics.com>
To: Rich Felker <dalias@libc.org>
Cc: musl@lists.openwall.com
Subject: Re: Further limits/stdint issues
Date: Wed, 3 Dec 2014 16:08:14 -0500	[thread overview]
Message-ID: <5B0BDFB9-8AF8-457F-AF13-52C66A8E0253@cognitive-electronics.com> (raw)
In-Reply-To: <20141203170136.GL4574@brightrain.aerifal.cx>


> On Dec 3, 2014, at 12:01 PM, Rich Felker <dalias@libc.org> wrote:
> 
> On Wed, Dec 03, 2014 at 11:34:02AM -0500, Glenn Weinberg wrote:
>> 
>> The fundamental answer is that our architecture does not define any
>> 8, 16 or 32-bit integer arithmetic operations. Integer add,
>> subtract, divide and remainder take 64-bit operands and produce a
>> 64-bit result; integer multiply, multiply-add and multiply-subtract
>> take 64-bit operands and produce a 128-bit result.
> 
> Thanks for the explanation. I don't see how this makes smaller
> operations "slow" though. The compiler will just generate the 64-bit
> operations and ignore the junk in the upper bits of the register,
> which will eventually get discarded when storing back to memory.

If the compiler doesn't limit intermediate results to 32 bits it's not preserving unsigned wraparound overflow semantics as required by the C standard.  Consider the following 32-bit pseudo assembly language implementing (0xffffffff + 2) / 2:

	r1 = 0xffffffff	// 0xffffffff
	r2 = 2		// 0x00000002
	r3 = r1 + r2	// 0x00000001 (Wraparound overflow)
	r4 = r3 / r2	// 0x00000000
	Store r4	// 0x00000000

Now consider it in 64-bit (without adding additional code to limit the value in r3):

	r1 = 0xffffffff	// 0x00000000ffffffff
	r2 = 2		// 0x0000000000000002
	r3 = r1 + r2	// 0x0000000100000001 (No overflow, 64-bit)
	r4 = r3 / r2	// 0x0000000080000000
	Store low32(r4)	// 0x80000000

> Is it
> just the cost of that store you're worried about? Being that there
> will be lots of 32-bit stores even if the fast32 type is 64-bit (e.g.
> everything that's plain "int" and things which are explicitly
> [u]int32_t), I would think you'd want/need 32-bit store to be fast
> anyway.


If it were free would we add 8, 16 and 32-bit arithmetic operations and fast 8, 16 and 32-bit stores to the hardware architecture?  Sure.  But it's not free, the architecture is what it is, and that is precisely the reason why the "fast" types should remain architecture-specific. 

	Regards,

	Glenn
--
Glenn Weinberg
Vice President, Product
Cognitive Electronics, Inc.
www.cognitive-electronics.com
glenn@cognitive-electronics.com





  reply	other threads:[~2014-12-03 21:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-03  1:02 Rich Felker
2014-12-03  4:10 ` Glenn Weinberg
2014-12-03  5:03   ` Rich Felker
2014-12-03 16:34     ` Glenn Weinberg
2014-12-03 17:01       ` Rich Felker
2014-12-03 21:08         ` Glenn Weinberg [this message]
2014-12-03 10:38 ` Jens Gustedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5B0BDFB9-8AF8-457F-AF13-52C66A8E0253@cognitive-electronics.com \
    --to=glenn@cognitive-electronics.com \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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