-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdx_vector.m
More file actions
executable file
·645 lines (490 loc) · 24.5 KB
/
Copy pathdx_vector.m
File metadata and controls
executable file
·645 lines (490 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
% Copyright (c) 2019 Alaskan Emily, Transnat Games
%
% This software is provided 'as-is', without any express or implied warranty.
% In no event will the authors be held liable for any damages arising from
% the use of this software.
%
% Permission is granted to anyone to use this software for any purpose,
% including commercial applications, and to alter it and redistribute it
% freely, subject to the following restrictions:
%
% 1. The origin of this software must not be misrepresented; you must not
% claim that you wrote the original software. If you use this software
% in a product, an acknowledgment in the product documentation would be
% appreciated but is not required.
%
% 2. Altered source versions must be plainly marked as such, and must not
% be misrepresented as being the original software.
%
% 3. This notice may not be removed or altered from any source distribution.
%
:- module dx_vector.
%=============================================================================%
% Bindings to DirectX Math vector4.
% Provides pure Mercury fallbacks for grades that do not interface with C.
:- interface.
%=============================================================================%
:- type vector. % ---> vector(x::float, y::float, z::float, w::float).
%-----------------------------------------------------------------------------%
% Used to simulate a vector functor.
:- func vector(float, float, float, float) = vector.
:- mode vector(in, in, in, in) = uo is det.
:- mode vector(uo, uo, uo, uo) = in is det.
%-----------------------------------------------------------------------------%
% Field access functions.
:- func x(vector) = float.
:- func y(vector) = float.
:- func z(vector) = float.
:- func w(vector) = float.
%-----------------------------------------------------------------------------%
:- func 'x :='(vector, float) = vector.
:- func 'y :='(vector, float) = vector.
:- func 'z :='(vector, float) = vector.
:- func 'w :='(vector, float) = vector.
%-----------------------------------------------------------------------------%
% Arithmetic operations. These are multi-moded to allow destructive update,
% limiting the number of allocations that must occur.
:- func (vector) + (vector) = (vector).
:- mode (in) + (in) = (uo) is det.
:- mode (di) + (in) = (uo) is det.
:- mode (in) + (di) = (uo) is det.
:- mode (di) + (di) = (uo) is det.
:- func plus(vector, vector) = (vector).
:- mode plus(in, in) = (uo) is det.
:- mode plus(di, in) = (uo) is det.
:- mode plus(in, di) = (uo) is det.
:- mode plus(di, di) = (uo) is det.
:- pred plus(vector, vector, vector).
:- mode plus(in, in, uo) is det.
:- mode plus(di, in, uo) is det.
:- mode plus(in, di, uo) is det.
:- mode plus(di, di, uo) is det.
%-----------------------------------------------------------------------------%
:- func (vector) - (vector) = (vector).
:- mode (in) - (in) = (uo) is det.
:- mode (di) - (in) = (uo) is det.
:- mode (in) - (di) = (uo) is det.
:- mode (di) - (di) = (uo) is det.
:- func minus(vector, vector) = (vector).
:- mode minus(in, in) = (uo) is det.
:- mode minus(di, in) = (uo) is det.
:- mode minus(in, di) = (uo) is det.
:- mode minus(di, di) = (uo) is det.
:- pred minus(vector, vector, vector).
:- mode minus(in, in, uo) is det.
:- mode minus(di, in, uo) is det.
:- mode minus(in, di, uo) is det.
:- mode minus(di, di, uo) is det.
%-----------------------------------------------------------------------------%
:- func (vector) * (vector) = (vector).
:- mode (in) * (in) = (uo) is det.
:- mode (di) * (in) = (uo) is det.
:- mode (in) * (di) = (uo) is det.
:- mode (di) * (di) = (uo) is det.
:- func times(vector, vector) = (vector).
:- mode times(in, in) = (uo) is det.
:- mode times(di, in) = (uo) is det.
:- mode times(in, di) = (uo) is det.
:- mode times(di, di) = (uo) is det.
:- pred times(vector, vector, vector).
:- mode times(in, in, uo) is det.
:- mode times(di, in, uo) is det.
:- mode times(in, di, uo) is det.
:- mode times(di, di, uo) is det.
%-----------------------------------------------------------------------------%
:- func (vector) / (vector) = (vector).
:- mode (in) / (in) = (uo) is det.
:- mode (di) / (in) = (uo) is det.
:- mode (in) / (di) = (uo) is det.
:- mode (di) / (di) = (uo) is det.
:- func div(vector, vector) = (vector).
:- mode div(in, in) = (uo) is det.
:- mode div(di, in) = (uo) is det.
:- mode div(in, di) = (uo) is det.
:- mode div(di, di) = (uo) is det.
:- pred div(vector, vector, vector).
:- mode div(in, in, uo) is det.
:- mode div(di, in, uo) is det.
:- mode div(in, di, uo) is det.
:- mode div(di, di, uo) is det.
%-----------------------------------------------------------------------------%
% Unary plus.
:- func +(vector) = (vector).
:- mode +(di) = (uo) is det.
:- mode +(in) = (out) is det.
:- mode +(mdi) = (muo) is det.
%-----------------------------------------------------------------------------%
:- func -(vector) = (vector).
:- mode -(di) = (uo) is det.
:- mode -(in) = (uo) is det.
:- func negate(vector) = (vector).
:- mode negate(di) = (uo) is det.
:- mode negate(in) = (uo) is det.
:- pred negate(vector, vector).
:- mode negate(di, uo) is det.
:- mode negate(in, uo) is det.
:- mode negate(uo, in) is det.
:- mode negate(uo, di) is det.
%-----------------------------------------------------------------------------%
:- func scale(vector, float) = (vector) .
:- mode scale(in, in) = (uo) is det.
:- mode scale(di, in) = (uo) is det.
:- pred scale(vector, float, vector).
:- mode scale(in, in, uo) is det.
:- mode scale(di, in, uo) is det.
%-----------------------------------------------------------------------------%
:- func length(vector::in) = (float::uo) is det.
:- pred length(vector::in, float::uo) is det.
%-----------------------------------------------------------------------------%
:- func length_squared(vector::in) = (float::uo) is det.
:- pred length_squared(vector::in, float::uo) is det.
%-----------------------------------------------------------------------------%
:- func normalize(vector) = vector.
:- mode normalize(in) = (uo) is det.
:- mode normalize(di) = (uo) is det.
:- pred normalize(vector, vector).
:- mode normalize(in, uo) is det.
:- mode normalize(di, uo) is det.
%-----------------------------------------------------------------------------%
:- func dot(vector::in, vector::in) = (float::uo) is det.
:- pred dot(vector::in, vector::in, float::uo) is det.
%=============================================================================%
:- implementation.
%=============================================================================%
% Never learn how the sausage is made.
:- import_module float.
:- use_module math.
% We try to force inlining as much as possible. This allows many equations to
% actually compile down from Mercury all the way to ASM which is pretty close
% to what the C version would have been (sans some extra allocations ;_; )
:- pragma inline(vector/4).
:- pragma inline(('+')/2).
:- pragma inline(plus/3).
:- pragma inline(plus/2).
:- pragma inline(('-')/2).
:- pragma inline(minus/3).
:- pragma inline(minus/2).
:- pragma inline(('*')/2).
:- pragma inline(times/3).
:- pragma inline(times/2).
:- pragma inline(('/')/2).
:- pragma inline(div/3).
:- pragma inline(div/2).
:- pragma inline(('-')/1).
:- pragma inline(('+')/1).
:- pragma inline(scale/2).
:- pragma inline(scale/3).
:- pragma inline(length/2).
:- pragma inline(length_squared/1).
:- pragma inline(length_squared/2).
:- pragma inline(normalize/1).
:- pragma inline(normalize/2).
:- pragma inline(dot/2).
:- pragma inline(dot/3).
%=============================================================================%
% Pred and func versions of operators come first, as these are shared between
% both the C backend and the Mercury backend.
%=============================================================================%
plus(A, B, A+B).
plus(A, B) = A+B.
%-----------------------------------------------------------------------------%
minus(A, B, A-B).
minus(A, B) = A-B.
%-----------------------------------------------------------------------------%
times(A, B, A*B).
times(A, B) = (A*B).
%-----------------------------------------------------------------------------%
div(A, B, A/B).
div(A, B) = (A/B).
%-----------------------------------------------------------------------------%
+(A) = (A).
%-----------------------------------------------------------------------------%
negate(A) = (-A).
negate(A::in, B::uo) :- B = -A.
negate(A::di, B::uo) :- B = -A.
negate(A::uo, B::in) :- A = -B.
negate(A::uo, B::di) :- A = -B.
:- pragma promise_pure(negate/2).
%-----------------------------------------------------------------------------%
normalize(A, normalize(A)).
%-----------------------------------------------------------------------------%
dot(A, B, dot(A, B)).
%-----------------------------------------------------------------------------%
length(A, length(A)).
%-----------------------------------------------------------------------------%
scale(A, F, scale(A, F)).
%-----------------------------------------------------------------------------%
length_squared(A, length_squared(A)).
%=============================================================================%
% Mercury implementation. This is used for all backends except C.
%=============================================================================%
:- type vector ---> vec(vx::float, vy::float, vz::float, vw::float).
%-----------------------------------------------------------------------------%
vector(X::in, Y::in, Z::in, W::in) = (vec(X+0.0, Y+0.0, Z+0.0, W+0.0)::uo).
vector(X+0.0::uo, Y+0.0::uo, Z+0.0::uo, W+0.0::uo) = (vec(X, Y, Z, W)::in).
% Needed because of the disjunct bodies, even in pure Mercury.
:- pragma promise_pure(vector/4).
%-----------------------------------------------------------------------------%
x(V) = V ^ vx.
y(V) = V ^ vy.
z(V) = V ^ vz.
w(V) = V ^ vw.
%-----------------------------------------------------------------------------%
'x :='(V, X) = V ^ vx := X.
'y :='(V, Y) = V ^ vy := Y.
'z :='(V, Z) = V ^ vz := Z.
'w :='(V, W) = V ^ vw := W.
%-----------------------------------------------------------------------------%
vector(X1, Y1, Z1, W1) + vector(X2, Y2, Z2, W2) = vector(X1+X2, Y1+Y2, Z1+Z2, W1+W2).
%-----------------------------------------------------------------------------%
vector(X1, Y1, Z1, W1) - vector(X2, Y2, Z2, W2) = vector(X1-X2, Y1-Y2, Z1-Z2, W1-W2).
%-----------------------------------------------------------------------------%
vector(X1, Y1, Z1, W1) * vector(X2, Y2, Z2, W2) = vector(X1*X2, Y1*Y2, Z1*Z2, W1*W2).
%-----------------------------------------------------------------------------%
vector(X1, Y1, Z1, W1) / vector(X2, Y2, Z2, W2) = vector(X1/X2, Y1/Y2, Z1/Z2, W1/W2).
%-----------------------------------------------------------------------------%
-vector(X, Y, Z, W) = vector(-X, -Y, -Z, -W).
%-----------------------------------------------------------------------------%
scale(vector(X, Y, Z, W), T) = vector(X*T, Y*T, Z*T, W*T).
%-----------------------------------------------------------------------------%
length(A) = math.unchecked_sqrt(length_squared(A)).
length_squared(A) = dot(A, A).
%-----------------------------------------------------------------------------%
normalize(V) = scale(V, 1.0/length(V)).
%-----------------------------------------------------------------------------%
dot(vector(X1, Y1, Z1, W1), vector(X2, Y2, Z2, W2)) =
(X1*X2) + (Y1*Y2) + (Z1*Z2) + (W1*W2).
%=============================================================================%
% DirectXMath implementation, used in C backends.
%=============================================================================%
:- pragma foreign_decl("C", " #include ""dx_math_mercury.h"" ").
:- pragma foreign_type("C", vector, "struct MerDX_VECTOR4_wrapper*").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", x(V::in) = (F::out),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" F = DXMATH_VectorChannel(MerDX_Vector4Unwrap(V), 0); ").
:- pragma foreign_proc("C", y(V::in) = (F::out),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" F = DXMATH_VectorChannel(MerDX_Vector4Unwrap(V), 1); ").
:- pragma foreign_proc("C", z(V::in) = (F::out),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" F = DXMATH_VectorChannel(MerDX_Vector4Unwrap(V), 2); ").
:- pragma foreign_proc("C", w(V::in) = (F::out),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" F = DXMATH_VectorChannel(MerDX_Vector4Unwrap(V), 3); ").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", 'x :='(V::in, F::in) = (Out::out),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = MerDX_Vector4Unwrap(V);
Out = MerDX_CreateVector4(
F,
DXMATH_VectorChannel(vec, 1),
DXMATH_VectorChannel(vec, 2),
DXMATH_VectorChannel(vec, 3)); ").
:- pragma foreign_proc("C", 'y :='(V::in, F::in) = (Out::out),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = MerDX_Vector4Unwrap(V);
Out = MerDX_CreateVector4(
DXMATH_VectorChannel(vec, 0),
F,
DXMATH_VectorChannel(vec, 2),
DXMATH_VectorChannel(vec, 3)); ").
:- pragma foreign_proc("C", 'z :='(V::in, F::in) = (Out::out),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = MerDX_Vector4Unwrap(V);
Out = MerDX_CreateVector4(
DXMATH_VectorChannel(vec, 0),
DXMATH_VectorChannel(vec, 1),
F,
DXMATH_VectorChannel(vec, 3)); ").
:- pragma foreign_proc("C", 'w :='(V::in, F::in) = (Out::out),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = MerDX_Vector4Unwrap(V);
Out = MerDX_CreateVector4(
DXMATH_VectorChannel(vec, 0),
DXMATH_VectorChannel(vec, 1),
DXMATH_VectorChannel(vec, 2),
F); ").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", vector(X::in, Y::in, Z::in, W::in) = (V::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" V = MerDX_CreateVector4(X, Y, Z, W); ").
:- pragma foreign_proc("C", vector(X::uo, Y::uo, Z::uo, W::uo) = (V::in),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = MerDX_Vector4Unwrap(V);
X = DXMATH_VectorChannel(vec, 0);
Y = DXMATH_VectorChannel(vec, 1);
Z = DXMATH_VectorChannel(vec, 2);
W = DXMATH_VectorChannel(vec, 3); ").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", (V1::in) + (V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = DXMATH_Vector4Add(MerDX_Vector4Unwrap(V1), MerDX_Vector4Unwrap(V2));
Out = MerDX_CreateVector4Assign(vec);
").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", (V1::di) + (V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Add, V1, V1, V2); ").
:- pragma foreign_proc("C", (V1::in) + (V2::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Add, V2, V1, V2); ").
:- pragma foreign_proc("C", (V1::di) + (V2::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Add, V1, V1, V2); ").
:- pragma promise_pure(('+')/2).
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", (V1::in) - (V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = DXMATH_Vector4Subtract(MerDX_Vector4Unwrap(V1), MerDX_Vector4Unwrap(V2));
Out = MerDX_CreateVector4Assign(vec);
").
:- pragma foreign_proc("C", (V1::di) - (V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Subtract, V1, V1, V2); ").
:- pragma foreign_proc("C", (V1::in) - (V2::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Subtract, V2, V1, V2); ").
:- pragma foreign_proc("C", (V1::di) - (V2::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Subtract, V1, V1, V2); ").
:- pragma promise_pure(('-')/2).
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", (V1::in) * (V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = DXMATH_Vector4Multiply(MerDX_Vector4Unwrap(V1), MerDX_Vector4Unwrap(V2));
Out = MerDX_CreateVector4Assign(vec);
").
:- pragma foreign_proc("C", (V1::di) * (V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Multiply, V1, V1, V2); ").
:- pragma foreign_proc("C", (V1::in) * (V2::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Multiply, V2, V1, V2); ").
:- pragma foreign_proc("C", (V1::di) * (V2::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Multiply, V1, V1, V2); ").
:- pragma promise_pure(('*')/2).
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", (V1::in) / (V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = DXMATH_Vector4Divide(MerDX_Vector4Unwrap(V1), MerDX_Vector4Unwrap(V2));
Out = MerDX_CreateVector4Assign(vec);
").
:- pragma foreign_proc("C", (V1::di) / (V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Divide, V1, V1, V2); ").
:- pragma foreign_proc("C", (V1::in) / (V2::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Divide, V2, V1, V2); ").
:- pragma foreign_proc("C", (V1::di) / (V2::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = MerDX_DESTRUCTIVE_BINOP(DXMATH_Vector4Divide, V1, V1, V2); ").
:- pragma promise_pure(('/')/2).
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", -(V::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = DXMATH_Vector4Subtract(DXMATH_Vector4Zero(), MerDX_Vector4Unwrap(V));
Out = MerDX_CreateVector4Assign(vec); ").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", -(V::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
";
Out = V;
const DXMATH_VECTOR4 vec = DXMATH_Vector4Subtract(DXMATH_Vector4Zero(), MerDX_Vector4Unwrap(V));
MerDX_Vector4Unwrap(Out) = vec;
").
:- pragma promise_pure(('-')/1).
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", scale(V::in, MerF::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const float F = MerF;
const DXMATH_VECTOR4 vec = DXMATH_Vector4Scale(MerDX_Vector4Unwrap(V), F);
Out = MerDX_CreateVector4Assign(vec);
").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", scale(V::di, MerF::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const float F = MerF;
Out = V;
const DXMATH_VECTOR4 vec = DXMATH_Vector4Scale(MerDX_Vector4Unwrap(V), F);
MerDX_Vector4Unwrap(Out) = vec;
").
:- pragma promise_pure(scale/2).
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", normalize(V::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
const DXMATH_VECTOR4 vec = DXMATH_Vector4Normalize(MerDX_Vector4Unwrap(V));
Out = MerDX_CreateVector4Assign(vec);
").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", normalize(V::di) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
Out = V;
const DXMATH_VECTOR4 vec = DXMATH_Vector4Normalize(MerDX_Vector4Unwrap(V));
MerDX_Vector4Unwrap(Out) = vec;
").
:- pragma promise_pure(normalize/1).
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", dot(V1::in, V2::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
"
Out = DXMATH_Vector4Dot(MerDX_Vector4Unwrap(V1), MerDX_Vector4Unwrap(V2));
").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", length(V::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = DXMATH_Vector4Length(MerDX_Vector4Unwrap(V)); ").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C", length_squared(V::in) = (Out::uo),
[will_not_call_mercury, will_not_throw_exception, promise_pure, thread_safe,
will_not_modify_trail, does_not_affect_liveness, may_duplicate],
" Out = DXMATH_Vector4LengthSq(MerDX_Vector4Unwrap(V)); ").