| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus bytevectors) |
| 18 |
(export |
| 19 |
make-bytevector bytevector-length bytevector-s8-ref |
| 20 |
bytevector-u8-ref bytevector-u8-set! bytevector-s8-set! |
| 21 |
bytevector-copy! u8-list->bytevector bytevector->u8-list |
| 22 |
bytevector-u16-native-ref bytevector-u16-native-set! |
| 23 |
bytevector-s16-native-ref bytevector-s16-native-set! |
| 24 |
bytevector-u32-native-ref bytevector-u32-native-set! |
| 25 |
bytevector-s32-native-ref bytevector-s32-native-set! |
| 26 |
bytevector-u64-native-ref bytevector-u64-native-set! |
| 27 |
bytevector-s64-native-ref bytevector-s64-native-set! |
| 28 |
bytevector-u16-ref bytevector-u16-set! |
| 29 |
bytevector-s16-ref bytevector-s16-set! |
| 30 |
bytevector-u32-ref bytevector-u32-set! |
| 31 |
bytevector-s32-ref bytevector-s32-set! |
| 32 |
bytevector-u64-ref bytevector-u64-set! |
| 33 |
bytevector-s64-ref bytevector-s64-set! |
| 34 |
bytevector-fill! bytevector-copy bytevector=? |
| 35 |
bytevector-uint-ref bytevector-sint-ref |
| 36 |
bytevector-uint-set! bytevector-sint-set! |
| 37 |
bytevector->uint-list bytevector->sint-list |
| 38 |
uint-list->bytevector sint-list->bytevector |
| 39 |
bytevector-ieee-double-native-ref bytevector-ieee-double-native-set! |
| 40 |
bytevector-ieee-single-native-ref bytevector-ieee-single-native-set! |
| 41 |
bytevector-ieee-double-ref bytevector-ieee-double-set! |
| 42 |
bytevector-ieee-single-ref bytevector-ieee-single-set! |
| 43 |
native-endianness) |
| 44 |
(import |
| 45 |
(except (ikarus) |
| 46 |
make-bytevector bytevector-length bytevector-s8-ref |
| 47 |
bytevector-u8-ref bytevector-u8-set! bytevector-s8-set! |
| 48 |
bytevector-copy! u8-list->bytevector bytevector->u8-list |
| 49 |
bytevector-u16-native-ref bytevector-u16-native-set! |
| 50 |
bytevector-s16-native-ref bytevector-s16-native-set! |
| 51 |
bytevector-u32-native-ref bytevector-u32-native-set! |
| 52 |
bytevector-s32-native-ref bytevector-s32-native-set! |
| 53 |
bytevector-u64-native-ref bytevector-u64-native-set! |
| 54 |
bytevector-s64-native-ref bytevector-s64-native-set! |
| 55 |
bytevector-u16-ref bytevector-u16-set! |
| 56 |
bytevector-s16-ref bytevector-s16-set! |
| 57 |
bytevector-u32-ref bytevector-u32-set! |
| 58 |
bytevector-s32-ref bytevector-s32-set! |
| 59 |
bytevector-u64-ref bytevector-u64-set! |
| 60 |
bytevector-s64-ref bytevector-s64-set! |
| 61 |
bytevector-fill! bytevector-copy bytevector=? |
| 62 |
bytevector-uint-ref bytevector-sint-ref |
| 63 |
bytevector-uint-set! bytevector-sint-set! |
| 64 |
bytevector->uint-list bytevector->sint-list |
| 65 |
uint-list->bytevector sint-list->bytevector |
| 66 |
bytevector-ieee-double-native-ref bytevector-ieee-double-native-set! |
| 67 |
bytevector-ieee-double-ref bytevector-ieee-double-set! |
| 68 |
bytevector-ieee-single-native-ref bytevector-ieee-single-native-set! |
| 69 |
bytevector-ieee-single-ref bytevector-ieee-single-set! |
| 70 |
native-endianness) |
| 71 |
(ikarus system $fx) |
| 72 |
(ikarus system $bignums) |
| 73 |
(ikarus system $pairs) |
| 74 |
(ikarus system $bytevectors)) |
| 75 |
|
| 76 |
(define (native-endianness) 'little) ;;; HARDCODED |
| 77 |
|
| 78 |
|
| 79 |
(define ($bytevector-fill x i j fill) |
| 80 |
(cond |
| 81 |
[($fx= i j) x] |
| 82 |
[else |
| 83 |
($bytevector-set! x i fill) |
| 84 |
($bytevector-fill x ($fxadd1 i) j fill)])) |
| 85 |
|
| 86 |
(define make-bytevector |
| 87 |
(case-lambda |
| 88 |
[(k) |
| 89 |
(if (and (fixnum? k) ($fx>= k 0)) |
| 90 |
($make-bytevector k) |
| 91 |
(die 'make-bytevector "not a valid size" k))] |
| 92 |
[(k fill) |
| 93 |
(if (and (fixnum? fill) ($fx<= -128 fill) ($fx<= fill 255)) |
| 94 |
($bytevector-fill (make-bytevector k) 0 k fill) |
| 95 |
(die 'make-bytevector "not a valid fill" fill))])) |
| 96 |
|
| 97 |
(define bytevector-fill! |
| 98 |
(lambda (x fill) |
| 99 |
(unless (bytevector? x) |
| 100 |
(die 'bytevector-fill! "not a bytevector" x)) |
| 101 |
(unless (and (fixnum? fill) ($fx<= -128 fill) ($fx<= fill 255)) |
| 102 |
(die 'bytevector-fill! "not a valid fill" fill)) |
| 103 |
($bytevector-fill x 0 ($bytevector-length x) fill))) |
| 104 |
|
| 105 |
|
| 106 |
(define bytevector-length |
| 107 |
(lambda (x) |
| 108 |
(if (bytevector? x) |
| 109 |
($bytevector-length x) |
| 110 |
(die 'bytevector-length "not a bytevector" x)))) |
| 111 |
|
| 112 |
(define bytevector-s8-ref |
| 113 |
(lambda (x i) |
| 114 |
(if (bytevector? x) |
| 115 |
(if (and (fixnum? i) ($fx<= 0 i) ($fx< i ($bytevector-length x))) |
| 116 |
($bytevector-s8-ref x i) |
| 117 |
(die 'bytevector-s8-ref "invalid index" i x)) |
| 118 |
(die 'bytevector-s8-ref "not a bytevector" x)))) |
| 119 |
|
| 120 |
(define bytevector-u8-ref |
| 121 |
(lambda (x i) |
| 122 |
(if (bytevector? x) |
| 123 |
(if (and (fixnum? i) ($fx<= 0 i) ($fx< i ($bytevector-length x))) |
| 124 |
($bytevector-u8-ref x i) |
| 125 |
(die 'bytevector-u8-ref "invalid index" i x)) |
| 126 |
(die 'bytevector-u8-ref "not a bytevector" x)))) |
| 127 |
|
| 128 |
|
| 129 |
(define bytevector-s8-set! |
| 130 |
(lambda (x i v) |
| 131 |
(if (bytevector? x) |
| 132 |
(if (and (fixnum? i) ($fx<= 0 i) ($fx< i ($bytevector-length x))) |
| 133 |
(if (and (fixnum? v) ($fx<= -128 v) ($fx<= v 127)) |
| 134 |
($bytevector-set! x i v) |
| 135 |
(die 'bytevector-s8-set! "not a byte" v)) |
| 136 |
(die 'bytevector-s8-set! "invalid index" i x)) |
| 137 |
(die 'bytevector-s8-set! "not a bytevector" x)))) |
| 138 |
|
| 139 |
(define bytevector-u8-set! |
| 140 |
(lambda (x i v) |
| 141 |
(if (bytevector? x) |
| 142 |
(if (and (fixnum? i) ($fx<= 0 i) ($fx< i ($bytevector-length x))) |
| 143 |
(if (and (fixnum? v) ($fx<= 0 v) ($fx<= v 255)) |
| 144 |
($bytevector-set! x i v) |
| 145 |
(die 'bytevector-u8-set! "not an octet" v)) |
| 146 |
(die 'bytevector-u8-set! "invalid index" i x)) |
| 147 |
(die 'bytevector-u8-set! "not a bytevector" x)))) |
| 148 |
|
| 149 |
(define bytevector-u16-native-ref ;;; HARDCODED |
| 150 |
(lambda (x i) |
| 151 |
(if (bytevector? x) |
| 152 |
(if (and (fixnum? i) |
| 153 |
($fx<= 0 i) |
| 154 |
($fx< i ($fxsub1 ($bytevector-length x))) |
| 155 |
($fxzero? ($fxlogand i 1))) |
| 156 |
($fxlogor ;;; little |
| 157 |
($bytevector-u8-ref x i) |
| 158 |
($fxsll ($bytevector-u8-ref x ($fxadd1 i)) 8)) |
| 159 |
(die 'bytevector-u16-native-ref "invalid index" i)) |
| 160 |
(die 'bytevector-u16-native-ref "not a bytevector" x)))) |
| 161 |
|
| 162 |
|
| 163 |
(define bytevector-u16-native-set! ;;; HARDCODED |
| 164 |
(lambda (x i n) |
| 165 |
(if (bytevector? x) |
| 166 |
(if (and (fixnum? n) |
| 167 |
($fx<= 0 n) |
| 168 |
($fx<= n #xFFFF)) |
| 169 |
(if (and (fixnum? i) |
| 170 |
($fx<= 0 i) |
| 171 |
($fx< i ($fxsub1 ($bytevector-length x))) |
| 172 |
($fxzero? ($fxlogand i 1))) |
| 173 |
(begin ;;; little |
| 174 |
($bytevector-set! x i n) |
| 175 |
($bytevector-set! x ($fxadd1 i) ($fxsra n 8))) |
| 176 |
(die 'bytevector-u16-native-set! "invalid index" i)) |
| 177 |
(die 'bytevector-u16-native-set! "invalid value" n)) |
| 178 |
(die 'bytevector-u16-native-set! "not a bytevector" x)))) |
| 179 |
|
| 180 |
(define bytevector-s16-native-set! ;;; HARDCODED |
| 181 |
(lambda (x i n) |
| 182 |
(if (bytevector? x) |
| 183 |
(if (and (fixnum? n) |
| 184 |
($fx<= #x-8000 n) |
| 185 |
($fx<= n #x7FFF)) |
| 186 |
(if (and (fixnum? i) |
| 187 |
($fx<= 0 i) |
| 188 |
($fx< i ($fxsub1 ($bytevector-length x))) |
| 189 |
($fxzero? ($fxlogand i 1))) |
| 190 |
(begin ;;; little |
| 191 |
($bytevector-set! x i n) |
| 192 |
($bytevector-set! x ($fxadd1 i) ($fxsra n 8))) |
| 193 |
(die 'bytevector-s16-native-set! "invalid index" i)) |
| 194 |
(die 'bytevector-s16-native-set! "invalid value" n)) |
| 195 |
(die 'bytevector-s16-native-set! "not a bytevector" x)))) |
| 196 |
|
| 197 |
(define bytevector-s16-native-ref ;;; HARDCODED |
| 198 |
(lambda (x i) |
| 199 |
(if (bytevector? x) |
| 200 |
(if (and (fixnum? i) |
| 201 |
($fx<= 0 i) |
| 202 |
($fx< i ($fxsub1 ($bytevector-length x))) |
| 203 |
($fxzero? ($fxlogand i 1))) |
| 204 |
($fxlogor ;;; little |
| 205 |
($bytevector-u8-ref x i) |
| 206 |
($fxsll ($bytevector-s8-ref x ($fxadd1 i)) 8)) |
| 207 |
(die 'bytevector-s16-native-ref "invalid index" i)) |
| 208 |
(die 'bytevector-s16-native-ref "not a bytevector" x)))) |
| 209 |
|
| 210 |
(define bytevector-u16-ref |
| 211 |
(lambda (x i end) |
| 212 |
(if (bytevector? x) |
| 213 |
(if (and (fixnum? i) |
| 214 |
($fx<= 0 i) |
| 215 |
($fx< i ($fxsub1 ($bytevector-length x)))) |
| 216 |
(case end |
| 217 |
[(big) |
| 218 |
($fxlogor |
| 219 |
($fxsll ($bytevector-u8-ref x i) 8) |
| 220 |
($bytevector-u8-ref x ($fxadd1 i)))] |
| 221 |
[(little) |
| 222 |
($fxlogor |
| 223 |
($fxsll ($bytevector-u8-ref x (fxadd1 i)) 8) |
| 224 |
($bytevector-u8-ref x i))] |
| 225 |
[else (die 'bytevector-u16-ref "invalid endianness" end)]) |
| 226 |
(die 'bytevector-u16-ref "invalid index" i)) |
| 227 |
(die 'bytevector-u16-ref "not a bytevector" x)))) |
| 228 |
|
| 229 |
(define bytevector-u32-ref |
| 230 |
(lambda (x i end) |
| 231 |
(if (bytevector? x) |
| 232 |
(if (and (fixnum? i) |
| 233 |
($fx<= 0 i) |
| 234 |
($fx< i ($fx- ($bytevector-length x) 3))) |
| 235 |
(case end |
| 236 |
[(big) |
| 237 |
(+ (sll ($bytevector-u8-ref x i) 24) |
| 238 |
($fxlogor |
| 239 |
($fxsll ($bytevector-u8-ref x ($fx+ i 1)) 16) |
| 240 |
($fxlogor |
| 241 |
($fxsll ($bytevector-u8-ref x ($fx+ i 2)) 8) |
| 242 |
($bytevector-u8-ref x ($fx+ i 3)))))] |
| 243 |
[(little) |
| 244 |
(+ (sll ($bytevector-u8-ref x ($fx+ i 3)) 24) |
| 245 |
($fxlogor |
| 246 |
($fxsll ($bytevector-u8-ref x ($fx+ i 2)) 16) |
| 247 |
($fxlogor |
| 248 |
($fxsll ($bytevector-u8-ref x ($fx+ i 1)) 8) |
| 249 |
($bytevector-u8-ref x i))))] |
| 250 |
[else (die 'bytevector-u32-ref "invalid endianness" end)]) |
| 251 |
(die 'bytevector-u32-ref "invalid index" i)) |
| 252 |
(die 'bytevector-u32-ref "not a bytevector" x)))) |
| 253 |
|
| 254 |
(define bytevector-u32-native-ref |
| 255 |
(lambda (x i) |
| 256 |
(if (bytevector? x) |
| 257 |
(if (and (fixnum? i) |
| 258 |
($fx<= 0 i) |
| 259 |
($fx= 0 ($fxlogand i 3)) |
| 260 |
($fx< i ($fx- ($bytevector-length x) 3))) |
| 261 |
(+ (sll ($bytevector-u8-ref x ($fx+ i 3)) 24) |
| 262 |
($fxlogor |
| 263 |
($fxsll ($bytevector-u8-ref x ($fx+ i 2)) 16) |
| 264 |
($fxlogor |
| 265 |
($fxsll ($bytevector-u8-ref x ($fx+ i 1)) 8) |
| 266 |
($bytevector-u8-ref x i)))) |
| 267 |
(die 'bytevector-u32-native-ref "invalid index" i)) |
| 268 |
(die 'bytevector-u32-native-ref "not a bytevector" x)))) |
| 269 |
|
| 270 |
(define bytevector-s32-ref |
| 271 |
(lambda (x i end) |
| 272 |
(if (bytevector? x) |
| 273 |
(if (and (fixnum? i) |
| 274 |
($fx<= 0 i) |
| 275 |
($fx< i ($fx- ($bytevector-length x) 3))) |
| 276 |
(case end |
| 277 |
[(big) |
| 278 |
(+ (sll ($bytevector-s8-ref x i) 24) |
| 279 |
($fxlogor |
| 280 |
($fxsll ($bytevector-u8-ref x ($fx+ i 1)) 16) |
| 281 |
($fxlogor |
| 282 |
($fxsll ($bytevector-u8-ref x ($fx+ i 2)) 8) |
| 283 |
($bytevector-u8-ref x ($fx+ i 3)))))] |
| 284 |
[(little) |
| 285 |
(+ (sll ($bytevector-s8-ref x ($fx+ i 3)) 24) |
| 286 |
($fxlogor |
| 287 |
($fxsll ($bytevector-u8-ref x ($fx+ i 2)) 16) |
| 288 |
($fxlogor |
| 289 |
($fxsll ($bytevector-u8-ref x ($fx+ i 1)) 8) |
| 290 |
($bytevector-u8-ref x i))))] |
| 291 |
[else (die 'bytevector-s32-ref "invalid endianness" end)]) |
| 292 |
(die 'bytevector-s32-ref "invalid index" i)) |
| 293 |
(die 'bytevector-s32-ref "not a bytevector" x)))) |
| 294 |
|
| 295 |
(define bytevector-s32-native-ref |
| 296 |
(lambda (x i) |
| 297 |
(if (bytevector? x) |
| 298 |
(if (and (fixnum? i) |
| 299 |
($fx<= 0 i) |
| 300 |
($fx= 0 ($fxlogand i 3)) |
| 301 |
($fx< i ($fx- ($bytevector-length x) 3))) |
| 302 |
(+ (sll ($bytevector-s8-ref x ($fx+ i 3)) 24) |
| 303 |
($fxlogor |
| 304 |
($fxsll ($bytevector-u8-ref x ($fx+ i 2)) 16) |
| 305 |
($fxlogor |
| 306 |
($fxsll ($bytevector-u8-ref x ($fx+ i 1)) 8) |
| 307 |
($bytevector-u8-ref x i)))) |
| 308 |
(die 'bytevector-s32-native-ref "invalid index" i)) |
| 309 |
(die 'bytevector-s32-native-ref "not a bytevector" x)))) |
| 310 |
|
| 311 |
(define bytevector-u16-set! |
| 312 |
(lambda (x i n end) |
| 313 |
(if (bytevector? x) |
| 314 |
(if (and (fixnum? n) |
| 315 |
($fx<= 0 n) |
| 316 |
($fx<= n #xFFFF)) |
| 317 |
(if (and (fixnum? i) |
| 318 |
($fx<= 0 i) |
| 319 |
($fx< i ($fxsub1 ($bytevector-length x)))) |
| 320 |
(case end |
| 321 |
[(big) |
| 322 |
($bytevector-set! x i ($fxsra n 8)) |
| 323 |
($bytevector-set! x ($fxadd1 i) n)] |
| 324 |
[(little) |
| 325 |
($bytevector-set! x i n) |
| 326 |
($bytevector-set! x ($fxadd1 i) (fxsra n 8))] |
| 327 |
[else (die 'bytevector-u16-ref "invalid endianness" end)]) |
| 328 |
(die 'bytevector-u16-set! "invalid index" i)) |
| 329 |
(die 'bytevector-u16-set! "invalid value" n)) |
| 330 |
(die 'bytevector-u16-set! "not a bytevector" x)))) |
| 331 |
|
| 332 |
|
| 333 |
(define bytevector-u32-set! |
| 334 |
(lambda (x i n end) |
| 335 |
(if (bytevector? x) |
| 336 |
(if (if (fixnum? n) |
| 337 |
($fx>= n 0) |
| 338 |
(if (bignum? n) |
| 339 |
(<= 0 n #xFFFFFFFF) |
| 340 |
#f)) |
| 341 |
(if (and (fixnum? i) |
| 342 |
($fx<= 0 i) |
| 343 |
($fx< i ($fx- ($bytevector-length x) 3))) |
| 344 |
(case end |
| 345 |
[(big) |
| 346 |
(let ([b (sra n 16)]) |
| 347 |
($bytevector-set! x i ($fxsra b 8)) |
| 348 |
($bytevector-set! x ($fx+ i 1) b)) |
| 349 |
(let ([b (bitwise-and n #xFFFF)]) |
| 350 |
($bytevector-set! x ($fx+ i 2) ($fxsra b 8)) |
| 351 |
($bytevector-set! x ($fx+ i 3) b))] |
| 352 |
[(little) |
| 353 |
(let ([b (sra n 16)]) |
| 354 |
($bytevector-set! x ($fx+ i 3) ($fxsra b 8)) |
| 355 |
($bytevector-set! x ($fx+ i 2) b)) |
| 356 |
(let ([b (bitwise-and n #xFFFF)]) |
| 357 |
($bytevector-set! x ($fx+ i 1) ($fxsra b 8)) |
| 358 |
($bytevector-set! x i b))] |
| 359 |
[else (die 'bytevector-u32-ref "invalid endianness" end)]) |
| 360 |
(die 'bytevector-u32-set! "invalid index" i)) |
| 361 |
(die 'bytevector-u32-set! "invalid value" n)) |
| 362 |
(die 'bytevector-u32-set! "not a bytevector" x)))) |
| 363 |
|
| 364 |
(define bytevector-u32-native-set! |
| 365 |
(lambda (x i n) |
| 366 |
(if (bytevector? x) |
| 367 |
(if (if (fixnum? n) |
| 368 |
($fx>= n 0) |
| 369 |
(if (bignum? n) |
| 370 |
(<= 0 n #xFFFFFFFF) |
| 371 |
#f)) |
| 372 |
(if (and (fixnum? i) |
| 373 |
($fx<= 0 i) |
| 374 |
($fx= 0 ($fxlogand i 3)) |
| 375 |
($fx< i ($fx- ($bytevector-length x) 3))) |
| 376 |
(begin |
| 377 |
(let ([b (sra n 16)]) |
| 378 |
($bytevector-set! x ($fx+ i 3) ($fxsra b 8)) |
| 379 |
($bytevector-set! x ($fx+ i 2) b)) |
| 380 |
(let ([b (bitwise-and n #xFFFF)]) |
| 381 |
($bytevector-set! x ($fx+ i 1) ($fxsra b 8)) |
| 382 |
($bytevector-set! x i b))) |
| 383 |
(die 'bytevector-u32-native-set! "invalid index" i)) |
| 384 |
(die 'bytevector-u32-native-set! "invalid value" n)) |
| 385 |
(die 'bytevector-u32-native-set! "not a bytevector" x)))) |
| 386 |
|
| 387 |
|
| 388 |
(define bytevector-s32-native-set! |
| 389 |
(lambda (x i n) |
| 390 |
(if (bytevector? x) |
| 391 |
(if (if (fixnum? n) |
| 392 |
#t |
| 393 |
(if (bignum? n) |
| 394 |
(<= #x-80000000 n #x7FFFFFFF) |
| 395 |
#f)) |
| 396 |
(if (and (fixnum? i) |
| 397 |
($fx<= 0 i) |
| 398 |
($fx= 0 ($fxlogand i 3)) |
| 399 |
($fx< i ($fx- ($bytevector-length x) 3))) |
| 400 |
(begin |
| 401 |
(let ([b (sra n 16)]) |
| 402 |
($bytevector-set! x ($fx+ i 3) ($fxsra b 8)) |
| 403 |
($bytevector-set! x ($fx+ i 2) b)) |
| 404 |
(let ([b (bitwise-and n #xFFFF)]) |
| 405 |
($bytevector-set! x ($fx+ i 1) ($fxsra b 8)) |
| 406 |
($bytevector-set! x i b))) |
| 407 |
(die 'bytevector-s32-native-set! "invalid index" i)) |
| 408 |
(die 'bytevector-s32-native-set! "invalid value" n)) |
| 409 |
(die 'bytevector-s32-native-set! "not a bytevector" x)))) |
| 410 |
|
| 411 |
(define bytevector-s32-set! |
| 412 |
(lambda (x i n end) |
| 413 |
(if (bytevector? x) |
| 414 |
(if (if (fixnum? n) |
| 415 |
#t |
| 416 |
(if (bignum? n) |
| 417 |
(<= #x-80000000 n #x7FFFFFFF) |
| 418 |
#f)) |
| 419 |
(if (and (fixnum? i) |
| 420 |
($fx<= 0 i) |
| 421 |
($fx< i ($fx- ($bytevector-length x) 3))) |
| 422 |
(case end |
| 423 |
[(big) |
| 424 |
(let ([b (sra n 16)]) |
| 425 |
($bytevector-set! x i ($fxsra b 8)) |
| 426 |
($bytevector-set! x ($fx+ i 1) b)) |
| 427 |
(let ([b (bitwise-and n #xFFFF)]) |
| 428 |
($bytevector-set! x ($fx+ i 2) ($fxsra b 8)) |
| 429 |
($bytevector-set! x ($fx+ i 3) b))] |
| 430 |
[(little) |
| 431 |
(let ([b (sra n 16)]) |
| 432 |
($bytevector-set! x ($fx+ i 3) ($fxsra b 8)) |
| 433 |
($bytevector-set! x ($fx+ i 2) b)) |
| 434 |
(let ([b (bitwise-and n #xFFFF)]) |
| 435 |
($bytevector-set! x ($fx+ i 1) ($fxsra b 8)) |
| 436 |
($bytevector-set! x i b))] |
| 437 |
[else (die 'bytevector-s32-ref "invalid endianness" end)]) |
| 438 |
(die 'bytevector-s32-set! "invalid index" i)) |
| 439 |
(die 'bytevector-s32-set! "invalid value" n)) |
| 440 |
(die 'bytevector-s32-set! "not a bytevector" x)))) |
| 441 |
|
| 442 |
(define bytevector-s16-ref |
| 443 |
(lambda (x i end) |
| 444 |
(if (bytevector? x) |
| 445 |
(if (and (fixnum? i) |
| 446 |
($fx<= 0 i) |
| 447 |
($fx< i ($fxsub1 ($bytevector-length x)))) |
| 448 |
(case end |
| 449 |
[(big) |
| 450 |
($fxlogor |
| 451 |
($fxsll ($bytevector-s8-ref x i) 8) |
| 452 |
($bytevector-u8-ref x ($fxadd1 i)))] |
| 453 |
[(little) |
| 454 |
($fxlogor |
| 455 |
($fxsll ($bytevector-s8-ref x (fxadd1 i)) 8) |
| 456 |
($bytevector-u8-ref x i))] |
| 457 |
[else (die 'bytevector-s16-ref "invalid endianness" end)]) |
| 458 |
(die 'bytevector-s16-ref "invalid index" i)) |
| 459 |
(die 'bytevector-s16-ref "not a bytevector" x)))) |
| 460 |
|
| 461 |
(define bytevector-s16-set! |
| 462 |
(lambda (x i n end) |
| 463 |
(if (bytevector? x) |
| 464 |
(if (and (fixnum? n) |
| 465 |
($fx<= #x-8000 n) |
| 466 |
($fx<= n #x7FFF)) |
| 467 |
(if (and (fixnum? i) |
| 468 |
($fx<= 0 i) |
| 469 |
($fx< i ($fxsub1 ($bytevector-length x)))) |
| 470 |
(case end |
| 471 |
[(big) |
| 472 |
($bytevector-set! x i ($fxsra n 8)) |
| 473 |
($bytevector-set! x ($fxadd1 i) n)] |
| 474 |
[(little) |
| 475 |
($bytevector-set! x i n) |
| 476 |
($bytevector-set! x ($fxadd1 i) (fxsra n 8))] |
| 477 |
[else (die 'bytevector-s16-ref "invalid endianness" end)]) |
| 478 |
(die 'bytevector-s16-set! "invalid index" i)) |
| 479 |
(die 'bytevector-s16-set! "invalid value" n)) |
| 480 |
(die 'bytevector-s16-set! "not a bytevector" x)))) |
| 481 |
|
| 482 |
(define bytevector->u8-list |
| 483 |
(lambda (x) |
| 484 |
(unless (bytevector? x) |
| 485 |
(die 'bytevector->u8-list "not a bytevector" x)) |
| 486 |
(let f ([x x] [i ($bytevector-length x)] [ac '()]) |
| 487 |
(cond |
| 488 |
[($fx= i 0) ac] |
| 489 |
[else |
| 490 |
(let ([i ($fxsub1 i)]) |
| 491 |
(f x i (cons ($bytevector-u8-ref x i) ac)))])))) |
| 492 |
|
| 493 |
(define u8-list->bytevector |
| 494 |
(letrec ([race |
| 495 |
(lambda (h t ls n) |
| 496 |
(if (pair? h) |
| 497 |
(let ([h ($cdr h)]) |
| 498 |
(if (pair? h) |
| 499 |
(if (not (eq? h t)) |
| 500 |
(race ($cdr h) ($cdr t) ls ($fx+ n 2)) |
| 501 |
(die 'u8-list->bytevector "circular list" ls)) |
| 502 |
(if (null? h) |
| 503 |
($fx+ n 1) |
| 504 |
(die 'u8-list->bytevector "not a proper list" ls)))) |
| 505 |
(if (null? h) |
| 506 |
n |
| 507 |
(die 'u8-list->bytevector "not a proper list" ls))))] |
| 508 |
[fill |
| 509 |
(lambda (s i ls) |
| 510 |
(cond |
| 511 |
[(null? ls) s] |
| 512 |
[else |
| 513 |
(let ([c ($car ls)]) |
| 514 |
(unless (and (fixnum? c) ($fx<= 0 c) ($fx<= c 255)) |
| 515 |
(die 'u8-list->bytevector "not an octet" c)) |
| 516 |
($bytevector-set! s i c) |
| 517 |
(fill s ($fxadd1 i) (cdr ls)))]))]) |
| 518 |
(lambda (ls) |
| 519 |
(let ([n (race ls ls ls 0)]) |
| 520 |
(let ([s ($make-bytevector n)]) |
| 521 |
(fill s 0 ls)))))) |
| 522 |
|
| 523 |
(define bytevector-copy |
| 524 |
(lambda (src) |
| 525 |
(unless (bytevector? src) |
| 526 |
(die 'bytevector-copy "not a bytevector" src)) |
| 527 |
(let ([n ($bytevector-length src)]) |
| 528 |
(let f ([src src] [dst ($make-bytevector n)] [i 0] [n n]) |
| 529 |
(cond |
| 530 |
[($fx= i n) dst] |
| 531 |
[else |
| 532 |
($bytevector-set! dst i ($bytevector-u8-ref src i)) |
| 533 |
(f src dst ($fxadd1 i) n)]))))) |
| 534 |
|
| 535 |
(define bytevector=? |
| 536 |
(lambda (x y) |
| 537 |
(unless (bytevector? x) |
| 538 |
(die 'bytevector=? "not a bytevector" x)) |
| 539 |
(unless (bytevector? y) |
| 540 |
(die 'bytevector=? "not a bytevector" y)) |
| 541 |
(let ([n ($bytevector-length x)]) |
| 542 |
(and ($fx= n ($bytevector-length y)) |
| 543 |
(let f ([x x] [y y] [i 0] [n n]) |
| 544 |
(or ($fx= i n) |
| 545 |
(and ($fx= ($bytevector-u8-ref x i) |
| 546 |
($bytevector-u8-ref y i)) |
| 547 |
(f x y ($fxadd1 i) n)))))))) |
| 548 |
|
| 549 |
(define bytevector-copy! |
| 550 |
(lambda (src src-start dst dst-start k) |
| 551 |
(cond |
| 552 |
[(or (not (fixnum? src-start)) ($fx< src-start 0)) |
| 553 |
(die 'bytevector-copy! "not a valid starting index" src-start)] |
| 554 |
[(or (not (fixnum? dst-start)) ($fx< dst-start 0)) |
| 555 |
(die 'bytevector-copy! "not a valid starting index" dst-start)] |
| 556 |
[(or (not (fixnum? k)) ($fx< k 0)) |
| 557 |
(die 'bytevector-copy! "not a valid length" k)] |
| 558 |
[(not (bytevector? src)) |
| 559 |
(die 'bytevector-copy! "not a bytevector" src)] |
| 560 |
[(not (bytevector? dst)) |
| 561 |
(die 'bytevector-copy! "not a bytevector" dst)] |
| 562 |
[(let ([n ($fx+ src-start k)]) |
| 563 |
(or ($fx< n 0) ($fx> n ($bytevector-length src)))) |
| 564 |
(die 'bytevector-copy! "out of range" src-start k)] |
| 565 |
[(let ([n ($fx+ dst-start k)]) |
| 566 |
(or ($fx< n 0) ($fx> n ($bytevector-length dst)))) |
| 567 |
(die 'bytevector-copy! "out of range" dst-start k)] |
| 568 |
[(eq? src dst) |
| 569 |
(cond |
| 570 |
[($fx< dst-start src-start) |
| 571 |
(let f ([src src] [si src-start] [di dst-start] [sj ($fx+ src-start k)]) |
| 572 |
(unless ($fx= si sj) |
| 573 |
($bytevector-set! src di ($bytevector-u8-ref src si)) |
| 574 |
(f src ($fxadd1 si) ($fxadd1 di) sj)))] |
| 575 |
[($fx< src-start dst-start) |
| 576 |
(let f ([src src] [si ($fx+ src-start k)] [di ($fx+ dst-start k)] [sj src-start]) |
| 577 |
(unless ($fx= si sj) |
| 578 |
(let ([si ($fxsub1 si)] [di ($fxsub1 di)]) |
| 579 |
($bytevector-set! src di ($bytevector-u8-ref src si)) |
| 580 |
(f src si di sj))))] |
| 581 |
[else (void)])] |
| 582 |
[else |
| 583 |
(let f ([src src] [si src-start] [dst dst] [di dst-start] [sj ($fx+ src-start k)]) |
| 584 |
(unless ($fx= si sj) |
| 585 |
($bytevector-set! dst di ($bytevector-u8-ref src si)) |
| 586 |
(f src ($fxadd1 si) dst ($fxadd1 di) sj)))]))) |
| 587 |
|
| 588 |
(module (bytevector-uint-ref bytevector-sint-ref |
| 589 |
bytevector->uint-list bytevector->sint-list) |
| 590 |
(define (uref-big x ib il) ;; ib included, il excluded |
| 591 |
(cond |
| 592 |
[($fx= il ib) 0] |
| 593 |
[else |
| 594 |
(let ([b ($bytevector-u8-ref x ib)]) |
| 595 |
(cond |
| 596 |
[($fx= b 0) (uref-big x ($fxadd1 ib) il)] |
| 597 |
[else |
| 598 |
(case ($fx- il ib) |
| 599 |
[(1) b] |
| 600 |
[(2) ($fx+ ($fxsll b 8) |
| 601 |
($bytevector-u8-ref x ($fxsub1 il)))] |
| 602 |
[(3) |
| 603 |
($fx+ ($fxsll ($fx+ ($fxsll b 8) |
| 604 |
($bytevector-u8-ref x ($fxadd1 ib))) |
| 605 |
8) |
| 606 |
($bytevector-u8-ref x ($fxsub1 il)))] |
| 607 |
[else |
| 608 |
(let ([im ($fxsra ($fx+ il ib) 1)]) |
| 609 |
(+ (uref-big x im il) |
| 610 |
(* (uref-big x ib im) |
| 611 |
(expt 256 ($fx- il im)))))])]))])) |
| 612 |
(define (uref-little x il ib) ;; il included, ib excluded |
| 613 |
(cond |
| 614 |
[($fx= il ib) 0] |
| 615 |
[else |
| 616 |
(let ([ib^ ($fxsub1 ib)]) |
| 617 |
(let ([b ($bytevector-u8-ref x ib^)]) |
| 618 |
(cond |
| 619 |
[($fx= b 0) (uref-little x il ib^)] |
| 620 |
[else |
| 621 |
(case ($fx- ib il) |
| 622 |
[(1) b] |
| 623 |
[(2) ($fx+ ($fxsll b 8) ($bytevector-u8-ref x il))] |
| 624 |
[(3) |
| 625 |
($fx+ ($fxsll ($fx+ ($fxsll b 8) |
| 626 |
($bytevector-u8-ref x ($fxadd1 il))) |
| 627 |
8) |
| 628 |
($bytevector-u8-ref x il))] |
| 629 |
[else |
| 630 |
(let ([im ($fxsra ($fx+ il ib) 1)]) |
| 631 |
(+ (uref-little x il im) |
| 632 |
(* (uref-little x im ib) |
| 633 |
(expt 256 ($fx- im il)))))])])))])) |
| 634 |
(define (sref-big x ib il) ;; ib included, il excluded |
| 635 |
(cond |
| 636 |
[($fx= il ib) -1] |
| 637 |
[else |
| 638 |
(let ([b ($bytevector-u8-ref x ib)]) |
| 639 |
(cond |
| 640 |
[($fx= b 0) (uref-big x ($fxadd1 ib) il)] |
| 641 |
[($fx= b 255) (sref-big-neg x ($fxadd1 ib) il)] |
| 642 |
[($fx< b 128) (uref-big x ib il)] |
| 643 |
[else (- (uref-big x ib il) (expt 256 ($fx- il ib)))]))])) |
| 644 |
(define (sref-big-neg x ib il) ;; ib included, il excluded |
| 645 |
(cond |
| 646 |
[($fx= il ib) -1] |
| 647 |
[else |
| 648 |
(let ([b ($bytevector-u8-ref x ib)]) |
| 649 |
(cond |
| 650 |
[($fx= b 255) (sref-big-neg x ($fxadd1 ib) il)] |
| 651 |
[else (- (uref-big x ib il) (expt 256 ($fx- il ib)))]))])) |
| 652 |
(define (sref-little x il ib) ;; il included, ib excluded |
| 653 |
(cond |
| 654 |
[($fx= il ib) -1] |
| 655 |
[else |
| 656 |
(let ([ib^ ($fxsub1 ib)]) |
| 657 |
(let ([b ($bytevector-u8-ref x ib^)]) |
| 658 |
(cond |
| 659 |
[($fx= b 0) (uref-little x il ib^)] |
| 660 |
[($fx= b 255) (sref-little-neg x il ib^)] |
| 661 |
[($fx< b 128) (uref-little x il ib)] |
| 662 |
[else (- (uref-little x il ib) (expt 256 ($fx- ib il)))])))])) |
| 663 |
(define (sref-little-neg x il ib) ;; il included, ib excluded |
| 664 |
(cond |
| 665 |
[($fx= il ib) -1] |
| 666 |
[else |
| 667 |
(let ([ib^ ($fxsub1 ib)]) |
| 668 |
(let ([b ($bytevector-u8-ref x ib^)]) |
| 669 |
(cond |
| 670 |
[($fx= b 255) (sref-little-neg x il ib^)] |
| 671 |
[else (- (uref-little x il ib) (expt 256 ($fx- ib il)))])))])) |
| 672 |
(define bytevector-sint-ref |
| 673 |
(lambda (x k endianness size) |
| 674 |
(define who 'bytevector-sint-ref) |
| 675 |
(unless (bytevector? x) (die who "not a bytevector" x)) |
| 676 |
(unless (and (fixnum? k) ($fx>= k 0)) (die who "invalid index" k)) |
| 677 |
(unless (and (fixnum? size) ($fx>= size 1)) (die who "invalid size" size)) |
| 678 |
(let ([n ($bytevector-length x)]) |
| 679 |
(unless ($fx< k n) (die who "index is out of range" k)) |
| 680 |
(let ([end ($fx+ k size)]) |
| 681 |
(unless (and ($fx>= end 0) ($fx<= end n)) |
| 682 |
(die who "out of range" k size)) |
| 683 |
(case endianness |
| 684 |
[(little) (sref-little x k end)] |
| 685 |
[(big) (sref-big x k end)] |
| 686 |
[else (die who "invalid endianness" endianness)]))))) |
| 687 |
(define bytevector-uint-ref |
| 688 |
(lambda (x k endianness size) |
| 689 |
(define who 'bytevector-uint-ref) |
| 690 |
(unless (bytevector? x) (die who "not a bytevector" x)) |
| 691 |
(unless (and (fixnum? k) ($fx>= k 0)) (die who "invalid index" k)) |
| 692 |
(unless (and (fixnum? size) ($fx>= size 1)) (die who "invalid size" size)) |
| 693 |
(let ([n ($bytevector-length x)]) |
| 694 |
(unless ($fx< k n) (die who "index is out of range" k)) |
| 695 |
(let ([end ($fx+ k size)]) |
| 696 |
(unless (and ($fx>= end 0) ($fx<= end n)) |
| 697 |
(die who "out of range" k size)) |
| 698 |
(case endianness |
| 699 |
[(little) (uref-little x k end)] |
| 700 |
[(big) (uref-big x k end)] |
| 701 |
[else (die who "invalid endianness" endianness)]))))) |
| 702 |
(define (bytevector->some-list x k n ls proc who) |
| 703 |
(cond |
| 704 |
[($fx= n 0) ls] |
| 705 |
[else |
| 706 |
(let ([i ($fx- n k)]) |
| 707 |
(cond |
| 708 |
[($fx>= i 0) |
| 709 |
(bytevector->some-list x k i (cons (proc x i n) ls) proc who)] |
| 710 |
[else |
| 711 |
(die who "invalid size" k)]))])) |
| 712 |
(define bytevector->uint-list |
| 713 |
(lambda (x endianness size) |
| 714 |
(define who 'bytevector->uint-list) |
| 715 |
(unless (bytevector? x) (die who "not a bytevector" x)) |
| 716 |
(unless (and (fixnum? size) ($fx>= size 1)) (die who "invalid size" size)) |
| 717 |
(case endianness |
| 718 |
[(little) (bytevector->some-list x size ($bytevector-length x) |
| 719 |
'() uref-little 'bytevector->uint-list)] |
| 720 |
[(big) (bytevector->some-list x size ($bytevector-length x) |
| 721 |
'() uref-big 'bytevector->uint-list)] |
| 722 |
[else (die who "invalid endianness" endianness)]))) |
| 723 |
(define bytevector->sint-list |
| 724 |
(lambda (x endianness size) |
| 725 |
(define who 'bytevector->sint-list) |
| 726 |
(unless (bytevector? x) (die who "not a bytevector" x)) |
| 727 |
(unless (and (fixnum? size) ($fx>= size 1)) (die who "invalid size" size)) |
| 728 |
(case endianness |
| 729 |
[(little) (bytevector->some-list x size ($bytevector-length x) |
| 730 |
'() sref-little 'bytevector->sint-list)] |
| 731 |
[(big) (bytevector->some-list x size ($bytevector-length x) |
| 732 |
'() sref-big 'bytevector->sint-list)] |
| 733 |
[else (die who "invalid endianness" endianness)])))) |
| 734 |
|
| 735 |
|
| 736 |
(define (bytevector-uint-set! bv i0 n endianness size) |
| 737 |
(define who 'bytevector-uint-set!) |
| 738 |
(bytevector-uint-set!/who bv i0 n endianness size who)) |
| 739 |
|
| 740 |
(define (bytevector-uint-set!/who bv i0 n endianness size who) |
| 741 |
(unless (bytevector? bv) |
| 742 |
(die who "not a bytevector" bv)) |
| 743 |
(unless (or (fixnum? n) (bignum? n)) |
| 744 |
(die who "not an exact number" n)) |
| 745 |
(unless (>= n 0) |
| 746 |
(die who "number must be positive" n)) |
| 747 |
(let ([bvsize ($bytevector-length bv)]) |
| 748 |
(unless (and (fixnum? i0) |
| 749 |
($fx>= i0 0) |
| 750 |
($fx< i0 bvsize)) |
| 751 |
(die who "invalid index" i0)) |
| 752 |
(unless (and (fixnum? size) |
| 753 |
($fx> size 0) |
| 754 |
($fx<= i0 ($fx- bvsize size))) |
| 755 |
(die who "invalid size" size))) |
| 756 |
(let ([nsize (bitwise-length n)]) |
| 757 |
(when (< (* size 8) nsize) |
| 758 |
(die who |
| 759 |
(format "number does not fit in ~a byte~a" size (if (= size 1) "" "s")) |
| 760 |
n))) |
| 761 |
(case endianness |
| 762 |
[(little) |
| 763 |
(let f ([bv bv] [i0 i0] [i1 (fx+ i0 size)] [n n]) |
| 764 |
(unless ($fx= i0 i1) |
| 765 |
($bytevector-set! bv i0 (bitwise-and n 255)) |
| 766 |
(f bv ($fx+ i0 1) i1 (sra n 8))))] |
| 767 |
[(big) |
| 768 |
(let f ([bv bv] [i0 i0] [i1 (fx+ i0 size)] [n n]) |
| 769 |
(unless ($fx= i0 i1) |
| 770 |
(let ([i1 ($fx- i1 1)]) |
| 771 |
($bytevector-set! bv i1 (bitwise-and n 255)) |
| 772 |
(f bv i0 i1 (sra n 8)))))] |
| 773 |
[else (die who "invalid endianness" endianness)])) |
| 774 |
|
| 775 |
|
| 776 |
(define (bytevector-sint-set! bv i0 n endianness size) |
| 777 |
(define who 'bytevector-sint-set!) |
| 778 |
(bytevector-sint-set!/who bv i0 n endianness size who)) |
| 779 |
|
| 780 |
(define (bytevector-sint-set!/who bv i0 n endianness size who) |
| 781 |
(unless (bytevector? bv) |
| 782 |
(die who "not a bytevector" bv)) |
| 783 |
(unless (or (fixnum? n) (bignum? n)) |
| 784 |
(die who "not an exact number" n)) |
| 785 |
(let ([bvsize ($bytevector-length bv)]) |
| 786 |
(unless (and (fixnum? i0) |
| 787 |
($fx>= i0 0) |
| 788 |
($fx< i0 bvsize)) |
| 789 |
(die who "invalid index" i0)) |
| 790 |
(unless (and (fixnum? size) |
| 791 |
($fx> size 0) |
| 792 |
($fx<= i0 ($fx- bvsize size))) |
| 793 |
(die who "invalid size" size))) |
| 794 |
(let ([nsize (+ (bitwise-length n) 1)]) |
| 795 |
(when (< (* size 8) nsize) |
| 796 |
(die who "number does not fit" n))) |
| 797 |
(case endianness |
| 798 |
[(little) |
| 799 |
(let f ([bv bv] [i0 i0] [i1 (fx+ i0 size)] [n n]) |
| 800 |
(unless ($fx= i0 i1) |
| 801 |
($bytevector-set! bv i0 (bitwise-and n 255)) |
| 802 |
(f bv ($fx+ i0 1) i1 (sra n 8))))] |
| 803 |
[(big) |
| 804 |
(let f ([bv bv] [i0 i0] [i1 (fx+ i0 size)] [n n]) |
| 805 |
(unless ($fx= i0 i1) |
| 806 |
(let ([i1 ($fx- i1 1)]) |
| 807 |
($bytevector-set! bv i1 (bitwise-and n 255)) |
| 808 |
(f bv i0 i1 (sra n 8)))))] |
| 809 |
[else (die who "invalid endianness" endianness)])) |
| 810 |
|
| 811 |
|
| 812 |
(module (uint-list->bytevector sint-list->bytevector) |
| 813 |
(define (make-xint-list->bytevector who bv-set!) |
| 814 |
(define (race h t ls idx endianness size) |
| 815 |
(if (pair? h) |
| 816 |
(let ([h ($cdr h)] [a ($car h)]) |
| 817 |
(if (pair? h) |
| 818 |
(if (not (eq? h t)) |
| 819 |
(let ([bv (race ($cdr h) ($cdr t) ls |
| 820 |
($fx+ idx ($fx+ size size)) |
| 821 |
endianness size)]) |
| 822 |
(bv-set! bv idx a endianness size who) |
| 823 |
(bv-set! bv ($fx+ idx size) ($car h) endianness size who) |
| 824 |
bv) |
| 825 |
(die who "circular list" ls)) |
| 826 |
(if (null? h) |
| 827 |
(let ([bv (make-bytevector ($fx+ idx size))]) |
| 828 |
(bv-set! bv idx a endianness size who) |
| 829 |
bv) |
| 830 |
(die who "not a proper list" ls)))) |
| 831 |
(if (null? h) |
| 832 |
(make-bytevector idx) |
| 833 |
(die who "not a proper list" ls)))) |
| 834 |
(lambda (ls endianness size) |
| 835 |
(if (and (fixnum? size) (fx> size 0)) |
| 836 |
(race ls ls ls 0 endianness size) |
| 837 |
(die who "size must be a positive integer" size)))) |
| 838 |
(define uint-list->bytevector |
| 839 |
(make-xint-list->bytevector |
| 840 |
'uint-list->bytevector bytevector-uint-set!/who)) |
| 841 |
(define sint-list->bytevector |
| 842 |
(make-xint-list->bytevector |
| 843 |
'sint-list->bytevector bytevector-sint-set!/who))) |
| 844 |
|
| 845 |
(define (bytevector-ieee-double-native-ref bv i) |
| 846 |
(if (bytevector? bv) |
| 847 |
(if (and (fixnum? i) |
| 848 |
($fx>= i 0) |
| 849 |
($fxzero? ($fxlogand i 7)) |
| 850 |
($fx< i ($bytevector-length bv))) |
| 851 |
($bytevector-ieee-double-native-ref bv i) |
| 852 |
(die 'bytevector-ieee-double-native-ref "invalid index" i)) |
| 853 |
(die 'bytevector-ieee-double-native-ref "not a bytevector" bv))) |
| 854 |
|
| 855 |
(define (bytevector-ieee-single-native-ref bv i) |
| 856 |
(if (bytevector? bv) |
| 857 |
(if (and (fixnum? i) |
| 858 |
($fx>= i 0) |
| 859 |
($fxzero? ($fxlogand i 3)) |
| 860 |
($fx< i ($bytevector-length bv))) |
| 861 |
($bytevector-ieee-single-native-ref bv i) |
| 862 |
(die 'bytevector-ieee-single-native-ref "invalid index" i)) |
| 863 |
(die 'bytevector-ieee-single-native-ref "not a bytevector" bv))) |
| 864 |
|
| 865 |
(define (bytevector-ieee-double-native-set! bv i x) |
| 866 |
(if (bytevector? bv) |
| 867 |
(if (and (fixnum? i) |
| 868 |
($fx>= i 0) |
| 869 |
($fxzero? ($fxlogand i 7)) |
| 870 |
($fx< i ($bytevector-length bv))) |
| 871 |
(if (flonum? x) |
| 872 |
($bytevector-ieee-double-native-set! bv i x) |
| 873 |
(die 'bytevector-ieee-double-native-set! "not a flonum" x)) |
| 874 |
(die 'bytevector-ieee-double-native-set! "invalid index" i)) |
| 875 |
(die 'bytevector-ieee-double-native-set! "not a bytevector" bv))) |
| 876 |
|
| 877 |
(define (bytevector-ieee-single-native-set! bv i x) |
| 878 |
(if (bytevector? bv) |
| 879 |
(if (and (fixnum? i) |
| 880 |
($fx>= i 0) |
| 881 |
($fxzero? ($fxlogand i 3)) |
| 882 |
($fx< i ($bytevector-length bv))) |
| 883 |
(if (flonum? x) |
| 884 |
($bytevector-ieee-single-native-set! bv i x) |
| 885 |
(die 'bytevector-ieee-single-native-set! "not a flonum" x)) |
| 886 |
(die 'bytevector-ieee-single-native-set! "invalid index" i)) |
| 887 |
(die 'bytevector-ieee-single-native-set! "not a bytevector" bv))) |
| 888 |
|
| 889 |
(define ($bytevector-ieee-double-ref/big x i) |
| 890 |
(import (ikarus system $flonums)) |
| 891 |
(let ([y ($make-flonum)]) |
| 892 |
($flonum-set! y 0 ($bytevector-u8-ref x i)) |
| 893 |
($flonum-set! y 1 ($bytevector-u8-ref x ($fx+ i 1))) |
| 894 |
($flonum-set! y 2 ($bytevector-u8-ref x ($fx+ i 2))) |
| 895 |
($flonum-set! y 3 ($bytevector-u8-ref x ($fx+ i 3))) |
| 896 |
($flonum-set! y 4 ($bytevector-u8-ref x ($fx+ i 4))) |
| 897 |
($flonum-set! y 5 ($bytevector-u8-ref x ($fx+ i 5))) |
| 898 |
($flonum-set! y 6 ($bytevector-u8-ref x ($fx+ i 6))) |
| 899 |
($flonum-set! y 7 ($bytevector-u8-ref x ($fx+ i 7))) |
| 900 |
y)) |
| 901 |
|
| 902 |
(define ($bytevector-ieee-double-set!/big x i y) |
| 903 |
(import (ikarus system $flonums)) |
| 904 |
($bytevector-set! x i ($flonum-u8-ref y 0)) |
| 905 |
($bytevector-set! x ($fx+ i 1) ($flonum-u8-ref y 1)) |
| 906 |
($bytevector-set! x ($fx+ i 2) ($flonum-u8-ref y 2)) |
| 907 |
($bytevector-set! x ($fx+ i 3) ($flonum-u8-ref y 3)) |
| 908 |
($bytevector-set! x ($fx+ i 4) ($flonum-u8-ref y 4)) |
| 909 |
($bytevector-set! x ($fx+ i 5) ($flonum-u8-ref y 5)) |
| 910 |
($bytevector-set! x ($fx+ i 6) ($flonum-u8-ref y 6)) |
| 911 |
($bytevector-set! x ($fx+ i 7) ($flonum-u8-ref y 7))) |
| 912 |
|
| 913 |
(define ($bytevector-ieee-double-ref/little x i) |
| 914 |
(import (ikarus system $flonums)) |
| 915 |
(let ([y ($make-flonum)]) |
| 916 |
($flonum-set! y 7 ($bytevector-u8-ref x i)) |
| 917 |
($flonum-set! y 6 ($bytevector-u8-ref x ($fx+ i 1))) |
| 918 |
($flonum-set! y 5 ($bytevector-u8-ref x ($fx+ i 2))) |
| 919 |
($flonum-set! y 4 ($bytevector-u8-ref x ($fx+ i 3))) |
| 920 |
($flonum-set! y 3 ($bytevector-u8-ref x ($fx+ i 4))) |
| 921 |
($flonum-set! y 2 ($bytevector-u8-ref x ($fx+ i 5))) |
| 922 |
($flonum-set! y 1 ($bytevector-u8-ref x ($fx+ i 6))) |
| 923 |
($flonum-set! y 0 ($bytevector-u8-ref x ($fx+ i 7))) |
| 924 |
y)) |
| 925 |
|
| 926 |
(define ($bytevector-ieee-double-set!/little x i y) |
| 927 |
(import (ikarus system $flonums)) |
| 928 |
($bytevector-set! x i ($flonum-u8-ref y 7)) |
| 929 |
($bytevector-set! x ($fx+ i 1) ($flonum-u8-ref y 6)) |
| 930 |
($bytevector-set! x ($fx+ i 2) ($flonum-u8-ref y 5)) |
| 931 |
($bytevector-set! x ($fx+ i 3) ($flonum-u8-ref y 4)) |
| 932 |
($bytevector-set! x ($fx+ i 4) ($flonum-u8-ref y 3)) |
| 933 |
($bytevector-set! x ($fx+ i 5) ($flonum-u8-ref y 2)) |
| 934 |
($bytevector-set! x ($fx+ i 6) ($flonum-u8-ref y 1)) |
| 935 |
($bytevector-set! x ($fx+ i 7) ($flonum-u8-ref y 0))) |
| 936 |
|
| 937 |
(define ($bytevector-ieee-single-ref/little x i) |
| 938 |
(let ([bv (make-bytevector 4)]) |
| 939 |
($bytevector-set! bv 0 ($bytevector-u8-ref x i)) |
| 940 |
($bytevector-set! bv 1 ($bytevector-u8-ref x ($fx+ i 1))) |
| 941 |
($bytevector-set! bv 2 ($bytevector-u8-ref x ($fx+ i 2))) |
| 942 |
($bytevector-set! bv 3 ($bytevector-u8-ref x ($fx+ i 3))) |
| 943 |
($bytevector-ieee-single-native-ref bv 0))) |
| 944 |
|
| 945 |
(define ($bytevector-ieee-single-ref/big x i) |
| 946 |
(let ([bv (make-bytevector 4)]) |
| 947 |
($bytevector-set! bv 3 ($bytevector-u8-ref x i)) |
| 948 |
($bytevector-set! bv 2 ($bytevector-u8-ref x ($fx+ i 1))) |
| 949 |
($bytevector-set! bv 1 ($bytevector-u8-ref x ($fx+ i 2))) |
| 950 |
($bytevector-set! bv 0 ($bytevector-u8-ref x ($fx+ i 3))) |
| 951 |
($bytevector-ieee-single-native-ref bv 0))) |
| 952 |
|
| 953 |
(define ($bytevector-ieee-single-set!/little x i v) |
| 954 |
(let ([bv (make-bytevector 4)]) |
| 955 |
($bytevector-ieee-single-native-set! bv 0 v) |
| 956 |
($bytevector-set! x i ($bytevector-u8-ref bv 0)) |
| 957 |
($bytevector-set! x ($fx+ i 1) ($bytevector-u8-ref bv 1)) |
| 958 |
($bytevector-set! x ($fx+ i 2) ($bytevector-u8-ref bv 2)) |
| 959 |
($bytevector-set! x ($fx+ i 3) ($bytevector-u8-ref bv 3)))) |
| 960 |
|
| 961 |
(define ($bytevector-ieee-single-set!/big x i v) |
| 962 |
(let ([bv (make-bytevector 4)]) |
| 963 |
($bytevector-ieee-single-native-set! bv 0 v) |
| 964 |
($bytevector-set! x i ($bytevector-u8-ref bv 3)) |
| 965 |
($bytevector-set! x ($fx+ i 1) ($bytevector-u8-ref bv 2)) |
| 966 |
($bytevector-set! x ($fx+ i 2) ($bytevector-u8-ref bv 1)) |
| 967 |
($bytevector-set! x ($fx+ i 3) ($bytevector-u8-ref bv 0)))) |
| 968 |
|
| 969 |
(define (bytevector-ieee-double-ref bv i endianness) |
| 970 |
(define who 'bytevector-ieee-double-ref) |
| 971 |
(if (bytevector? bv) |
| 972 |
(if (and (fixnum? i) ($fx>= i 0)) |
| 973 |
(let ([len ($bytevector-length bv)]) |
| 974 |
(if (and ($fxzero? ($fxlogand i 7)) ($fx< i len)) |
| 975 |
(case endianness |
| 976 |
[(little) ($bytevector-ieee-double-native-ref bv i)] |
| 977 |
[(big) ($bytevector-ieee-double-nonnative-ref bv i)] |
| 978 |
[else (die who "invalid endianness" endianness)]) |
| 979 |
(if ($fx<= i ($fx- len 8)) |
| 980 |
(case endianness |
| 981 |
[(little) |
| 982 |
($bytevector-ieee-double-ref/little bv i)] |
| 983 |
[(big) |
| 984 |
($bytevector-ieee-double-ref/big bv i)] |
| 985 |
[else (die who "invalid endianness" endianness)]) |
| 986 |
(die who "invalid index" i)))) |
| 987 |
(die who "invalid index" i)) |
| 988 |
(die who "not a bytevector" bv))) |
| 989 |
|
| 990 |
(define (bytevector-ieee-single-ref bv i endianness) |
| 991 |
(define who 'bytevector-ieee-single-ref) |
| 992 |
(if (bytevector? bv) |
| 993 |
(if (and (fixnum? i) ($fx>= i 0)) |
| 994 |
(let ([len ($bytevector-length bv)]) |
| 995 |
(if (and ($fxzero? ($fxlogand i 3)) ($fx< i len)) |
| 996 |
(case endianness |
| 997 |
[(little) ($bytevector-ieee-single-native-ref bv i)] |
| 998 |
[(big) ($bytevector-ieee-single-nonnative-ref bv i)] |
| 999 |
[else (die who "invalid endianness" endianness)]) |
| 1000 |
(if ($fx<= i ($fx- len 4)) |
| 1001 |
(case endianness |
| 1002 |
[(little) |
| 1003 |
($bytevector-ieee-single-ref/little bv i)] |
| 1004 |
[(big) |
| 1005 |
($bytevector-ieee-single-ref/big bv i)] |
| 1006 |
[else (die who "invalid endianness" endianness)]) |
| 1007 |
(die who "invalid index" i)))) |
| 1008 |
(die who "invalid index" i)) |
| 1009 |
(die who "not a bytevector" bv))) |
| 1010 |
|
| 1011 |
(define (bytevector-ieee-double-set! bv i x endianness) |
| 1012 |
(define who 'bytevector-ieee-double-set!) |
| 1013 |
(if (bytevector? bv) |
| 1014 |
(if (flonum? x) |
| 1015 |
(if (and (fixnum? i) ($fx>= i 0)) |
| 1016 |
(let ([len ($bytevector-length bv)]) |
| 1017 |
(if (and ($fxzero? ($fxlogand i 7)) ($fx< i len)) |
| 1018 |
(case endianness |
| 1019 |
[(little) ($bytevector-ieee-double-native-set! bv i x)] |
| 1020 |
[(big) ($bytevector-ieee-double-nonnative-set! bv i x)] |
| 1021 |
[else |
| 1022 |
(die who "invalid endianness" endianness)]) |
| 1023 |
(if ($fx<= i ($fx- len 8)) |
| 1024 |
(case endianness |
| 1025 |
[(little) |
| 1026 |
($bytevector-ieee-double-set!/little bv i x)] |
| 1027 |
[(big) |
| 1028 |
($bytevector-ieee-double-set!/big bv i x)] |
| 1029 |
[else |
| 1030 |
(die who "invalid endianness" endianness)]) |
| 1031 |
(die who "invalid index" i)))) |
| 1032 |
(die who "invalid index" i)) |
| 1033 |
(die who "not a flonum" x)) |
| 1034 |
(die who "not a bytevector" bv))) |
| 1035 |
|
| 1036 |
(define (bytevector-ieee-single-set! bv i x endianness) |
| 1037 |
(define who 'bytevector-ieee-single-set!) |
| 1038 |
(if (bytevector? bv) |
| 1039 |
(if (flonum? x) |
| 1040 |
(if (and (fixnum? i) ($fx>= i 0)) |
| 1041 |
(let ([len ($bytevector-length bv)]) |
| 1042 |
(if (and ($fxzero? ($fxlogand i 3)) ($fx< i len)) |
| 1043 |
(case endianness |
| 1044 |
[(little) ($bytevector-ieee-single-native-set! bv i x)] |
| 1045 |
[(big) ($bytevector-ieee-single-nonnative-set! bv i x)] |
| 1046 |
[else |
| 1047 |
(die who "invalid endianness" endianness)]) |
| 1048 |
(if ($fx<= i ($fx- len 4)) |
| 1049 |
(case endianness |
| 1050 |
[(little) |
| 1051 |
($bytevector-ieee-single-set!/little bv i x)] |
| 1052 |
[(big) |
| 1053 |
($bytevector-ieee-single-set!/big bv i x)] |
| 1054 |
[else |
| 1055 |
(die who "invalid endianness" endianness)]) |
| 1056 |
(die who "invalid index" i)))) |
| 1057 |
(die who "invalid index" i)) |
| 1058 |
(die who "not a flonum" x)) |
| 1059 |
(die who "not a bytevector" bv))) |
| 1060 |
|
| 1061 |
(define ($bytevector-ref/64/aligned bv i who decoder endianness) |
| 1062 |
(if (bytevector? bv) |
| 1063 |
(if (and (fixnum? i) |
| 1064 |
($fx>= i 0) |
| 1065 |
($fxzero? ($fxlogand i 7)) |
| 1066 |
($fx< i ($bytevector-length bv))) |
| 1067 |
(case endianness |
| 1068 |
[(little big) |
| 1069 |
(decoder bv i endianness 8)] |
| 1070 |
[else (die who "invalid endianness" endianness)]) |
| 1071 |
(die who "invalid index" i)) |
| 1072 |
(die who "not a bytevector" bv))) |
| 1073 |
|
| 1074 |
(define ($bytevector-ref/64 bv i who decoder endianness) |
| 1075 |
(if (bytevector? bv) |
| 1076 |
(if (and (fixnum? i) |
| 1077 |
($fx>= i 0) |
| 1078 |
($fx< i ($fx- ($bytevector-length bv) 7))) |
| 1079 |
(case endianness |
| 1080 |
[(little big) |
| 1081 |
(decoder bv i endianness 8)] |
| 1082 |
[else (die who "invalid endianness" endianness)]) |
| 1083 |
(die who "invalid index" i)) |
| 1084 |
(die who "not a bytevector" bv))) |
| 1085 |
|
| 1086 |
(define (bytevector-u64-native-ref bv i) |
| 1087 |
($bytevector-ref/64/aligned bv i 'bytevector-u64-native-ref |
| 1088 |
bytevector-uint-ref 'little)) |
| 1089 |
(define (bytevector-s64-native-ref bv i) |
| 1090 |
($bytevector-ref/64/aligned bv i 'bytevector-s64-native-ref |
| 1091 |
bytevector-sint-ref 'little)) |
| 1092 |
(define (bytevector-u64-ref bv i endianness) |
| 1093 |
($bytevector-ref/64 bv i 'bytevector-u64-ref |
| 1094 |
bytevector-uint-ref endianness)) |
| 1095 |
(define (bytevector-s64-ref bv i endianness) |
| 1096 |
($bytevector-ref/64 bv i 'bytevector-s64-ref |
| 1097 |
bytevector-sint-ref endianness)) |
| 1098 |
|
| 1099 |
(define ($bytevector-set/64/align bv i n lo hi who setter endianness) |
| 1100 |
(if (bytevector? bv) |
| 1101 |
(if (and (fixnum? i) |
| 1102 |
($fx>= i 0) |
| 1103 |
($fxzero? ($fxlogand i 7)) |
| 1104 |
($fx< i ($bytevector-length bv))) |
| 1105 |
(case endianness |
| 1106 |
[(little big) |
| 1107 |
(unless (or (fixnum? n) (bignum? n)) |
| 1108 |
(die who "number is not an exact number" n)) |
| 1109 |
(unless (and (<= lo n) (< n hi)) |
| 1110 |
(die who "number out of range" n)) |
| 1111 |
(setter bv i n endianness 8)] |
| 1112 |
[else (die who "invalid endianness" endianness)]) |
| 1113 |
(die who "invalid index" i)) |
| 1114 |
(die who "not a bytevector" bv))) |
| 1115 |
|
| 1116 |
(define ($bytevector-set/64 bv i n lo hi who setter endianness) |
| 1117 |
(if (bytevector? bv) |
| 1118 |
(if (and (fixnum? i) |
| 1119 |
($fx>= i 0) |
| 1120 |
($fx< i ($fx- ($bytevector-length bv) 7))) |
| 1121 |
(case endianness |
| 1122 |
[(little big) |
| 1123 |
(unless (or (fixnum? n) (bignum? n)) |
| 1124 |
(die who "number is not exact number" n)) |
| 1125 |
(unless (and (<= lo n) (< n hi)) |
| 1126 |
(die who "number out of range" n)) |
| 1127 |
(setter bv i n endianness 8)] |
| 1128 |
[else (die who "invalid endianness" endianness)]) |
| 1129 |
(die who "invalid index" i)) |
| 1130 |
(die who "not a bytevector" bv))) |
| 1131 |
|
| 1132 |
|
| 1133 |
(define (bytevector-u64-native-set! bv i n) |
| 1134 |
($bytevector-set/64/align bv i n 0 (expt 2 64) |
| 1135 |
'bytevector-u64-native-set! bytevector-uint-set! 'little)) |
| 1136 |
(define (bytevector-s64-native-set! bv i n) |
| 1137 |
($bytevector-set/64/align bv i n (- (expt 2 63)) (expt 2 63) |
| 1138 |
'bytevector-s64-native-set! bytevector-sint-set! 'little)) |
| 1139 |
(define (bytevector-u64-set! bv i n endianness) |
| 1140 |
($bytevector-set/64 bv i n 0 (expt 2 64) |
| 1141 |
'bytevector-u64-set! bytevector-uint-set! endianness)) |
| 1142 |
(define (bytevector-s64-set! bv i n endianness) |
| 1143 |
($bytevector-set/64 bv i n (- (expt 2 63)) (expt 2 63) |
| 1144 |
'bytevector-s64-set! bytevector-sint-set! endianness)) |
| 1145 |
|
| 1146 |
) |
| 1147 |
|
| 1148 |
(library (ikarus system bytevectors) |
| 1149 |
(export $bytevector-u8-ref $bytevector-length $make-bytevector) |
| 1150 |
(import (ikarus)) |
| 1151 |
(define $bytevector-u8-ref bytevector-u8-ref) |
| 1152 |
(define $bytevector-length bytevector-length) |
| 1153 |
(define $make-bytevector make-bytevector)) |