public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* SRFI-60
@ 2014-02-18  6:53 Jamison Hope
  2014-02-18  7:12 ` SRFI-60 Per Bothner
  0 siblings, 1 reply; 10+ messages in thread
From: Jamison Hope @ 2014-02-18  6:53 UTC (permalink / raw)
  To: kawa@sourceware.org list

[-- Attachment #1: Type: text/plain, Size: 1166 bytes --]

I saw that SRFI-60 was almost there, so I went ahead and implemented the rest of it.  The functions all fall into one of these categories:

1. already provided as a builtin with the same name, so nothing to do

2. already provided as a builtin with a different name, so just add an alias

3. functionality is provided by a similar-name builtin but arguments are reordered or signature otherwise changes slightly, so define a wrapper function and then call the builtin

   3.a. name/signature conflicts with a builtin (this happens exactly once, with logbit?)

4. new function, so implement it


Since there are argument order differences compared to existing numbers.scm procedures, I figure this one should probably require an explicit require/import, so it can go in the gnu.kawa.slib package next to srfi1.scm.  Even so, I wonder whether it might be a good idea to remove the builtin logbit? (which is just an alias for bitwise-set-bit? anyway).

I can make the require.java/ImportFromLibrary.java changes, but since this is a new file I wanted to see if I was on the right track first.

--
Jamison Hope
The PTR Group
www.theptrgroup.com



[-- Attachment #2: srfi60.scm --]
[-- Type: application/octet-stream, Size: 3210 bytes --]

;; SRFI-60 implementation for Kawa (almost everything is already built
;; in, but some of the names differ slightly).
;; Copyright (C) 2014 by Jamison Hope.  The implementations of
;; integer->list, list->integer, and booleans->integer were taken with
;; slight modifications from the reference implementation of SRFI-60,
;; which is copyright (C) 1991, 1993, 2001, 2003, 2005 Aubrey Jaffer
;
;Permission to copy this software, to modify it, to redistribute it,
;to distribute modified versions, and to use it for any purpose is
;granted, subject to the following restrictions and understandings.
;
;1.  Any copy made of this software must include this copyright notice
;in full.
;
;2.  I have made no warranty or representation that the operation of
;this software will be error-free, and I am under no obligation to
;provide any services, by way of maintenance, update, or otherwise.
;
;3.  In conjunction with products arising from the use of this
;material, there shall be no use of my name in any advertising,
;promotional, or sales literature without prior written consent in
;each case.

(module-compile-options warn-unknown-member: #t)

;;; These procedures are already Kawa built-ins and do not need to be
;;; defined here: logand/bitwise-and, logior/bitwise-ior,
;;; logxor/bitwise-xor, lognot/bitwise-not, bitwise-if, logtest,
;;; logcount, integer-length, and ash/arithmetic-shift.

;;; These procedures alias functionality provided by built-ins with
;;; differing names:

(define bitwise-merge       bitwise-if)
(define any-bits-set?       logtest)
(define bit-count           logcount)
(define log2-binary-factors bitwise-first-bit-set)
(define first-set-bit       bitwise-first-bit-set)
(define bit-field           bitwise-bit-field)
(define reverse-bit-field   bitwise-reverse-bit-field)

;;; These procedures are similar to built-ins but with arguments
;;; reordered:

;; Note that this shadows the built-in logbit? procedure, which is an
;; alias for bitwise-bit-set?.
(define (logbit? index::int n::integer) ::boolean
  (bitwise-bit-set? n index))
(define bit-set? logbit?)

(define (copy-bit-field to::integer from::integer start::int end::int)
  ::integer
  (bitwise-copy-bit-field to start end from))

(define (rotate-bit-field n::integer count::int start::int end::int)
  ::integer
  (bitwise-rotate-bit-field n start end count))

;;; This procedure has a slightly different signature compared to the
;;; built-in bitwise-copy-bit: the first two arguments are swapped and
;;; the last is a boolean instead of an int
(define (copy-bit index::int from::integer bit::boolean)
  ::integer
  (bitwise-copy-bit from index (if bit 1 0)))

;;; These procedures are entirely new, with implementations derived
;;; from the SRFI-60 reference.
(define (integer->list k::integer #!optional (len ::int (integer-length k)))
  ::list
  (do ((idx ::int (- len 1) (- idx 1))
       (k ::integer k (ash k -1))
       (lst ::list '() (cons (odd? k) lst)))
      ((< idx 0) lst)))

(define (list->integer bools::list) ::integer
  (do ((bs bools (cdr bs))
       (acc ::integer 0 (if (car bs) (+ acc acc 1) (+ acc acc))))
      ((null? bs) acc)))

(define (booleans->integer . bools)
  (list->integer bools))

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

end of thread, other threads:[~2014-02-26  7:13 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-18  6:53 SRFI-60 Jamison Hope
2014-02-18  7:12 ` SRFI-60 Per Bothner
2014-02-18 16:30   ` SRFI-60 Jamison Hope
2014-02-20  7:54     ` SRFI-60 Per Bothner
2014-02-20 13:47       ` SRFI-60 Jamison Hope
2014-02-20 13:51         ` [PATCH] Was: SRFI-60 Jamison Hope
2014-02-21 17:54           ` Per Bothner
2014-02-22  0:41             ` Jamison Hope
2014-02-22  8:04             ` Jamison Hope
2014-02-26  7:13               ` Per Bothner

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 read-only IMAP folder(s) and NNTP newsgroup(s).