wanted: efficient endian swap

Bug #310191 reported by Nikodemus Siivola
2
Affects Status Importance Assigned to Milestone
SBCL
Confirmed
Wishlist
Unassigned

Bug Description

Endian-swapping binary data of all sorts would be useful. On x86 there is even a specialized instruction
(not sure how CISC-slow it is, though) BSWAP.

Either in SB-EXT, or as an SB-ROTATE-BYTE-style contrib.

Tags: feature
Changed in sbcl:
importance: Undecided → Wishlist
status: New → Confirmed
Revision history for this message
Nathan Froyd (froydnj) wrote :

I think to make this really useful/more applicable, you need a version that operates not only on bare integer values, but also loads endian-reversed data from arrays/saps. Then you can at least let PPC get into the act and you even have the option of specializing code for Atom.

Revision history for this message
Nikodemus Siivola (nikodemus) wrote :

;;; Here's a quick version for x86-64 as a discussion point re. API.
;;;
;;; Specifying the word-width in the API is an issue.
;;;
;;; It should also be able to byte-swap vectors of unsigned 8 using
;;; any byte-width upto, dunno, 128 bits?

(in-package :sb-c)

(defknown byte-swap (word) word
    (unsafe))

(in-package :sb-vm)

(define-vop (fast-byte-swap fast-safe-arith-op)
  (:translate sb-c::byte-swap)
  (:args (x :scs (unsigned-reg) :target res))
  (:arg-types unsigned-num)
  (:results (res :scs (unsigned-reg)))
  (:result-types unsigned-num)
  (:generator 1
   (move res x)
   (inst bswap res)))

(defun byte-swap-vector (vector)
  (declare (type (simple-array (unsigned-byte 64) (*)) vector))
  (dotimes (i (length vector))
    (setf (aref vector i) (sb-c::byte-swap (aref vector i))))
  vector)

Revision history for this message
Nikodemus Siivola (nikodemus) wrote :

"It should also be able to byte-swap vectors of unsigned 8 using any byte-width upto, dunno, 128 bits?"

...what didn't come out right.

What I meant was that a vector of unsigned 8 should be byte-swappable as if it was a vector of unsigned 16, 32, 64, etc.

Revision history for this message
Stas Boukarev (stassats) wrote :

FWIW, here is a portable swap-bytes with VOPS for x86 and x86-64 SBCL: http://github.com/stassats/swap-bytes

Revision history for this message
Douglas Katzman (dougk) wrote :

Maybe we could bring the code from 'nibbles' in as a contrib? Then subsequent versions of 'nibbles' can just do (REQUIRE :sb-nibbles) and call it done.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.