DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
mathutil.cpp
1#pragma name_counter 245
2
3#include "mathutil.hpp"
4
5#include <eekernel.h>
6#include <libvu0.h>
7#include <sifdev.h>
8
9#include <cmath>
10#include <cstdlib>
11#include <cstring>
12
13#include "literals.hpp"
14
15/* Virtual table of std::exception: type information, this adjustment, destructor, what(). */
16extern "C" void *__vt__Q23std9exception[4] __attribute__((section(".vtables"))) = {
17 (void *) __RTTI__Q23std9exception, 0, (void *) __dt__Q23std9exceptionFv,
18 (void *) what__Q23std9exceptionCFv};
19/* Virtual table of std::bad_exception: type information, this adjustment, destructor, what(). */
20extern "C" void *__vt__Q23std13bad_exception[4] __attribute__((section(".vtables"))) = {
21 (void *) &__RTTI__Q23std13bad_exception, 0, (void *) __dt__Q23std13bad_exceptionFv,
22 (void *) what__Q23std13bad_exceptionCFv};
23void (*thandler__3std)() __attribute__((section(".data"))) __attribute__((aligned(8))) = dthandler__3stdFv;
24void (*uhandler__3std)() __attribute__((section(".data"))) __attribute__((aligned(8))) = duhandler__3stdFv;
25MWGlobalDestructor *__global_destructor_chain __attribute__((section(".bss")));
26
27namespace std {
31class exception {
32public:
33 virtual ~exception() throw();
34 virtual const char *what() const throw();
35};
36
40class bad_exception : public exception {
41public:
42 virtual ~bad_exception() throw();
43 virtual const char *what() const throw();
44};
45} // namespace std
46
51private:
52 void *array;
53 unsigned int element_size;
54 unsigned int count;
57public:
58 unsigned int constructed;
61 this->array = array;
62 this->element_size = element_size;
63 this->count = count;
64 this->destructor = destructor;
65 constructed = this->count;
66 }
67
69 unsigned char *element;
70
71 if (constructed < count && destructor != NULL) {
72 for (element = (unsigned char *) array + element_size * constructed; constructed > 0; constructed--) {
73 element -= element_size;
74 destructor(element, -1);
75 }
76 }
77 }
78};
79
80const char ExceptionWhat[] = "exception";
81
89#pragma schedule on
90#pragma exceptions on
91#pragma optimize_for_size off
92#pragma alignlabel on
93
94void __construct_array(void *array, MWRuntimeObjectFunction constructor, MWRuntimeObjectFunction destructor, unsigned int element_size, unsigned int count) {
95 MWPartialArrayDestructor partial(array, element_size, count, destructor);
96 unsigned char *element;
97
98 for (partial.constructed = 0, element = (unsigned char *) array; partial.constructed < count; partial.constructed++, element += element_size) {
99 constructor(element, 1);
100 }
101}
102
103#pragma alignlabel reset
104#pragma optimize_for_size reset
105#pragma exceptions reset
106#pragma schedule reset
114#pragma schedule on
115#pragma exceptions on
116#pragma optimize_for_size off
117#pragma alignlabel on
118
119void *__construct_new_array(void *allocation, MWRuntimeObjectFunction constructor, MWRuntimeObjectFunction destructor, unsigned int element_size, unsigned int count) {
120 char *array;
121
122 if ((array = (char *) allocation) != NULL) {
123 unsigned int *header = (unsigned int *) array;
124 header[0] = element_size;
125 header[1] = count;
126 array += 16;
127
128 if (constructor != NULL) {
129 MWPartialArrayDestructor partial(array, element_size, count, destructor);
130 char *element;
131
132 for (partial.constructed = 0, element = array; partial.constructed < count; partial.constructed++, element += element_size) {
133 constructor(element, 1);
134 }
135 }
136 }
137
138 return array;
139}
140
141#pragma alignlabel reset
142#pragma optimize_for_size reset
143#pragma exceptions reset
144#pragma schedule reset
152#pragma schedule on
153#pragma exceptions on
154
155void __dl(void *storage) throw() {
156 free(storage);
157}
158
159#pragma exceptions reset
160#pragma schedule reset
168#pragma schedule on
169#pragma exceptions on
170
171extern "C" void *__dt__Q23std9exceptionFv(void **self, short flag) throw() {
172 if (self != NULL) {
173 *self = __vt__Q23std9exception;
174
175 if (flag > 0) {
176 __dl(self);
177 }
178 }
179
180 return self;
181}
182
183#pragma exceptions reset
184#pragma schedule reset
192#pragma schedule on
193
194extern "C" const char *what__Q23std9exceptionCFv(const void *exception) {
195 (void) exception;
196 return ExceptionWhat;
197}
198
199#pragma schedule reset
207#pragma optimization_level 4
208#pragma optimize_for_size off
209#pragma padloop on
210#pragma alignlabel on
211#pragma schedule on
212
213extern "C" char __throw_catch_compare(const char *thrown_type, const char *caught_type, long *pointer_adjustment) {
214 const char *thrown;
215 const char *caught;
216
217 *pointer_adjustment = 0;
218
219 if ((caught = caught_type) == NULL) {
220 // catch (...)
221 return true;
222 }
223
224 thrown = thrown_type;
225
226 if (*caught == 'P') {
227 caught++;
228
229 if (*caught == 'C') {
230 caught++;
231 }
232
233 if (*caught == 'V') {
234 caught++;
235 }
236
237 if (*caught == 'v') {
238 // catch (cv void *) takes any thrown pointer
239 if (*thrown == 'P' || *thrown == '*') {
240 return true;
241 }
242 }
243
244 caught = caught_type;
245 }
246
247 switch (*thrown) {
248 case '*':
249 case '!':
250 // A thrown class lists each base class name followed by its offset.
251 if (*thrown++ != *caught++) {
252 return false;
253 }
254
255 for (;;) {
256 if (*thrown == *caught++) {
257 if (*thrown++ == '!') {
258 long offset;
259
260 for (offset = 0; *thrown != '!';) {
261 offset = offset * 10 + *thrown++ - '0';
262 }
263
264 *pointer_adjustment = offset;
265 return true;
266 }
267 } else {
268 while (*thrown++ != '!') {
269 }
270
271 while (*thrown++ != '!') {
272 }
273
274 if (*thrown == 0) {
275 return false;
276 }
277
278 caught = caught_type + 1;
279 }
280 }
281
282 return false;
283 }
284
285 while ((*thrown == 'P' || *thrown == 'R') && *thrown == *caught) {
286 thrown++;
287 caught++;
288
289 if (*caught == 'C') {
290 if (*thrown == 'C') {
291 thrown++;
292 }
293
294 caught++;
295 }
296
297 if (*thrown == 'C') {
298 return false;
299 }
300
301 if (*caught == 'V') {
302 if (*thrown == 'V') {
303 thrown++;
304 }
305
306 caught++;
307 }
308
309 if (*thrown == 'V') {
310 return false;
311 }
312 }
313
314 for (; *thrown == *caught; thrown++, caught++) {
315 if (*thrown == 0) {
316 return true;
317 }
318 }
319
320 return false;
321}
322
323#pragma schedule reset
324#pragma alignlabel reset
325#pragma padloop reset
326#pragma optimize_for_size reset
327#pragma optimization_level reset
335#pragma schedule on
336
337extern "C" void unexpected__3stdFv() {
338 uhandler__3std();
339}
340
341#pragma schedule reset
349#pragma schedule on
350
351extern "C" void terminate__3stdFv() {
352 thandler__3std();
353}
354
355#pragma schedule reset
363#pragma schedule on
364
365extern "C" void duhandler__3stdFv() {
366 thandler__3std();
367}
368
369#pragma schedule reset
377#pragma schedule on
378
379extern "C" void dthandler__3stdFv() {
380 abort();
381}
382
383#pragma schedule reset
391#pragma schedule on
392
393void *__register_global_object(void *object, MWRuntimeObjectFunction destructor, MWGlobalDestructor *record) {
394 record->next = __global_destructor_chain;
395 record->destructor = destructor;
396 record->object = object;
397 __global_destructor_chain = record;
398 return object;
399}
400
401#pragma schedule reset
409#pragma schedule on
410#pragma optimization_level 4
411#pragma padloop on
412
413extern "C" void __initialize_cpp_rts(void *first, void *last, void *overlay_start, void *overlay_end) {
414 void (**initializer)() = (void (**)()) first;
415
416 if ((void (**)()) first < (void (**)()) last) {
417 do {
418 (*initializer)();
419 initializer++;
420 } while (initializer < (void (**)()) last);
421 }
422}
423
424#pragma padloop reset
425#pragma optimization_level reset
426#pragma schedule reset
434#pragma schedule on
435
436char *__DecodeUnsignedNumber(char *encoded, unsigned int *value) {
437 unsigned int first = (unsigned char) encoded[0];
438
439 if ((first & 1) == 0) {
440 *value = first >> 1;
441 return encoded + 1;
442 }
443
444 unsigned int second = (unsigned char) encoded[1];
445
446 if ((first & 2) == 0) {
447 *value = ((first >> 2) << 8) | second;
448 return encoded + 2;
449 }
450
451 unsigned int third = (unsigned char) encoded[2];
452
453 if ((first & 4) == 0) {
454 *value = ((first >> 3) << 16) | (second << 8) | third;
455 return encoded + 3;
456 }
457
458 *value = ((first >> 3) << 24) | (second << 16) | (third << 8) | (unsigned char) encoded[3];
459 return encoded + 4;
460}
461
462#pragma schedule reset
470#pragma schedule on
471
472char *__DecodeSignedNumber(char *encoded, int *value) {
473 signed char first = encoded[0];
474
475 if ((first & 1) == 0) {
476 *value = first >> 1;
477 return encoded + 1;
478 }
479
480 unsigned int second = (unsigned char) encoded[1];
481
482 if ((first & 2) == 0) {
483 *value = ((first >> 2) << 8) | second;
484 return encoded + 2;
485 }
486
487 unsigned int third = (unsigned char) encoded[2];
488
489 if ((first & 4) == 0) {
490 *value = ((first >> 3) << 16) | (second << 8) | third;
491 return encoded + 3;
492 }
493
494 *value = ((first >> 3) << 24) | (second << 16) | (third << 8) | (unsigned char) encoded[3];
495 return encoded + 4;
496}
497
498#pragma schedule reset
506#pragma schedule on
507
508extern "C" void __end__catch(MWCatchRecord *record) {
509 if (record->object != NULL && record->destructor != NULL) {
510 record->destructor(record->object, -1);
511 }
512}
513
514#pragma schedule reset
515#pragma exceptions on
516
520static inline char __find_exception_spec(const char *type_info, MWExceptionSpecification *spec) {
521 long adjustment;
522 unsigned char *types = spec->types;
523
524 for (unsigned int i = 0; i < spec->count; i++) {
525 if (__throw_catch_compare(type_info, (const char *) (types[0] | (types[1] << 8) | (types[2] << 16) | (types[3] << 24)), &adjustment)) {
526 return true;
527 }
528
529 types += 4;
530 }
531
532 return false;
533}
534
542#pragma schedule on
543
544extern "C" void __unexpected(void *exception_record) {
545 char *encoded = (char *) ((MWCatchRecord *) exception_record)->stack_top;
547
548#pragma exception_magic
549 try {
550 unexpected__3stdFv();
551 } catch (...) {
552 spec.types = (unsigned char *) __DecodeSignedNumber(__DecodeUnsignedNumber(__DecodeUnsignedNumber(encoded + 1, &spec.count), &spec.unused_unsigned), &spec.unused_signed);
553
554 // A new exception the specification allows propagates; otherwise it becomes a
555 // std::bad_exception if that is allowed.
556 if (__find_exception_spec(((MWCatchRecord *) &__exception_magic)->type_info, &spec)) {
557 throw;
558 }
559
560 if (__find_exception_spec("!std::bad_exception!!", &spec)) {
561 throw std::bad_exception();
562 }
563 }
564 terminate__3stdFv();
565}
566
567#pragma schedule reset
568// MWCC generates a function at the next initialised data definition. This one emits
569// nothing, and makes __unexpected and its strings come out before the exception
570// setting changes and before the type information run below.
571static const int __unexpected_generated = 0;
572#pragma exceptions reset
573// The runtime's type information for std::exception and std::bad_exception.
574const char BadExceptionTypeName[] = "std::bad_exception";
575const char ExceptionTypeName[] = "std::exception";
576const MWTypeInfo ExceptionTypeInfo = {ExceptionTypeName, 0};
577const MWSingleBaseClass BadExceptionBaseClasses = {
578 {&ExceptionTypeInfo, 0},
579 0
580};
581const MWTypeInfo __RTTI__Q23std13bad_exception = {BadExceptionTypeName, &BadExceptionBaseClasses.base};
582const char BadExceptionWhat[] = "bad_exception";
590#pragma schedule on
591#pragma exceptions on
592
593extern "C" void *__dt__Q23std13bad_exceptionFv(void **self, short flag) throw() {
594 if (self != NULL) {
595 *self = __vt__Q23std13bad_exception;
596
597 if (self != NULL) {
598 *self = __vt__Q23std9exception;
599 }
600
601 if (flag > 0) {
602 __dl(self);
603 }
604 }
605
606 return self;
607}
608
609#pragma exceptions reset
610#pragma schedule reset
618#pragma schedule on
619
620extern "C" const char *what__Q23std13bad_exceptionCFv(const void *exception) {
621 (void) exception;
622 return BadExceptionWhat;
623}
624
625#pragma schedule reset
626
634void mwInit(int argc, const char **argv, const char **envp) {
635 __initialize_cpp_rts(__static_init, __static_init_end, _overlay_group_addresses, _overlay_group_addresses);
636}
637
645extern "C" void mwOverlayInit(void *overlay, int size) {
646 char *image = (char *) overlay;
647 OverlayHeader *header = (OverlayHeader *) overlay;
648 int bss_size = header->bss_size;
649
650 FlushCache(2);
651
652 if (bss_size != 0) {
653 image += size;
654 memset(image, 0, bss_size);
655 }
656
657 __initialize_cpp_rts(header->static_init, header->static_init_end, 0, 0);
658}
659
667void MWNotifyOverlayLoaded(void *address) {}
668
676extern "C" int mwBload(char *path, void *buffer) {
677 int fd;
678 int size = 0;
679
680 fd = sceOpen(path, 1);
681
682 if (fd >= 0) {
683 size = sceLseek(fd, 0, 2);
684 sceLseek(fd, 0, 0);
685 }
686
687 if (size > 0) {
688 size = sceRead(fd, buffer, size);
689 }
690
691 if (fd >= 0) {
692 sceClose(fd);
693 }
694
695 return size;
696}
697
705extern "C" int mwLoadOverlay(char *path, void *address) {
706 int size;
707 int result = 0;
708 size = mwBload(path, address);
709
710 if (size > 0) {
711 MWNotifyOverlayLoaded(address);
712 mwOverlayInit(address, size);
713 result = 1;
714 }
715
716 return result;
717}
718
719static float sin_table_num = 1024.0f;
720static float sin_table_unit_1 = 162.97466f;
721static float SinTable[1024];
722#ifndef PORT
723void VectorMax(float *max, float *a, float *b) {
724 register float *out = max;
725 register float *p0 = a;
726 register float *p1 = b;
727
728 asm {
729 lqc2 vf15, 0(p0)
730 lqc2 vf16, 0(p1)
731 vmax vf18, vf15, vf16
732 sqc2 vf18, 0(out)
733 }
734}
735
736void VectorMax(float *max, float *a, float *b, float *c) {
737 register float *out = max;
738 register float *p0 = a;
739 register float *p1 = b;
740 register float *p2 = c;
741
742 asm {
743 lqc2 vf15, 0(p0)
744 lqc2 vf16, 0(p1)
745 lqc2 vf17, 0(p2)
746 vmax vf18, vf15, vf16
747 vmax vf19, vf18, vf17
748 sqc2 vf19, 0(out)
749 }
750}
751
752void VectorMax(float *max, float *a, float *b, float *c, float *d) {
753 register float *out = max;
754 register float *p0 = a;
755 register float *p1 = b;
756 register float *p2 = c;
757 register float *p3 = d;
758
759 asm {
760 lqc2 vf15, 0(p0)
761 lqc2 vf16, 0(p1)
762 lqc2 vf17, 0(p2)
763 lqc2 vf18, 0(p3)
764 vmax vf20, vf15, vf16
765 vmax vf20, vf20, vf17
766 vmax vf20, vf20, vf18
767 sqc2 vf20, 0(out)
768 }
769}
770
771void VectorMin(float *min, float *a, float *b) {
772 register float *out = min;
773 register float *p0 = a;
774 register float *p1 = b;
775
776 asm {
777 lqc2 vf15, 0(p0)
778 lqc2 vf16, 0(p1)
779 vmini vf18, vf15, vf16
780 sqc2 vf18, 0(out)
781 }
782}
783
784void VectorMin(float *min, float *a, float *b, float *c, float *d) {
785 register float *out = min;
786 register float *p0 = a;
787 register float *p1 = b;
788 register float *p2 = c;
789 register float *p3 = d;
790
791 asm {
792 lqc2 vf15, 0(p0)
793 lqc2 vf16, 0(p1)
794 lqc2 vf17, 0(p2)
795 lqc2 vf18, 0(p3)
796 vmini vf20, vf15, vf16
797 vmini vf20, vf20, vf17
798 vmini vf20, vf20, vf18
799 sqc2 vf20, 0(out)
800 }
801}
802
803void VectorMaxMin(float *max, float *min, float *a, float *b) {
804 register float *out_max = max;
805 register float *out_min = min;
806 register float *p0 = a;
807 register float *p1 = b;
808
809 asm {
810 lqc2 vf15, 0(p0)
811 lqc2 vf16, 0(p1)
812 vmax vf18, vf15, vf16
813 vmini vf20, vf15, vf16
814 sqc2 vf18, 0(out_max)
815 sqc2 vf20, 0(out_min)
816 }
817}
818
819void VectorMaxMin(float *max, float *min, float *a, float *b, float *c) {
820 register float *out_max = max;
821 register float *out_min = min;
822 register float *p0 = a;
823 register float *p1 = b;
824 register float *p2 = c;
825
826 asm {
827 lqc2 vf15, 0(p0)
828 lqc2 vf16, 0(p1)
829 lqc2 vf17, 0(p2)
830 vmax vf18, vf15, vf16
831 vmini vf20, vf15, vf16
832 vmax vf19, vf18, vf17
833 vmini vf21, vf20, vf17
834 sqc2 vf19, 0(out_max)
835 sqc2 vf21, 0(out_min)
836 }
837}
838
839void VectorMaxMin(float *max, float *min, float *a, float *b, float *c, float *d) {
840 register float *out_max = max;
841 register float *out_min = min;
842 register float *p0 = a;
843 register float *p1 = b;
844 register float *p2 = c;
845 register float *p3 = d;
846
847 asm {
848 lqc2 vf15, 0(p0)
849 lqc2 vf16, 0(p1)
850 lqc2 vf17, 0(p2)
851 lqc2 vf18, 0(p3)
852 vmax vf20, vf15, vf16
853 vmini vf21, vf15, vf16
854 vmax vf20, vf20, vf17
855 vmini vf21, vf21, vf17
856 vmax vf20, vf20, vf18
857 vmini vf21, vf21, vf18
858 sqc2 vf20, 0(out_max)
859 sqc2 vf21, 0(out_min)
860 }
861}
862
863void PlaneNormal(float *normal, float *v0, float *v1, float *v2) {
864 register float *out = normal;
865 register float *p0 = v0;
866 register float *p1 = v1;
867 register float *p2 = v2;
868
869 asm {
870 lqc2 vf15, 0(p0)
871 lqc2 vf16, 0(p1)
872 lqc2 vf17, 0(p2)
873 vsub vf10, vf16, vf15
874 vsub vf11, vf17, vf15
875 vopmula ACC, vf10, vf11
876 vopmsub vf12, vf11, vf10
877 sqc2 vf12, 0(out)
878 }
879}
880#endif
881float DistPlanePoint(float *normal, float *on_plane, float *point) {
882 sceVu0FVECTOR offset;
883
884 sceVu0SubVector(offset, point, on_plane);
885 return sceVu0InnerProduct(normal, offset);
886}
887
888float ReflectionPlane(float *normal, float *on_plane, float *point, float *reflection) {
889 sceVu0FVECTOR step;
890 float distance;
891
892 distance = 2.0f * DistPlanePoint(normal, on_plane, point);
893 sceVu0ScaleVector(step, normal, -distance);
894 sceVu0SubVector(reflection, on_plane, point);
895 sceVu0SubVector(reflection, reflection, step);
896 return distance;
897}
898
899int IntersectionPoint_line_poly3(float *from, float *to, float *v0, float *v1, float *v2, float *normal, float *hit) {
900 sceVu0FVECTOR line;
901 sceVu0FVECTOR e0;
902 sceVu0FVECTOR e1;
903 sceVu0FVECTOR e2;
904 float above;
905 float along;
906
907 sceVu0SubVector(line, to, from);
908 sceVu0SubVector(e0, v0, from);
909 sceVu0SubVector(e1, v1, from);
910 sceVu0SubVector(e2, v2, from);
911 above = -sceVu0InnerProduct(normal, e0);
912 along = sceVu0InnerProduct(normal, line);
913
914 if (along == 0.0f) {
915 return 0;
916 }
917
918 sceVu0ScaleVector(hit, line, -above / along);
919 sceVu0AddVector(hit, hit, from);
920 return Check_Point_Poly3_XYZ(hit, v0, v1, v2, normal);
921}
922
923int Check_Point_Poly3_XYZ(float *point, float *v0, float *v1, float *v2, float *normal) {
924 sceVu0FVECTOR p0;
925 sceVu0FVECTOR p1;
926 sceVu0FVECTOR p2;
927 sceVu0FVECTOR e0;
928 sceVu0FVECTOR e1;
929 sceVu0FVECTOR e2;
930 sceVu0FVECTOR c0;
931 sceVu0FVECTOR c1;
932 sceVu0FVECTOR c2;
933 float d0;
934 float d1;
935 float d2;
936
937 sceVu0SubVector(p0, point, v0);
938 sceVu0SubVector(p1, point, v1);
939 sceVu0SubVector(p2, point, v2);
940 sceVu0SubVector(e0, v1, v0);
941 sceVu0SubVector(e1, v2, v1);
942 sceVu0SubVector(e2, v0, v2);
943 sceVu0OuterProduct(c0, e0, p0);
944 sceVu0OuterProduct(c1, e1, p1);
945 sceVu0OuterProduct(c2, e2, p2);
946 d0 = sceVu0InnerProduct(c0, normal);
947 d1 = sceVu0InnerProduct(c1, normal);
948 d2 = sceVu0InnerProduct(c2, normal);
949
950 if (d0 >= 0.0f && d1 >= 0.0f && d2 >= 0.0f) {
951 return 1;
952 }
953
954 if (d0 <= 0.0f && d1 <= 0.0f && d2 <= 0.0f) {
955 return 1;
956 }
957
958 return 0;
959}
960#ifndef PORT
961/* The square root leaves the Vector Unit in Q, which only a general register can be moved out of,
962 so the block hands the bits over in an integer and reinterprets them where a float return value
963 has to be. Both variables are the compiler's to place; the initializer is what stops it warning
964 about the one nothing in C ever writes. */
965float DistVector(float *vector) {
966 register float *pointer = vector;
967 register int root;
968 register float length = 0.0f;
969
970 asm {
971 lqc2 vf4, 0(pointer)
972 vmul.xyz vf4, vf4, vf4
973 vmr32.xy vf5, vf4
974 vmr32.x vf6, vf5
975 vadd.x vf7, vf4, vf5
976 vadd.x vf5, vf6, vf7
977 vsqrt Q, vf5x
978 vwaitq
979 cfc2 root, $vi22
980 mtc1 root, length
981 }
982
983 return length;
984}
985
986float DistVector(float *a, float *b) {
987 register float *p0 = a;
988 register float *p1 = b;
989 register int root;
990 register float length = 0.0f;
991
992 asm {
993 lqc2 vf2, 0(p0)
994 lqc2 vf3, 0(p1)
995 vsub.xyz vf4, vf3, vf2
996 vmul.xyz vf4, vf4, vf4
997 vmr32.xy vf5, vf4
998 vmr32.x vf6, vf5
999 vadd.x vf7, vf4, vf5
1000 vadd.x vf5, vf6, vf7
1001 vsqrt Q, vf5x
1002 vwaitq
1003 cfc2 root, $vi22
1004 mtc1 root, length
1005 }
1006
1007 return length;
1008}
1009
1010void MulMatrix(sceVu0FMATRIX product, sceVu0FMATRIX left_matrix, sceVu0FMATRIX right_matrix) {
1011 register float *out = (float *) product;
1012 register float *left = (float *) left_matrix;
1013 register float *right = (float *) right_matrix;
1014
1015 asm {
1016 lqc2 vf1, 0(left)
1017 lqc2 vf2, 16(left)
1018 lqc2 vf3, 32(left)
1019 lqc2 vf4, 48(left)
1020 lqc2 vf5, 0(right)
1021 lqc2 vf6, 16(right)
1022 lqc2 vf7, 32(right)
1023 lqc2 vf8, 48(right)
1024 vmulax ACC, vf1, vf5
1025 vmadday ACC, vf2, vf5
1026 vmaddaz ACC, vf3, vf5
1027 vmaddw vf20, vf4, vf5
1028 vmulax ACC, vf1, vf6
1029 vmadday ACC, vf2, vf6
1030 vmaddaz ACC, vf3, vf6
1031 vmaddw vf21, vf4, vf6
1032 vmulax ACC, vf1, vf7
1033 vmadday ACC, vf2, vf7
1034 vmaddaz ACC, vf3, vf7
1035 vmaddw vf22, vf4, vf7
1036 vmulax ACC, vf1, vf8
1037 vmadday ACC, vf2, vf8
1038 vmaddaz ACC, vf3, vf8
1039 vmaddw vf23, vf4, vf8
1040 sqc2 vf20, 0(out)
1041 sqc2 vf21, 16(out)
1042 sqc2 vf22, 32(out)
1043 sqc2 vf23, 48(out)
1044 }
1045}
1046
1047void RotMatrixY(sceVu0FMATRIX matrix, float angle_y) {
1048 register float *out = (float *) matrix;
1049 float angle = angle_y;
1050
1051 /* vf0 is the constant (0, 0, 0, 1), so three rotations of it are the other three rows and the
1052 identity costs four stores instead of sixteen. */
1053 asm {
1054 vmr32 vf1, vf0
1055 vmr32 vf2, vf1
1056 vmr32 vf3, vf2
1057 sqc2 vf0, 48(out)
1058 sqc2 vf1, 32(out)
1059 sqc2 vf2, 16(out)
1060 sqc2 vf3, 0(out)
1061 }
1062
1063 matrix[2][2] = Cosf(angle);
1064 matrix[0][0] = matrix[2][2];
1065 matrix[2][0] = Sinf(angle);
1066 matrix[0][2] = -matrix[2][0];
1067}
1068#endif
1069void LookAtMatrixZ(sceVu0FMATRIX matrix, float *direction) {
1070 sceVu0FMATRIX pitch;
1071 sceVu0FMATRIX yaw;
1072 sceVu0FVECTOR unit;
1073 sceVu0FVECTOR flat;
1074 float ground;
1075 float cosine;
1076 float sine;
1077
1078 sceVu0UnitMatrix(yaw);
1079 sceVu0CopyMatrix(pitch, yaw);
1080 sceVu0Normalize(unit, direction);
1081 sceVu0CopyVector(flat, unit);
1082 flat[1] = 0.0f;
1083 ground = DistVector(flat);
1084 /* The length is measured a second time and the answer thrown away; it is a call the function
1085 makes, like the truncation AngleLimit repeats below. */
1086 DistVector(flat);
1087
1088 if (ground == 0.0f) {
1089 cosine = 0.0f;
1090 sine = 1.0f;
1091 } else {
1092 cosine = unit[0] / ground;
1093 sine = unit[2] / ground;
1094 }
1095
1096 pitch[1][1] = ground;
1097 pitch[1][2] = -unit[1];
1098 pitch[2][1] = unit[1];
1099 pitch[2][2] = ground;
1100 yaw[0][0] = sine;
1101 yaw[0][2] = -cosine;
1102 yaw[2][0] = cosine;
1103 yaw[2][2] = sine;
1104 MulMatrix(matrix, yaw, pitch);
1105}
1106#ifndef PORT
1107void ApplyMatrixN(sceVu0FVECTOR *out, sceVu0FMATRIX matrix, sceVu0FVECTOR *in, int count) {
1108 register float *dst = (float *) out;
1109 register float *m = (float *) matrix;
1110 register float *src = (float *) in;
1111 register int remaining = count;
1112
1113 // clang-format off
1114 asm {
1115 addi remaining, remaining, -1
1116 lqc2 vf10, 0(m)
1117 lqc2 vf11, 16(m)
1118 lqc2 vf12, 32(m)
1119 lqc2 vf13, 48(m)
1120 lqc2 vf16, 0(src)
1121 vnop
1122 vnop
1123 vnop
1124 }
1125row:
1126 asm {
1127 vmulax ACC, vf10, vf16
1128 vmadday ACC, vf11, vf16
1129 vmaddaz ACC, vf12, vf16
1130 vmaddw vf17, vf13, vf16
1131 addi remaining, remaining, -1
1132 addi dst, dst, 16
1133 addi src, src, 16
1134 sqc2 vf17, -16(dst)
1135 lqc2 vf16, 0(src)
1136 vnop
1137 bgez remaining, row
1138 nop
1139 }
1140 // clang-format on
1141}
1142#endif
1143void VectorInterpolate(float *out, float *from, float *to, float step, int mode) {
1144 sceVu0FVECTOR gap;
1145 int i;
1146 float magnitude;
1147 float delta;
1148
1149 sceVu0SubVector(gap, to, from);
1150
1151 switch (mode) {
1152 case INTERPOLATE_STEP:
1153 for (i = 0; i < 3; i++) {
1154 /* The signed difference and its magnitude are carried as two variables, and each
1155 arm writes back the one the other arm would have changed - so the second assignment
1156 stores nothing on a value that already agrees. */
1157 delta = gap[i];
1158 magnitude = delta;
1159
1160 if (delta < 0.0f) {
1161 magnitude = -delta;
1162 } else {
1163 delta = magnitude;
1164 }
1165
1166 if (magnitude < step) {
1167 out[i] = to[i];
1168 } else if (delta < 0.0f) {
1169 out[i] = from[i] - step;
1170 } else {
1171 out[i] = from[i] + step;
1172 }
1173 }
1174
1175 break;
1177 for (i = 0; i < 3; i++) {
1178 out[i] = from[i] + gap[i] / step;
1179 }
1180
1181 break;
1182 }
1183}
1184
1185/* Every angle in the game is radians held in (-PI, PI], and each of these puts its result back in
1186 that range by adding or subtracting one whole turn — once is enough because both operands are
1187 already in range, so a difference cannot leave it by more than a turn. */
1188
1189float AngleInterpolate(float from, float to, float step, int mode) {
1190 float delta;
1191 float offset;
1192 float result;
1193
1194 delta = to - from;
1195
1196 if (delta > PI) {
1197 delta -= PI * 2.0f;
1198 }
1199
1200 if (delta <= -PI) {
1201 delta += PI * 2.0f;
1202 }
1203
1204 offset = 0.0f;
1205
1206 if (mode == INTERPOLATE_STEP && (delta < 0.0f ? -delta : delta) < step) {
1207 return to;
1208 }
1209
1210 switch (mode) {
1211 case INTERPOLATE_STEP:
1212 if (delta < 0.0f) {
1213 if (step < delta) {
1214 return to;
1215 }
1216
1217 offset -= step;
1218 }
1219
1220 if (delta >= 0.0f) {
1221 if (step > delta) {
1222 return to;
1223 }
1224
1225 offset += step;
1226 }
1227
1228 break;
1230 offset = delta / step;
1231 break;
1232 }
1233
1234 result = from + offset;
1235
1236 if (result > PI) {
1237 result -= PI * 2.0f;
1238 }
1239
1240 if (result <= -PI) {
1241 result += PI * 2.0f;
1242 }
1243
1244 return result;
1245}
1246
1247int AngleCmp(float a, float b, float tolerance) {
1248 float delta;
1249
1250 delta = a - b;
1251
1252 if (delta == 0.0f) {
1253 return 0;
1254 }
1255
1256 if (delta > PI) {
1257 delta -= PI * 2.0f;
1258 }
1259
1260 if (delta < -PI) {
1261 delta += PI * 2.0f;
1262 }
1263
1264 if (delta > tolerance) {
1265 return 1;
1266 }
1267
1268 if (delta < -tolerance) {
1269 return -1;
1270 }
1271
1272 return 0;
1273}
1274
1275float AngleLimit(float angle) {
1276 float turns;
1277 int whole;
1278
1279 if (angle < PI && angle > -PI) {
1280 return angle;
1281 }
1282
1283 turns = angle / (PI * 2.0f);
1284 /* The first truncation is a call whose result nothing reads, and it is kept because it is one
1285 the function makes: each conversion in the source is its own call. */
1286 whole = (int) turns;
1287 angle -= PI * 2.0f * (int) turns;
1288
1289 if (angle > PI) {
1290 angle -= PI * 2.0f;
1291 }
1292
1293 if (angle < -PI) {
1294 angle += PI * 2.0f;
1295 }
1296
1297 return angle;
1298}
1299
1300float rnd() {
1301 return (float) rand() / 2147483648.0f;
1302}
1303
1304float nrnd() {
1305 return rnd() + rnd() + rnd() + rnd() + rnd() + rnd() + rnd() + rnd() + rnd() + rnd() + rnd() + rnd() - 6.0f;
1306}
1307
1308void CreateSinTable() {
1309 int i;
1310
1311 sin_table_num = 1024.0f;
1312 sin_table_unit_1 = 162.97466f;
1313
1314 for (i = 0; i < 1024; i++) {
1315 SinTable[i] = sinf(PI * (2.0f * (float) i) / sin_table_num);
1316 }
1317}
1318
1319float Sinf(float angle) {
1320 if (angle >= 0.0f) {
1321 return SinTable[(int) (angle * sin_table_unit_1) % 1024];
1322 }
1323
1324 return -SinTable[(int) (-angle * sin_table_unit_1) % 1024];
1325}
1326
1327float Cosf(float angle) {
1328 return Sinf(PI / 2.0f + angle);
1329}
unsigned int element_size
Definition mathutil.cpp:53
unsigned int constructed
Definition mathutil.cpp:58
MWRuntimeObjectFunction destructor
Definition mathutil.cpp:55
@ INTERPOLATE_FRACTION
Definition mathutil.hpp:16
@ INTERPOLATE_STEP
Definition mathutil.hpp:15
void(* __static_init[])()
const unsigned char __RTTI__Q23std9exception[]
void(* __static_init_end[])()
void(* MWRuntimeObjectFunction)(void *object, int mode)
Definition mathutil.hpp:29
MWRuntimeObjectFunction destructor
Definition mathutil.hpp:169
unsigned int unused_unsigned
Definition mathutil.hpp:180
unsigned char * types
Definition mathutil.hpp:182
MWRuntimeObjectFunction destructor
Definition mathutil.hpp:101
MWGlobalDestructor * next
Definition mathutil.hpp:100
void * static_init_end
Definition mathutil.hpp:212
void * static_init
Definition mathutil.hpp:211