9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] ntohl, htonl, &c
@ 2022-05-13 11:50 adr
  2022-05-13 13:11 ` Alex Musolino
  0 siblings, 1 reply; 9+ messages in thread
From: adr @ 2022-05-13 11:50 UTC (permalink / raw)
  To: 9fans

Hi,

I'm getting rid of ape but I need the functions at
/sys/src/ape/lib/bsd/ntohl.c to port some software. These functions
just change endianness. Is there some native similar functions
somebody is aware of? lookman is not helping me here. I don't want to add code
if a similar functionality is already in the system.

Regards,
adr.

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-M2eabe8ea5d75da927a0a19b4
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] ntohl, htonl, &c
  2022-05-13 11:50 [9fans] ntohl, htonl, &c adr
@ 2022-05-13 13:11 ` Alex Musolino
  2022-05-13 14:22   ` ori
  0 siblings, 1 reply; 9+ messages in thread
From: Alex Musolino @ 2022-05-13 13:11 UTC (permalink / raw)
  To: 9fans

> I'm getting rid of ape but I need the functions at
> /sys/src/ape/lib/bsd/ntohl.c to port some software.  These functions
> just change endianness.  Is there some native similar functions
> somebody is aware of?  lookman is not helping me here.  I don't want
> to add code if a similar functionality is already in the system.

No. Those functions are mental.  The Plan 9 way is so simple that
there's no library, everyone just brings their own macros/functions.
The trick is: if you care about the order of bytes then you should be
dealing with a byte array, not a native integral type.

These are from Ori's git implementation though there are other,
similar implementations around the place (e.g. lib9p, venti).  They
do the job perfectly well.

#define GETBE32(b) \
        ((((b)[0] & 0xFFul) << 24) | \
         (((b)[1] & 0xFFul) << 16) | \
         (((b)[2] & 0xFFul) <<  8) | \
         (((b)[3] & 0xFFul) <<  0))

#define PUTBE32(b, n)\
        do{ \
                (b)[0] = (n) >> 24; \
                (b)[1] = (n) >> 16; \
                (b)[2] = (n) >> 8; \
                (b)[3] = (n) >> 0; \
        } while(0)

Little endian versions proceed similarly:

#define GETLE32(b) \
        ((((b)[0] & 0xFFul) <<  0) | \
         (((b)[1] & 0xFFul) <<  8) | \
         (((b)[2] & 0xFFul) << 16) | \
         (((b)[3] & 0xFFul) << 24))

#define PUTLE32(b, n)\
        do{ \
                (b)[0] = (n) >> 0; \
                (b)[1] = (n) >> 8; \
                (b)[2] = (n) >> 16; \
                (b)[3] = (n) >> 24; \
        } while(0)

The 16-bit and 64-bit versions should be obvious.

For porting I'd just use APE, or copy the bits and pieces you need.


------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-M0b8f2ed4b88270bf7dd090ab
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] ntohl, htonl, &c
  2022-05-13 13:11 ` Alex Musolino
@ 2022-05-13 14:22   ` ori
  2022-05-13 15:51     ` adr
  0 siblings, 1 reply; 9+ messages in thread
From: ori @ 2022-05-13 14:22 UTC (permalink / raw)
  To: 9fans

Quoth Alex Musolino <alex@musolino.id.au>:
> > I'm getting rid of ape but I need the functions at
> > /sys/src/ape/lib/bsd/ntohl.c to port some software.  These functions
> > just change endianness.  Is there some native similar functions
> > somebody is aware of?  lookman is not helping me here.  I don't want
> > to add code if a similar functionality is already in the system.
> 
> No. Those functions are mental.  The Plan 9 way is so simple that
> there's no library, everyone just brings their own macros/functions.
> The trick is: if you care about the order of bytes then you should be
> dealing with a byte array, not a native integral type.

see also:

https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html



------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-M11b2b34d2e1336b13546b09f
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] ntohl, htonl, &c
  2022-05-13 14:22   ` ori
@ 2022-05-13 15:51     ` adr
  2022-05-13 16:42       ` adr
  2022-05-13 21:23       ` ori
  0 siblings, 2 replies; 9+ messages in thread
From: adr @ 2022-05-13 15:51 UTC (permalink / raw)
  To: 9fans

On Fri, 13 May 2022, ori@eigenstate.org wrote:

