cmpct.lsp 7.03 KB
Newer Older
1
2
3
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:

jgarcia's avatar
jgarcia committed
4
;;;;
jjgarcia's avatar
jjgarcia committed
5
6
7
8
9
10
11
12
13
14
15
16
17
;;;; CMPCT --  Optimizer for several constant values

;;;;  Copyright (c) 2003, Juan Jose Garcia Ripoll.
;;;;
;;;;    ECoLisp is free software; you can redistribute it and/or
;;;;    modify it under the terms of the GNU Library General Public
;;;;    License as published by the Free Software Foundation; either
;;;;    version 2 of the License, or (at your option) any later version.
;;;;
;;;;    See file '../Copyright' for full details.

(in-package "COMPILER")

18
(defparameter +optimizable-constants+ '())
jjgarcia's avatar
jjgarcia committed
19

20
(defun c1constant-value (val &key always only-small-values)
jjgarcia's avatar
jjgarcia committed
21
  (cond
22
   ((let ((x (assoc val *optimizable-constants*)))
23
24
      (when x
       (pushnew "#include <float.h>" *clines-string-list*)
25
26
27
       (setf x (cdr x))
       (if (listp x)
           (c1expr x)
28
           x))))
jjgarcia's avatar
jjgarcia committed
29
30
31
32
33
34
   ((eq val nil) (c1nil))
   ((eq val t) (c1t))
   ((sys::fixnump val)
    (make-c1form* 'LOCATION :type 'FIXNUM :args (list 'FIXNUM-VALUE val)))
   ((characterp val)
    (make-c1form* 'LOCATION :type 'CHARACTER
Daniel Kochmański's avatar
Daniel Kochmański committed
35
                  :args (list 'CHARACTER-VALUE (char-code val))))
36
   ((typep val 'DOUBLE-FLOAT)
37
38
    (when (and (ext:float-nan-p val) (not only-small-values))
      (cmperr "Cannot externalize value ~A" val))
39
    (make-c1form* 'LOCATION :type 'DOUBLE-FLOAT
Daniel Kochmański's avatar
Daniel Kochmański committed
40
                  :args (list 'DOUBLE-FLOAT-VALUE val (add-object val))))
41
   ((typep val 'SINGLE-FLOAT)
42
43
    (when (and (ext:float-nan-p val) (not only-small-values))
      (cmperr "Cannot externalize value ~A" val))
44
    (make-c1form* 'LOCATION :type 'SINGLE-FLOAT
Daniel Kochmański's avatar
Daniel Kochmański committed
45
                  :args (list 'SINGLE-FLOAT-VALUE val (add-object val))))
46
   ((typep val 'LONG-FLOAT)
47
48
    (when (and (ext:float-nan-p val) (not only-small-values))
      (cmperr "Cannot externalize value ~A" val))
49
    (make-c1form* 'LOCATION :type 'LONG-FLOAT
Daniel Kochmański's avatar
Daniel Kochmański committed
50
                  :args (list 'LONG-FLOAT-VALUE val (add-object val))))
51
52
53
   #+sse2
   ((typep val 'EXT:SSE-PACK)
    (c1constant-value/sse val))
54
   (only-small-values nil)
55
   (always
jjgarcia's avatar
jjgarcia committed
56
    (make-c1form* 'LOCATION :type (object-type val)
Daniel Kochmański's avatar
Daniel Kochmański committed
57
                  :args (add-object val)))
jjgarcia's avatar
jjgarcia committed
58
59
   (t nil)))

60
61
62
63
64
65
66
67
68
#+sse2
(defun c1constant-value/sse (value)
  (let* ((bytes (ext:sse-pack-to-vector value '(unsigned-byte 8)))
         (elt-type (ext:sse-pack-element-type value)))
    (multiple-value-bind (wrapper rtype)
        (case elt-type
          (single-float (values "_mm_castsi128_ps" :float-sse-pack))
          (double-float (values "_mm_castsi128_pd" :double-sse-pack))
          (otherwise    (values ""                 :int-sse-pack)))
69
      `(c-inline () () ,rtype
Daniel Kochmański's avatar
Daniel Kochmański committed
70
71
72
                 ,(format nil "~A(_mm_setr_epi8(~{~A~^,~}))"
                          wrapper (coerce bytes 'list))
                 :one-liner t :side-effects nil))))
73

jjgarcia's avatar
jjgarcia committed
74
75
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
76
;;; KNOWN OPTIMIZABLE CONSTANTS
jjgarcia's avatar
jjgarcia committed
77
78
;;;

79
80
(defun make-single-constant-optimizer (name c-value)
  (cond ((symbolp name)
Daniel Kochmański's avatar
Daniel Kochmański committed
81
82
83
84
85
86
         (let* ((value (symbol-value name))
                (type (lisp-type->rep-type (type-of value))))
           (cons value `(c-inline () () ,type ,c-value
                                  :one-liner t :side-effects nil))))
        ((floatp name)
         (let* ((value name)
87
                      (type (type-of value))
Daniel Kochmański's avatar
Daniel Kochmański committed
88
89
90
91
92
93
94
95
96
97
98
                (loc-type (case type
                            (single-float 'single-float-value)
                            (double-float 'double-float-value)
                            (long-float 'long-float-value)))
                (location (make-vv :location c-value :value value)))
           (cons value (make-c1form* 'LOCATION :type type
                                     :args (list loc-type value location)))))
        (t
         (cons name (make-c1form* 'LOCATION :type (type-of name)
                                  :args (make-vv :location c-value
                                                 :value name))))))
jjgarcia's avatar
jjgarcia committed
99

100
101
102
(defun make-optimizable-constants (machine)
  (loop for (value name) in (optimizable-constants-list machine)
     collect (make-single-constant-optimizer value name)))
jjgarcia's avatar
jjgarcia committed
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
(defun optimizable-constants-list (machine)
  (append
   ;; Constants that appear everywhere
   '(
     ;; Order is important: on platforms where 0.0 and -0.0 are the same
     ;; the last one is prioritized.
     (#.(coerce 0 'single-float) "cl_core.singlefloat_zero")
     (#.(coerce 0 'double-float) "cl_core.doublefloat_zero")
     (#.(coerce -0.0 'single-float) "cl_core.singlefloat_minus_zero")
     (#.(coerce -0.0 'double-float) "cl_core.doublefloat_minus_zero")
     #+long-float
     (#.(coerce 0 'long-float) "cl_core.longfloat_zero")
     #+long-float
     (#.(coerce -0.0 'long-float) "cl_core.longfloat_minus_zero")
jjgarcia's avatar
jjgarcia committed
118

119
120
121
     ;; We temporarily remove this constant, because the bytecodes compiler
     ;; does not know how to externalize it.
     ;;(#.(si::standard-readtable) "cl_core.standard_readtable")
jjgarcia's avatar
jjgarcia committed
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
     (#.(find-package :cl) "cl_core.lisp_package")
     (#.(find-package :cl-user) "cl_core.user_package")
     (#.(find-package :keyword) "cl_core.keyword_package")
     (#.(find-package :clos) "cl_core.clos_package")
     #+threads
     (#.(find-package :mp) "cl_core.mp_package")
     )
   (when (eq machine +default-machine+)
     ;; Constants which are not portable
     `((MOST-POSITIVE-SHORT-FLOAT "FLT_MAX")
       (MOST-POSITIVE-SINGLE-FLOAT "FLT_MAX")
       
       (MOST-NEGATIVE-SHORT-FLOAT "-FLT_MAX")
       (MOST-NEGATIVE-SINGLE-FLOAT "-FLT_MAX")
137

138
139
140
141
       (LEAST-POSITIVE-SHORT-FLOAT "FLT_MIN")
       (LEAST-POSITIVE-SINGLE-FLOAT "FLT_MIN")
       (LEAST-POSITIVE-NORMALIZED-SHORT-FLOAT "FLT_MIN")
       (LEAST-POSITIVE-NORMALIZED-SINGLE-FLOAT" FLT_MIN")
142

143
144
145
146
       (LEAST-NEGATIVE-SHORT-FLOAT "-FLT_MIN")
       (LEAST-NEGATIVE-SINGLE-FLOAT "-FLT_MIN")
       (LEAST-NEGATIVE-NORMALIZED-SHORT-FLOAT "-FLT_MIN")
       (LEAST-NEGATIVE-NORMALIZED-SINGLE-FLOAT "-FLT_MIN")
147

148
149
150
151
152
153
       (MOST-POSITIVE-DOUBLE-FLOAT "DBL_MAX")
       (MOST-NEGATIVE-DOUBLE-FLOAT "-DBL_MAX")
       (LEAST-POSITIVE-DOUBLE-FLOAT "DBL_MIN")
       (LEAST-POSITIVE-NORMALIZED-DOUBLE-FLOAT "DBL_MIN")
       (LEAST-NEGATIVE-DOUBLE-FLOAT "-DBL_MIN")
       (LEAST-NEGATIVE-NORMALIZED-DOUBLE-FLOAT "-DBL_MIN")
154

155
156
157
158
       #+ieee-floating-point
       ,@'((SHORT-FLOAT-POSITIVE-INFINITY  "INFINITY")
           (SINGLE-FLOAT-POSITIVE-INFINITY "INFINITY")
           (DOUBLE-FLOAT-POSITIVE-INFINITY "INFINITY")
159

160
161
162
           (SHORT-FLOAT-NEGATIVE-INFINITY  "-INFINITY")
           (SINGLE-FLOAT-NEGATIVE-INFINITY "-INFINITY")
           (DOUBLE-FLOAT-NEGATIVE-INFINITY "-INFINITY"))
163

164
       #+long-float
165
       ,@'((MOST-POSITIVE-LONG-FLOAT "LDBL_MAX")
Daniel Kochmański's avatar
Daniel Kochmański committed
166
167
168
169
170
           (MOST-NEGATIVE-LONG-FLOAT "-LDBL_MAX")
           (LEAST-POSITIVE-LONG-FLOAT "LDBL_MIN")
           (LEAST-POSITIVE-NORMALIZED-LONG-FLOAT" LDBL_MIN")
           (LEAST-NEGATIVE-LONG-FLOAT "-LDBL_MIN")
           (LEAST-NEGATIVE-NORMALIZED-LONG-FLOAT "-LDBL_MIN")
171
           #+ieee-floating-point
172
           (LONG-FLOAT-POSITIVE-INFINITY   "INFINITY")
173
           #+ieee-floating-point
174
           (LONG-FLOAT-NEGATIVE-INFINITY   "-INFINITY")
Daniel Kochmański's avatar
Daniel Kochmański committed
175
           )))))