Skip to content

Commit c26519e

Browse files
authored
LN_CORE: inline calls to flo and fix (#360)
Also apply macro-flo and macro-fix everywhere in this module to be consistent. Note, that macro-fix and macro-flo are only visible within ln_core.scm, but are much faster. fix and flo are available globally but are much costlier.
1 parent 820adf7 commit c26519e

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

modules/ln_core/color.scm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4444
(define (color-green x) (bitwise-and (arithmetic-shift x -8) #xff))
4545
(define (color-red x) (bitwise-and x #xff))
4646
(define (color-rgba r g b a)
47-
(let* ((fixr (fix r)) (fixg (fix g)) (fixb (fix b)) (fixa (fix a))
47+
(let* ((fixr (macro-fix r)) (fixg (macro-fix g)) (fixb (macro-fix b)) (fixa (macro-fix a))
4848
(clipr (if (fx> fixr 255) 255 (if (fx< fixr 0) 0 fixr)))
4949
(clipg (if (fx> fixg 255) 255 (if (fx< fixg 0) 0 fixg)))
5050
(clipb (if (fx> fixb 255) 255 (if (fx< fixb 0) 0 fixb)))
@@ -275,7 +275,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275275
;; 0 = totally transparent
276276
;; 1 = opaque
277277
(define (color-fade c f) (color-rgba
278-
(color-red c) (color-green c) (color-blue c) (fix (fl* 255. (flo f)))))
278+
(color-red c) (color-green c) (color-blue c) (macro-fix (fl* 255. (macro-flo f)))))
279279

280280

281281
;; oscillating alarm colors - flutterbugs :)

modules/ln_core/floatstring.scm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838
;; formatting of floating numbers
3939

4040
(define (float->string x p)
41-
(let* ((pe (flo (expt 10 (min 10 p))))
42-
(s (number->string (fl/ (flfloor (fl+ (fl* (flo x) pe) 0.5)) pe)))
41+
(let* ((pe (macro-flo (expt 10 (min 10 p))))
42+
(s (number->string (fl/ (flfloor (fl+ (fl* (macro-flo x) pe) 0.5)) pe)))
4343
(sl (string-length s))
4444
(b (substring s 0 1))
4545
(e (substring s (- sl 1) sl)))
@@ -85,6 +85,6 @@ static char *double_to_choppedstring(double val, int precision)
8585
end-of-c-declare
8686
)
8787

88-
(define (float->choppedstring v p) ((c-lambda (double int) char-string "double_to_choppedstring") (flo v) p))
88+
(define (float->choppedstring v p) ((c-lambda (double int) char-string "double_to_choppedstring") (macro-flo v) p))
8989

9090
;; eof

modules/ln_core/flofix.scm

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3535
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3636
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3737
|#
38-
;; type conversions used throughout to prevent FFI type errors
38+
;; type conversions used throughout to prevent FFI type errors
3939

4040
(define fix:fixnum-max-as-flonum (##fixnum->flonum ##max-fixnum))
4141

4242
(define (fix n)
4343
(declare (not safe))
4444
(cond
4545
((##fixnum? n) n)
46-
((##bignum? n) n)
46+
((##bignum? n) n)
4747
((##flonum? n) (if (##fl< n fix:fixnum-max-as-flonum)
4848
(##flonum->fixnum n) (##flonum->exact-int n)))
4949
((##ratnum? n) (##floor n))
@@ -58,4 +58,23 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5858
(else (##exact->inexact n))
5959
))
6060

61+
;; Note that macro-fix and macro-flo are only visible within ln_core.scm,
62+
;; but are much faster. fix and flo are available everywhere but costlier.
63+
(define-macro (macro-fix n)
64+
`(cond
65+
((##fixnum? ,n) ,n)
66+
((##bignum? ,n) ,n)
67+
((##flonum? ,n)
68+
(if (##fl< ,n fix:fixnum-max-as-flonum)
69+
(##flonum->fixnum ,n) (##flonum->exact-int ,n)))
70+
((##ratnum? ,n) (##floor ,n))
71+
(else #f) ;; no complex numbers
72+
))
73+
74+
(define-macro (macro-flo n)
75+
`(cond
76+
((##flonum? ,n) ,n)
77+
((##fixnum? ,n) (##fixnum->flonum ,n))
78+
(else (##exact->inexact ,n))))
79+
6180
;; eof

modules/ln_core/list.scm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7878
(define (list-interpolate lst x0)
7979
(let* ((x (max (min x0 1.) 0.))
8080
(n (- (length lst) 1))
81-
(idx1 (fix (floor (* x n))))
82-
(idx2 (fix (ceiling (* x n))))
81+
(idx1 (macro-fix (floor (* x n))))
82+
(idx2 (macro-fix (ceiling (* x n))))
8383
(v1 (list-ref lst idx1))
8484
(v2 (list-ref lst idx2)))
8585
(+ v1 (* (- (* x n) idx1) (- v2 v1)))))

modules/ln_core/time.scm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,17 +1497,17 @@ end-of-c-declare
14971497
(s (srfi19:time-second t))
14981498
(ns (srfi19:time-nanosecond t))
14991499
(t2 (- s (tm:leap-second-delta s)))
1500-
(tz (if (= (length tz0) 1) (car tz0) (timezone-hours (fix t2))))
1500+
(tz (if (= (length tz0) 1) (car tz0) (timezone-hours (macro-fix t2))))
15011501
(utc (+ 0.0 (* tz -3600.) t2 (* ns 1.0e-9)))
15021502
;; timezone-hours assumes t is UTC, this causes issues if one is DST but not both
1503-
(tz2 (timezone-hours (fix utc)))
1503+
(tz2 (timezone-hours (macro-fix utc)))
15041504
(tzdiff (- tz tz2))
15051505
(utc2 (if (= tzdiff 0) utc (+ utc (* tzdiff 3600.))))
15061506
)
15071507
utc2))
15081508

15091509
(define (seconds->string sec0 fmt . tz0)
1510-
(let* ((tz (if (= (length tz0) 1) (car tz0) (timezone-hours (fix sec0))))
1510+
(let* ((tz (if (= (length tz0) 1) (car tz0) (timezone-hours (macro-fix sec0))))
15111511
(sec (+ sec0 (* tz 3600.)))
15121512
(s (inexact->exact (floor sec)))
15131513
(ns (inexact->exact (floor (* 1.0e9 (- sec s)))))

modules/ln_core/u8vector-compress.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ end-of-c-declare
904904
(define (u8vector-compress inbuf)
905905
(if (u8vector? inbuf)
906906
(let* ((inlen (u8vector-length inbuf))
907-
(outlen (max 66 (fix (* 1.05 inlen))))
907+
(outlen (max 66 (macro-fix (* 1.05 inlen))))
908908
(outbuf (make-u8vector outlen)))
909909
(if (fx< inlen 16) (u8vector-append (u8vector 0) inbuf)
910910
(let ((retval ((c-lambda (scheme-object int scheme-object) int

0 commit comments

Comments
 (0)