Systemnahe Programmierung in C: Matrizen als eindimensionale Felder |
|
1#ifndef Matrix1__
2#define Matrix1__ 1
3
4typedef double Element;
5
6typedef Element *Matrix;
7
8extern Matrix newMatrix( int h, int w );
9extern Matrix zeroMatrix( int h, int w );
10extern Matrix unitMatrix( int h, int w );
11
12extern void freeMatrix( Matrix m );
13
14extern Matrix addMatrix( Matrix m1, Matrix m2, int h, int w );
15
16extern Matrix transposeMatrix( Matrix m, int h, int w );
17
18#endif
|
1#include "Matrix1.h"
2
3#include <stdlib.h>
4
5/*--------------------*/
6
7Matrix newMatrix( int h, int w )
8{
9 Matrix res = malloc( h * w * sizeof( Element ) );
10
11 if ( res ) {
12 return res;
13 }
14
15 /* heap overflow */
16 exit( 1 );
17}
18
19/*--------------------*/
20
21void freeMatrix( Matrix m )
22{
23 free( m );
24}
25
26/*--------------------*/
27
28Matrix zeroMatrix( int h, int w )
29{
30 Matrix res = newMatrix( h, w );
31
32 int len = w * h;
33 Matrix p = res;
34
35 while ( len-- ) {
36 *p++ = 0.0;
37 }
38
39 return res;
40}
41
42/*--------------------*/
43
44Matrix unitMatrix( int h, int w )
45{
46 Matrix res = zeroMatrix( h, w );
47
48 int i;
49 for ( i = 0; i < w && i < h; ++i ) {
50 res[i * w + i] = 1.0;
51 }
52
53 return res;
54}
55
56/*--------------------*/
57
58Matrix addMatrix( Matrix m1, Matrix m2, int h, int w )
59{
60
61 Matrix res = newMatrix( h, w );
62 int i, j;
63
64 for ( i = 0; i < h; ++i ) {
65 for ( j = 0; j < w; ++j ) {
66 res[i * w + j] = m1[i * w + j] + m2[i * w + j];
67 }
68 }
69
70 return res;
71}
72
73/*--------------------*/
74
75Matrix transposeMatrix( Matrix m, int h, int w )
76{
77
78 Matrix res = newMatrix( w, h );
79 int i, j;
80
81 for ( i = 0; i < h; ++i ) {
82 for ( j = 0; j < w; ++j ) {
83 res[j * h + i] = m[i * w + j];
84 }
85 }
86
87 return res;
88}
|
1 newMatrix:
2 subq $8, %rsp
3 imull %esi, %edi
4 movslq %edi, %rdi
5 salq $3, %rdi
6 call malloc
7 testq %rax, %rax
8 je .L2
9 addq $8, %rsp
10 ret
11 .L2:
12 movl $1, %edi
13 call exit
14 freeMatrix:
15 subq $8, %rsp
16 call free
17 addq $8, %rsp
18 ret
19 zeroMatrix:
20 pushq %rbp
21 pushq %rbx
22 subq $8, %rsp
23 movl %edi, %ebp
24 movl %esi, %ebx
25 call newMatrix
26 imull %ebp, %ebx
27 testl %ebx, %ebx
28 je .L5
29 subl $1, %ebx
30 leaq 8(%rax,%rbx,8), %rsi
31 movq %rax, %rdx
32 movl $0, %ecx
33 .L6:
34 movq %rcx, (%rdx)
35 addq $8, %rdx
36 cmpq %rsi, %rdx
37 jne .L6
38 .L5:
39 addq $8, %rsp
40 popq %rbx
41 popq %rbp
42 ret
43 unitMatrix:
44 pushq %rbp
45 pushq %rbx
46 subq $8, %rsp
47 movl %edi, %ebp
48 movl %esi, %ebx
49 call zeroMatrix
50 testl %ebx, %ebx
51 jle .L9
52 testl %ebp, %ebp
53 jle .L9
54 leal 1(%rbx), %r8d
55 movl $0, %ecx
56 movl $0, %edx
57 movabsq $4607182418800017408, %rdi
58 .L10:
59 movslq %ecx, %rsi
60 movq %rdi, (%rax,%rsi,8)
61 addl $1, %edx
62 addl %r8d, %ecx
63 cmpl %edx, %ebx
64 jle .L9
65 cmpl %edx, %ebp
66 jg .L10
67 .L9:
68 addq $8, %rsp
69 popq %rbx
70 popq %rbp
71 ret
72 addMatrix:
73 pushq %r13
74 pushq %r12
75 pushq %rbp
76 pushq %rbx
77 subq $8, %rsp
78 movq %rdi, %rbx
79 movq %rsi, %rbp
80 movl %edx, %r13d
81 movl %ecx, %r12d
82 movl %ecx, %esi
83 movl %edx, %edi
84 call newMatrix
85 testl %r13d, %r13d
86 jle .L14
87 movl %r12d, %r8d
88 movl $0, %esi
89 movl $0, %edi
90 jmp .L15
91 .L19:
92 leal (%rdx,%rsi), %ecx
93 movslq %ecx, %rcx
94 movsd (%rbx,%rcx,8), %xmm0
95 addsd 0(%rbp,%rcx,8), %xmm0
96 movsd %xmm0, (%rax,%rcx,8)
97 addl $1, %edx
98 cmpl %r12d, %edx
99 jne .L19
100 .L17:
101 addl $1, %edi
102 addl %r8d, %esi
103 cmpl %r13d, %edi
104 je .L14
105 .L15:
106 movl $0, %edx
107 testl %r12d, %r12d
108 jg .L19
109 jmp .L17
110 .L14:
111 addq $8, %rsp
112 popq %rbx
113 popq %rbp
114 popq %r12
115 popq %r13
116 ret
117 transposeMatrix:
118 pushq %r12
119 pushq %rbp
120 pushq %rbx
121 movq %rdi, %rbx
122 movl %esi, %r12d
123 movl %edx, %ebp
124 movl %edx, %edi
125 call newMatrix
126 testl %r12d, %r12d
127 jle .L22
128 movl %ebp, %r11d
129 movl $0, %r8d
130 movl $0, %r10d
131 movl %r12d, %r9d
132 jmp .L23
133 .L24:
134 leal (%rdx,%r8), %esi
135 movslq %esi, %rsi
136 movq (%rbx,%rsi,8), %rdi
137 movslq %ecx, %rsi
138 movq %rdi, (%rax,%rsi,8)
139 addl $1, %edx
140 addl %r9d, %ecx
141 cmpl %ebp, %edx
142 jne .L24
143 .L25:
144 addl $1, %r10d
145 addl %r11d, %r8d
146 cmpl %r12d, %r10d
147 je .L22
148 .L23:
149 movl %r10d, %ecx
150 movl $0, %edx
151 testl %ebp, %ebp
152 jg .L24
153 jmp .L25
154 .L22:
155 popq %rbx
156 popq %rbp
157 popq %r12
158 ret
|
Letzte Änderung: 11.01.2007 | © Prof. Dr. Uwe Schmidt |