> Date: Fri, 13 May 2022 10:22:13 -0400
> From: ori@eigenstate.org
> Reply-To: 9fans <9fans@9fans.net>
> To: 9fans@9fans.net
> Subject: Re: [9fans] ntohl, htonl, &c
> 
> Quoth Alex Musolino <alex@musolino.id.au>:
>>> I'm getting rid of ape but I need the functions at
>>> /sys/src/ape/lib/bsd/ntohl.c to port some software.  These functions
>>> just change endianness.  Is there some native similar functions
>>> somebody is aware of?  lookman is not helping me here.  I don't want
>>> to add code if a similar functionality is already in the system.
>>
>> No. Those functions are mental.  The Plan 9 way is so simple that
>> there's no library, everyone just brings their own macros/functions.
>> The trick is: if you care about the order of bytes then you should be
>> dealing with a byte array, not a native integral type.
>
> see also:
>
> https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html

Oh, I agree with that and I think this was a key in the design of
9p. I wasn't asking for the existence of those functions elsewhere,
but for some common code to change endianness of some data structure,
stream, etc. Not because is hard to do, just to not repeat the same
code again and again. Every format of image, audio, etc store
data in a determinded endianness, so I imagined that some functions
or macros could be defined already somewhere.

Regards,
adr.

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-Mf37677730df2f6e32e400995
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] ntohl, htonl, &c
  2022-05-13 15:51     ` adr
@ 2022-05-13 16:42       ` adr
  2022-05-13 21:23       ` ori
  1 sibling, 0 replies; 9+ messages in thread
From: adr @ 2022-05-13 16:42 UTC (permalink / raw)
  To: 9fans

> data in a determinded endianness, so I imagined that some functions
I mean "certain" endianness, ugly typo.

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-M6768de0bfccdc64669e77a6f
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] ntohl, htonl, &c
  2022-05-13 15:51     ` adr
  2022-05-13 16:42       ` adr
@ 2022-05-13 21:23       ` ori
  2022-05-13 22:29         ` Steve Simon
  2022-05-14 10:45         ` adr
  1 sibling, 2 replies; 9+ messages in thread
From: ori @ 2022-05-13 21:23 UTC (permalink / raw)
  To: 9fans

Quoth adr <adr@SDF.ORG>:
> so I imagined that some functions
> or macros could be defined already somewhere.

They exist in fcall.h -- see GBIT/PBIT macros.

I wouldn't object mych to putting them in libc, possibly
with better names and an accompanying patch to move
the rest of the code over to use them.


------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-Mdebff5dbd75d096557660c69
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] ntohl, htonl, &c
  2022-05-13 21:23       ` ori
@ 2022-05-13 22:29         ` Steve Simon
  2022-05-13 22:37           ` ori
  2022-05-14 10:45         ` adr
  1 sibling, 1 reply; 9+ messages in thread
From: Steve Simon @ 2022-05-13 22:29 UTC (permalink / raw)
  To: 9fans

ntohl in libc?
say its not so?

-Steve

> On 13 May 2022, at 10:23 pm, ori@eigenstate.org wrote:
> 
> Quoth adr <adr@SDF.ORG>:
>> so I imagined that some functions
>> or macros could be defined already somewhere.
> 
> They exist in fcall.h -- see GBIT/PBIT macros.
> 
> I wouldn't object mych to putting them in libc, possibly
> with better names and an accompanying patch to move
> the rest of the code over to use them.
> 

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-M89a982028d4683e7593a7b79
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] ntohl, htonl, &c
  2022-05-13 22:29         ` Steve Simon
@ 2022-05-13 22:37           ` ori
  0 siblings, 0 replies; 9+ messages in thread
From: ori @ 2022-05-13 22:37 UTC (permalink / raw)
  To: 9fans

Quoth Steve Simon <steve@quintile.net>:
> ntohl in libc?
> say its not so?

definitely not the byte swapping versions.

the unpacking of known bytes with known endianness
into an integer, though, is a sensible API that
many programs copy and paste; it's trivial to write,
but it's also common.

I don't feel strongly that it needs to be there, but
I dont' object to it either.


------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-M67c8b4f012da468975a17d1e
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] ntohl, htonl, &c
  2022-05-13 21:23       ` ori
  2022-05-13 22:29         ` Steve Simon
@ 2022-05-14 10:45         ` adr
  1 sibling, 0 replies; 9+ messages in thread
From: adr @ 2022-05-14 10:45 UTC (permalink / raw)
  To: 9fans

On Fri, 13 May 2022, ori@eigenstate.org wrote:
> They exist in fcall.h -- see GBIT/PBIT macros.

Thanks.

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tbf58310b561cd180-Mb43211fb858857f5155bf1cb
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-05-14 10:45 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-13 11:50 [9fans] ntohl, htonl, &c adr
2022-05-13 13:11 ` Alex Musolino
2022-05-13 14:22   ` ori
2022-05-13 15:51     ` adr
2022-05-13 16:42       ` adr
2022-05-13 21:23       ` ori
2022-05-13 22:29         ` Steve Simon
2022-05-13 22:37           ` ori
2022-05-14 10:45         ` adr

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