9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Alex Musolino <alex@musolino.id.au>
To: 9fans@9fans.net
Subject: Re: [9fans] ntohl, htonl, &c
Date: Fri, 13 May 2022 22:41:47 +0930	[thread overview]
Message-ID: <13F82633F6415657513741E64A249C22@musolino.id.au> (raw)
In-Reply-To: <929c3e53-53bf-29c1-d9ef-83d6949d7f57@SDF.ORG>

> 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

  reply	other threads:[~2022-05-13 13:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-13 11:50 adr
2022-05-13 13:11 ` Alex Musolino [this message]
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

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=13F82633F6415657513741E64A249C22@musolino.id.au \
    --to=alex@musolino.id.au \
    --cc=9fans@9fans.net \
    /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.
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).