From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1894 invoked by alias); 23 Feb 2015 19:39:34 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 1877 invoked by uid 89); 23 Feb 2015 19:39:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: smtp.gentoo.org Received: from smtp.gentoo.org (HELO smtp.gentoo.org) (140.211.166.183) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 23 Feb 2015 19:39:32 +0000 Received: from vapier (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with SMTP id C573E34092E; Mon, 23 Feb 2015 19:39:27 +0000 (UTC) Date: Mon, 23 Feb 2015 19:39:00 -0000 From: Mike Frysinger To: Jiri Gaisler Cc: gdb-patches@sourceware.org Subject: Re: [PATCH v2 10/22] sim/erc32: Switched emulated memory to host endian order. Message-ID: <20150223193927.GC13523@vapier> Mail-Followup-To: Jiri Gaisler , gdb-patches@sourceware.org References: <1424385100-15397-1-git-send-email-jiri@gaisler.se> <1424385100-15397-11-git-send-email-jiri@gaisler.se> <20150222205132.GA31422@vapier> <54EA61F2.6080303@gaisler.se> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ZmUaFz6apKcXQszQ" Content-Disposition: inline In-Reply-To: <54EA61F2.6080303@gaisler.se> X-IsSubscribed: yes X-SW-Source: 2015-02/txt/msg00676.txt.bz2 --ZmUaFz6apKcXQszQ Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-length: 2759 On 23 Feb 2015 00:10, Jiri Gaisler wrote: > On 02/22/2015 09:51 PM, Mike Frysinger wrote: > > On 19 Feb 2015 23:31, Jiri Gaisler wrote: > >> + *((unsigned short *) &(mem[waddr])) =3D *data & 0x0ffff; > >=20 > > this violates strict aliasing. you can't cast the RHS side like this. = it also=20 > > violates alignment since the buffer is passed in as unsigned char *. err, i obviously meant LHS there > I don't fully agree on this. *mem holds the pointer to romb or ramb, > which are defined as unsigned char arrays. However, their definition is > is preceded with an integer define: >=20 > static uint32 mem_blockprot; /* RAM block write protection enabled */ > static unsigned char romb[ROM_SZ]; > static unsigned char ramb[RAM_END - RAM_START]; >=20 > This means that romb and ramb are aligned on a 4-byte boundary > on systems where this matters (SPARC, ARM). When casting to short, > waddr is always aligned on 2, when casting to integer waddr is > always aligned on 4. So the casting really works without getting > an alignment error. Can I rather document this instead of using > a slower memcpy()? In cpu simulation, performance is essential and > every (host) instruction counts. afaik, nowhere are you guaranteed that the memory layout will match the dec= l=20 order of your code. there's no reason gcc/ld couldn't reorder those object= s=20 as long as the declared alignment is maintained. if you wanted to do that,= you=20 would have to create a union instead like: static union { unsigned char u8[ROM_SZ]; uint16_t u16[ROM_SZ / 2]; uint32_t u32[ROM_SZ / 4]; } romb; and then pass in romb.u8 or romb.u16 or whatever. even if the memory order was guaranteed, gcc cannot infer that level of=20 alignment. it would (rightly) complain that strict aliasing was being viol= ated. > > you should use memcpy() instead. on systems where unaligned access are= OK, gcc=20 > > should optimize it down to a few load/stores anyways. >=20 > memcpy() does have some overhead compared to a single store ... i think you misread what i said. on hosts, like x86_64, there is no call t= o=20 memcpy(). gcc will replace it with the exact asm insns required. in this = case=20 (a 16bit store), that's what you'll get. $ cat test.c int main(int argc, char *argv[]) { memcpy(argv[0], &argc, 4); puts(argv[0]); return 0; } $ gcc -O3 -S -o - test.c main: .cfi_startproc subq $8, %rsp .cfi_def_cfa_offset 16 /* these two insns are the memcpy */ movq (%rsi), %rax movl %edi, (%rax) /* here is the call to puts */ movq (%rsi), %rdi call puts xorl %eax, %eax addq $8, %rsp .cfi_def_cfa_offset 8 ret .cfi_endproc -mike --ZmUaFz6apKcXQszQ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-length: 819 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJU64HvAAoJEEFjO5/oN/WB7l4QAMiKRWOksGZovJ01If7VR7tF 5d8HbcwCpYIgJVrxPxGBkFjCt4r3HSby9IVwIO6idEMn79ZFbmpNXT24FWf+DBm0 EfBaXRYFp2/Z4TgKCErEwdxJNF1mfEi+ZSxdSjv+NcbOnNwZws/oovliKTqmJSTL nZSGrRaS8csT/nBAbtmZ+Us5B/wM7zG+aD63/rqDXjVQKjEHdZYjPYmilhDBjJdo ZZ0OxMIcAUYSFED2izWuTPs2NBRJDUhlfwcMLszJv8pTp9zD06iocXEA1P9sb2Xe e8odMmle5mbb3P/qHcNv+e88pbJQg5dvWHly4uhcP5nBZyWz5TsPJFAt51m1ABkn vkBo34dcaNlYJvInXbEOMjhjcXjUqbom0BHiRIB3JJuBdzscqkxYfhOOnjWgSbJB 6rPszjNqb9oSIig88STXqp/tTQloaPugogEHtKzm2Ctrc6kjPvmf+6CKTXyf/Kdj +B074QnSsjx3wHWX+womOLeid1P54mWv+HUQ+ZeFfHfyeCZ5S6jN5oDEkKGLFStH IFpiqNeILFEICuzEvWRKYMIHz5u2Sa0Mr/yCbI1F5MUUoiDcfq7BhXTsE8tITajx NJbJbB1rBwPu+TAL9JBbeUg+oi2SW2o4F5BB38unEPRJF3WUbwOPU0/t5I5EKFn1 y0sBMpf7kSyrzHjjBEAI =Ifa/ -----END PGP SIGNATURE----- --ZmUaFz6apKcXQszQ--