Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #123593

Re: Recasting data as long ints and chars

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Recasting data as long ints and chars
Date 2017-11-29 01:07 +0000
Organization A noiseless patient Spider
Message-ID <87efohsxin.fsf@bsb.me.uk> (permalink)
References (2 earlier) <oveo59$aa0$1@dont-email.me> <87fu90mw6q.fsf@bsb.me.uk> <ovh906$kj4$1@dont-email.me> <87d143k9e1.fsf@bsb.me.uk> <ovk5ev$ee3$1@dont-email.me>

Show all headers | View raw


James Harris <james.harris.1@gmail.com> writes:

> On 27/11/2017 15:51, Ben Bacarisse wrote:
>> James Harris <james.harris.1@gmail.com> writes:
<snip>
>>> When I speak of "portable" I am thinking that the source would be the
>>> same. The offsets would have to be determined at compile time (or
>>> later). It's easy to do this non-portably. For example, a non-portable
>>> way to carry out the sort on 32-bit integers might define an array of
>>> offsets such as
>>>
>>>    int i32_offsets[] = {3, 2, 1, 0, -1};
>>>
>>> The -1 indicates the end of the list.
>>
>> Ah, so the problem is generating the list at compile time?  That's
>> tricky without a build-phase -- i.e. a program that runs "at compile
>> time".
>
> Compile time would be ideal, if feasible.

I don't think you can do it "at compile time" rather than "at
configuration time".

<snip>
>> You'll have to tell me if I've got what you mean!
>
> I think so. And I see that picking code in a #if block can be done if
> suitable tests can be devised.

That's was not what I was suggesting.  But I'm not sure what I was
suggesting will be what you want anyway.

> But I am not sure that such an approach
> would scale well and I wonder if there would be a more flexible
> way. To illustrate, consider
>
>   unsigned U_ORDER = 0x00010203;
>   unsigned char *p = (unsigned char *) &U_ORDER;
>
> The idea would be that p[0] through p[3] would give the offsets of the
> bytes of an unsigned int in descending order of significance, and that
> it would work irrespective of endianness - big, little or anything
> else. But it has problems: it assumes sizeof(unsigned) is 4 and that
> CHAR_BIT is 8. Hence it is not portable.

This is run-time code, yes?  If you are prepared to do it at run-time
then you don't need to assume the size of anything.  You can start with

  unsigned nb = 0;
  unsigned U_ORDER = 0;

and loop setting

  (U_ORDER << CHAR_BIT) | nb++

sizeof (unsigned) times.  And everywhere you are tempted to use p[0] to
p[3] use p[0] to p[sizeof U_ORDER - 1] instead.

<snip>
-- 
Ben.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Recasting data as long ints and chars James Harris <james.harris.1@gmail.com> - 2017-11-19 21:11 +0000
  Re: Recasting data as long ints and chars Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-23 00:50 +0000
    Re: Recasting data as long ints and chars James Harris <james.harris.1@gmail.com> - 2017-11-26 15:55 +0000
      Re: Recasting data as long ints and chars Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-26 23:55 +0000
        Re: Recasting data as long ints and chars Spiros Bousbouras <spibou@gmail.com> - 2017-11-27 06:43 +0000
          Re: Recasting data as long ints and chars "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-27 09:26 +0100
            Re: Recasting data as long ints and chars Spiros Bousbouras <spibou@gmail.com> - 2017-11-27 08:50 +0000
            Re: Recasting data as long ints and chars "James R. Kuyper" <jameskuyper@verizon.net> - 2017-11-27 10:35 -0500
              Re: Recasting data as long ints and chars supercat@casperkitty.com - 2017-11-27 08:00 -0800
              Re: Recasting data as long ints and chars "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-27 23:49 +0100
                Re: Recasting data as long ints and chars jameskuyper@verizon.net - 2017-11-27 15:56 -0800
            Re: Recasting data as long ints and chars herrmannsfeldt@gmail.com - 2017-12-15 12:45 -0800
          Re: Recasting data as long ints and chars Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-27 15:08 +0000
        Re: Recasting data as long ints and chars James Harris <james.harris.1@gmail.com> - 2017-11-27 14:55 +0000
          Re: Recasting data as long ints and chars Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-27 15:51 +0000
            Re: Recasting data as long ints and chars James Harris <james.harris.1@gmail.com> - 2017-11-28 17:13 +0000
              Re: Recasting data as long ints and chars Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-29 01:07 +0000
                Re: Recasting data as long ints and chars Spiros Bousbouras <spibou@gmail.com> - 2017-11-29 18:59 +0000
                Re: Recasting data as long ints and chars Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-30 00:53 +0000
                Re: Recasting data as long ints and chars Keith Thompson <kst-u@mib.org> - 2017-11-29 17:01 -0800
                Re: Recasting data as long ints and chars Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-30 11:40 +0000
                Re: Recasting data as long ints and chars James Harris <james.harris.1@gmail.com> - 2017-11-30 19:53 +0000
              Re: Recasting data as long ints and chars Spiros Bousbouras <spibou@gmail.com> - 2017-11-29 18:37 +0000
                Re: Recasting data as long ints and chars James Harris <james.harris.1@gmail.com> - 2017-11-30 22:38 +0000
                Re: Recasting data as long ints and chars Spiros Bousbouras <spibou@gmail.com> - 2017-12-07 05:06 +0000
                Re: Recasting data as long ints and chars supercat@casperkitty.com - 2017-12-07 07:51 -0800
                Re: Recasting data as long ints and chars James Harris <james.harris.1@gmail.com> - 2017-12-08 23:14 +0000
                Re: Recasting data as long ints and chars supercat@casperkitty.com - 2017-12-12 16:19 -0800
                Re: Recasting data as long ints and chars Keith Thompson <kst-u@mib.org> - 2017-12-12 17:10 -0800
                Re: Recasting data as long ints and chars supercat@casperkitty.com - 2017-12-13 09:02 -0800
                Re: Recasting data as long ints and chars herrmannsfeldt@gmail.com - 2017-12-15 12:39 -0800
                Re: Recasting data as long ints and chars Robert Wessel <robertwessel2@yahoo.com> - 2017-12-15 17:39 -0600
                Re: Recasting data as long ints and chars herrmannsfeldt@gmail.com - 2017-12-15 15:52 -0800
                Re: Recasting data as long ints and chars Robert Wessel <robertwessel2@yahoo.com> - 2017-12-16 00:35 -0600
                Re: Recasting data as long ints and chars herrmannsfeldt@gmail.com - 2017-12-17 14:02 -0800
                Re: Recasting data as long ints and chars "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2017-12-17 15:15 -0800
                Re: Recasting data as long ints and chars herrmannsfeldt@gmail.com - 2017-12-17 18:47 -0800
                Re: Recasting data as long ints and chars herrmannsfeldt@gmail.com - 2017-12-17 14:09 -0800
                Re: Recasting data as long ints and chars herrmannsfeldt@gmail.com - 2017-12-17 14:19 -0800
                Re: Recasting data as long ints and chars herrmannsfeldt@gmail.com - 2017-12-17 14:45 -0800
                Re: Recasting data as long ints and chars Spiros Bousbouras <spibou@gmail.com> - 2017-12-13 19:13 +0000
                Re: Recasting data as long ints and chars James Harris <james.harris.1@gmail.com> - 2018-02-10 05:46 +0000

csiph-web