Skip to content

Commit 17c3b68

Browse files
authored
Merge pull request #1310 from herbie-fp/hash-lang-platform
Add a `#lang` for Herbie platforms
2 parents 7e01615 + a36c75e commit 17c3b68

File tree

18 files changed

+729
-919
lines changed

18 files changed

+729
-919
lines changed

.fmt.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
(define (the-formatter-map s)
88
(case s
99
[("define-operators") (standard-formatter-map "begin")]
10+
[("define-operations") (standard-formatter-map "define")]
1011
[("define-rules") (standard-formatter-map "define")]
12+
[("define-generator") (standard-formatter-map "define")]
1113
[("define-api-endpoint") (standard-formatter-map "define")]
1214
[else #f]))

infra/softposit.rkt

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
(require math/flonum
77
math/bigfloat
88
softposit-rkt
9-
"../src/utils/float.rkt" ; for shift/unshift
10-
rival
9+
"../src/syntax/types.rkt" ; for shift/unshift
1110
"../src/syntax/platform.rkt")
1211
(provide platform)
1312

@@ -87,8 +86,9 @@
8786

8887
(define cost 1)
8988

90-
(define platform
91-
(make-empty-platform 'softposit #:if-cost 1))
89+
(define platform (make-empty-platform 'softposit))
90+
91+
(platform-register-if-cost! platform #:cost 1)
9292

9393
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; REPRESENTATIONS ;;;;;;;;;;;;;;;;;;;;;;;
9494

@@ -98,8 +98,6 @@
9898

9999
(define posit8
100100
(make-representation #:name 'posit8
101-
#:type 'real
102-
#:repr? posit8?
103101
#:bf->repr (compose double->posit8* bigfloat->flonum)
104102
#:repr->bf (compose bf-inf->nan posit8->double)
105103
#:ordinal->repr (shift 7 ordinal->posit8)
@@ -109,8 +107,6 @@
109107

110108
(define posit16
111109
(make-representation #:name 'posit16
112-
#:type 'real
113-
#:repr? posit16?
114110
#:bf->repr (compose double->posit16* bigfloat->flonum)
115111
#:repr->bf (compose bf-inf->nan posit16->double)
116112
#:ordinal->repr (shift 15 ordinal->posit16)
@@ -120,8 +116,6 @@
120116

121117
(define posit32
122118
(make-representation #:name 'posit32
123-
#:type 'real
124-
#:repr? posit32?
125119
#:bf->repr (compose double->posit32* bigfloat->flonum)
126120
#:repr->bf (compose bf-inf->nan posit32->double)
127121
#:ordinal->repr (shift 31 ordinal->posit32)
@@ -131,8 +125,6 @@
131125

132126
(define quire8
133127
(make-representation #:name 'quire8
134-
#:type 'real
135-
#:repr? quire8?
136128
#:bf->repr (compose double->quire8* bigfloat->flonum)
137129
#:repr->bf (compose bf-inf->nan quire8->double)
138130
#:ordinal->repr (compose double->quire8 ordinal->flonum)
@@ -142,8 +134,6 @@
142134

143135
(define quire16
144136
(make-representation #:name 'quire16
145-
#:type 'real
146-
#:repr? quire16?
147137
#:bf->repr (compose double->quire16* bigfloat->flonum)
148138
#:repr->bf (compose bf-inf->nan quire16->double)
149139
#:ordinal->repr (compose double->quire16 ordinal->flonum)
@@ -153,8 +143,6 @@
153143

154144
(define quire32
155145
(make-representation #:name 'quire32
156-
#:type 'real
157-
#:repr? quire32?
158146
#:bf->repr (compose double->quire32 bigfloat->flonum) ; TODO: use double->quire32* when crash fixed
159147
#:repr->bf (compose bf-inf->nan quire32->double)
160148
#:ordinal->repr (compose double->quire32 ordinal->flonum)

src/platform.rkt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#lang racket
2+
3+
(require herbie/syntax/platforms-language)
4+
(provide (all-from-out herbie/syntax/platforms-language))
5+
(module reader syntax/module-reader
6+
herbie/syntax/platforms-language)

src/platforms/c-windows.rkt

Lines changed: 171 additions & 235 deletions
Large diffs are not rendered by default.

src/platforms/c.rkt

Lines changed: 178 additions & 237 deletions
Large diffs are not rendered by default.

src/platforms/herbie10.rkt

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,21 @@
55

66
(require math/bigfloat
77
math/flonum
8-
"../utils/float.rkt" ; for shift/unshift
8+
"../syntax/types.rkt" ; for shift/unshift
99
"../syntax/platform.rkt")
1010
(provide platform)
1111

1212
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; EMPTY PLATFORM ;;;;;;;;;;;;;;;;;;;;;;;;
1313

14-
(define platform (make-empty-platform 'herbie10 #:if-cost 0))
14+
(define platform (make-empty-platform 'herbie10))
15+
16+
(platform-register-if-cost! platform #:if-cost 0)
1517

1618
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BOOLEAN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1719

1820
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; representation ;;;;;;;;;;;;;;;;;;;;;;;;
1921

20-
(define bool
21-
(make-representation #:name 'bool
22-
#:type 'bool
23-
#:repr? boolean?
24-
#:bf->repr identity
25-
#:repr->bf identity
26-
#:ordinal->repr (λ (x) (= x 0))
27-
#:repr->ordinal (λ (x) (if x 1 0))
28-
#:total-bits 1
29-
#:special-value? (const #f)))
22+
(define bool <bool>)
3023

3124
(platform-register-representation! platform #:repr bool #:cost 0)
3225

@@ -72,11 +65,11 @@
7265
; ([name ([var : repr] ...) otype spec fl fpcore cost])
7366
(platform-register-implementations!
7467
platform
75-
([neg.f32 ([x : binary32]) binary32 (neg x) fl32- (! :precision binary32 (- x)) 0]
76-
[+.f32 ([x : binary32] [y : binary32]) binary32 (+ x y) fl32+ (! :precision binary32 (+ x y)) 0]
77-
[-.f32 ([x : binary32] [y : binary32]) binary32 (- x y) fl32- (! :precision binary32 (- x y)) 0]
78-
[*.f32 ([x : binary32] [y : binary32]) binary32 (* x y) fl32* (! :precision binary32 (* x y)) 0]
79-
[/.f32 ([x : binary32] [y : binary32]) binary32 (/ x y) fl32/ (! :precision binary32 (/ x y)) 0]
68+
([neg.f32 ([x : binary32]) binary32 (neg x) (compose flsingle -) (! :precision binary32 (- x)) 0]
69+
[+.f32 ([x : binary32] [y : binary32]) binary32 (+ x y) (compose flsingle +) (! :precision binary32 (+ x y)) 0]
70+
[-.f32 ([x : binary32] [y : binary32]) binary32 (- x y) (compose flsingle -) (! :precision binary32 (- x y)) 0]
71+
[*.f32 ([x : binary32] [y : binary32]) binary32 (* x y) (compose flsingle *) (! :precision binary32 (* x y)) 0]
72+
[/.f32 ([x : binary32] [y : binary32]) binary32 (/ x y) (compose flsingle /) (! :precision binary32 (/ x y)) 0]
8073
[==.f32 ([x : binary32] [y : binary32]) bool (== x y) = (== x y) 0]
8174
[!=.f32 ([x : binary32] [y : binary32]) bool (!= x y) (negate =) (!= x y) 0]
8275
[<.f32 ([x : binary32] [y : binary32]) bool (< x y) < (< x y) 0]
@@ -144,16 +137,7 @@
144137

145138
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; representation ;;;;;;;;;;;;;;;;;;;;;;;;
146139

147-
(define binary64
148-
(make-representation #:name 'binary64
149-
#:type 'real
150-
#:repr? flonum?
151-
#:bf->repr bigfloat->flonum
152-
#:repr->bf bf
153-
#:ordinal->repr (shift 63 ordinal->flonum)
154-
#:repr->ordinal (unshift 63 flonum->ordinal)
155-
#:total-bits 64
156-
#:special-value? nan?))
140+
(define binary64 <binary64>)
157141

158142
(platform-register-representation! platform #:repr binary64 #:cost 0)
159143

@@ -245,14 +229,14 @@
245229
binary32
246230
#:spec x
247231
#:fpcore (! :precision binary32 (cast x))
248-
#:fl flsingle))
232+
#:impl flsingle))
249233

250234
#;(platform-register-implementation! platform
251235
(make-operator-impl (binary32->binary64 [x : binary32])
252236
binary64
253237
#:spec x
254238
#:fpcore (! :precision binary64 (cast x))
255-
#:fl identity))
239+
#:impl identity))
256240

257241
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; REGISTER PLATFORM ;;;;;;;;;;;;;;;;;;;;;
258242

src/platforms/herbie20.rkt

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,21 @@
55

66
(require math/bigfloat
77
math/flonum
8-
"../utils/float.rkt" ; for shift/unshift
8+
"../syntax/types.rkt" ; for shift/unshift
99
"../syntax/platform.rkt")
1010
(provide platform)
1111

1212
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; EMPTY PLATFORM ;;;;;;;;;;;;;;;;;;;;;;;;
1313

14-
(define platform (make-empty-platform 'herbie20 #:if-cost 1))
14+
(define platform (make-empty-platform 'herbie20))
15+
16+
(platform-register-if-cost! platform #:if-cost 1)
1517

1618
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BOOLEAN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1719

1820
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; representation ;;;;;;;;;;;;;;;;;;;;;;;;
1921

20-
(define bool
21-
(make-representation #:name 'bool
22-
#:type 'bool
23-
#:repr? boolean?
24-
#:bf->repr identity
25-
#:repr->bf identity
26-
#:ordinal->repr (λ (x) (= x 0))
27-
#:repr->ordinal (λ (x) (if x 1 0))
28-
#:total-bits 1
29-
#:special-value? (const #f)))
22+
(define bool <bool>)
3023

3124
(platform-register-representation! platform #:repr bool #:cost 1)
3225

@@ -72,11 +65,11 @@
7265
; ([name ([var : repr] ...) otype spec fl fpcore cost])
7366
(platform-register-implementations!
7467
platform
75-
([neg.f32 ([x : binary32]) binary32 (neg x) fl32- (! :precision binary32 (- x)) 64]
76-
[+.f32 ([x : binary32] [y : binary32]) binary32 (+ x y) fl32+ (! :precision binary32 (+ x y)) 64]
77-
[-.f32 ([x : binary32] [y : binary32]) binary32 (- x y) fl32- (! :precision binary32 (- x y)) 64]
78-
[*.f32 ([x : binary32] [y : binary32]) binary32 (* x y) fl32* (! :precision binary32 (* x y)) 128]
79-
[/.f32 ([x : binary32] [y : binary32]) binary32 (/ x y) fl32/ (! :precision binary32 (/ x y)) 320]
68+
([neg.f32 ([x : binary32]) binary32 (neg x) (compose flsingle -) (! :precision binary32 (- x)) 64]
69+
[+.f32 ([x : binary32] [y : binary32]) binary32 (+ x y) (compose flsingle +) (! :precision binary32 (+ x y)) 64]
70+
[-.f32 ([x : binary32] [y : binary32]) binary32 (- x y) (compose flsingle -) (! :precision binary32 (- x y)) 64]
71+
[*.f32 ([x : binary32] [y : binary32]) binary32 (* x y) (compose flsingle *) (! :precision binary32 (* x y)) 128]
72+
[/.f32 ([x : binary32] [y : binary32]) binary32 (/ x y) (compose flsingle /) (! :precision binary32 (/ x y)) 320]
8073
[==.f32 ([x : binary32] [y : binary32]) bool (== x y) = (== x y) 128]
8174
[!=.f32 ([x : binary32] [y : binary32]) bool (!= x y) (negate =) (!= x y) 128]
8275
[<.f32 ([x : binary32] [y : binary32]) bool (< x y) < (< x y) 128]
@@ -144,16 +137,7 @@
144137

145138
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; representation ;;;;;;;;;;;;;;;;;;;;;;;;
146139

147-
(define binary64
148-
(make-representation #:name 'binary64
149-
#:type 'real
150-
#:repr? flonum?
151-
#:bf->repr bigfloat->flonum
152-
#:repr->bf bf
153-
#:ordinal->repr (shift 63 ordinal->flonum)
154-
#:repr->ordinal (unshift 63 flonum->ordinal)
155-
#:total-bits 64
156-
#:special-value? nan?))
140+
(define binary64 <binary64>)
157141

158142
(platform-register-representation! platform #:repr binary64 #:cost 64)
159143

@@ -245,15 +229,15 @@
245229
binary32
246230
#:spec x
247231
#:fpcore (! :precision binary32 (cast x))
248-
#:fl flsingle
232+
#:impl flsingle
249233
#:cost 64))
250234

251235
#;(platform-register-implementation! platform
252236
(make-operator-impl (binary32->binary64 [x : binary32])
253237
binary64
254238
#:spec x
255239
#:fpcore (! :precision binary64 (cast x))
256-
#:fl identity
240+
#:impl identity
257241
#:cost 64))
258242

259243
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; REGISTER PLATFORM ;;;;;;;;;;;;;;;;;;;;;

src/platforms/math.rkt

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
(require math/bigfloat
88
math/flonum
9-
"../utils/float.rkt" ; for shift/unshift
9+
"../syntax/types.rkt" ; for shift/unshift
1010
"../syntax/platform.rkt")
1111
(provide platform)
1212

@@ -15,22 +15,15 @@
1515
(define move-cost 0.02333600000000001)
1616
(define fl-move-cost (* move-cost 4))
1717

18-
(define platform (make-empty-platform 'math #:if-cost move-cost))
18+
(define platform (make-empty-platform 'math))
19+
20+
(platform-register-if-cost! platform #:if-cost move-cost)
1921

2022
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BOOLEAN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2123

2224
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; representation ;;;;;;;;;;;;;;;;;;;;;;;;
2325

24-
(define bool
25-
(make-representation #:name 'bool
26-
#:type 'bool
27-
#:repr? boolean?
28-
#:bf->repr identity
29-
#:repr->bf identity
30-
#:ordinal->repr (λ (x) (= x 0))
31-
#:repr->ordinal (λ (x) (if x 1 0))
32-
#:total-bits 1
33-
#:special-value? (const #f)))
26+
(define bool <bool>)
3427

3528
(platform-register-representation! platform #:repr bool #:cost move-cost)
3629

@@ -58,16 +51,7 @@
5851

5952
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; representation ;;;;;;;;;;;;;;;;;;;;;;;;
6053

61-
(define binary64
62-
(make-representation #:name 'binary64
63-
#:type 'real
64-
#:repr? flonum?
65-
#:bf->repr bigfloat->flonum
66-
#:repr->bf bf
67-
#:ordinal->repr (shift 63 ordinal->flonum)
68-
#:repr->ordinal (unshift 63 flonum->ordinal)
69-
#:total-bits 64
70-
#:special-value? nan?))
54+
(define binary64 <binary64>)
7155

7256
(platform-register-representation! platform #:repr binary64 #:cost fl-move-cost)
7357

@@ -149,7 +133,7 @@
149133
binary64
150134
#:spec (- 1 (erf x))
151135
#:fpcore (! :precision binary64 (erfc x))
152-
#:fl (from-libm 'erfc)
136+
#:impl (from-libm 'erfc)
153137
#:cost 0.816512))
154138

155139
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; REGISTER PLATFORM ;;;;;;;;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)