From ripley at stats.ox.ac.uk  Fri Dec  6 16:52:37 2013
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri, 06 Dec 2013 15:52:37 +0000
Subject: [Rd] Matrix memory layout R vs. C
In-Reply-To: <CABtg=KmrrJJhLXFRvY0YmxgKuAP=SkS95qbrD3nmpes-mowGEw@mail.gmail.com>
References: <52A1CF46.7070408@googlemail.com>	<52A1E17B.8090200@gmail.com>	<CABtg=KnYfx_VYvGdmximPm7GSSj-eY-vzdcYKpPPtHDcd1ppXg@mail.gmail.com>	<52A1F06D.3080701@stats.ox.ac.uk>
	<CABtg=KmrrJJhLXFRvY0YmxgKuAP=SkS95qbrD3nmpes-mowGEw@mail.gmail.com>
Message-ID: <52A1F2C5.4080401@stats.ox.ac.uk>

On 06/12/2013 15:49, G?bor Cs?rdi wrote:
> On Fri, Dec 6, 2013 at 10:42 AM, Prof Brian Ripley
> <ripley at stats.ox.ac.uk> wrote:
>> On 06/12/2013 14:42, G?bor Cs?rdi wrote:
>>>
>>> On Fri, Dec 6, 2013 at 9:38 AM, Duncan Murdoch <murdoch.duncan at gmail.com>
>>> wrote:
>>>>
>>>> On 06/12/2013 8:21 AM, Larissa Hauer wrote:
>>>
>>> [...]
>>>>
>>>>
>>>>
>>>> I would not assume that a 2D matrix in C doesn't have gaps in it between
>>>> the
>>>> rows.  Let C treat it as a vector, and write a little macro that does the
>>>> indexing.  For example,
>>>>
>>>> #define INDEX(i,j) (i) + rows*(j)
>>>
>>>
>>> I would make this
>>>
>>> #define INDEX(i,j) ((i) + rows*(j))
>>>
>>> just to be on the safe side.
>>
>>
>> And to be safer on a 64-bit platform
>>
>> #define INDEX(i,j) ((i) + rows*(R_xlen_t)(j))
>>
>> since rows*j might overflow there.
>
> Indeed. Of course this still does not save you from indexing
> out-of-range and integer overflow in the addition.

To a large extent it does.  It means the arithmetic is done in R_xlen_t, 
64-bit on 64-bit machines, and so any legitimate index gets computed 
correctly.


>
> Gabor
>
> [...]
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Matrix memory layout R vs. C
Prof Brian Ripley