generated from admin/template
lab-01
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
// task 1
|
||||
// long quot(long a, long b) {
|
||||
// return a / b;
|
||||
// }
|
||||
|
||||
// task 2
|
||||
// long swap(unsigned short a) {
|
||||
// return ((a & 0x00FF) << 8 | (a >> 8));
|
||||
// }
|
||||
|
||||
// task 3
|
||||
// int main(void) {
|
||||
// int a = 10;
|
||||
// int b = 5;
|
||||
// int c = a / b;
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// task 4
|
||||
// int multi(int x){
|
||||
// return x * 2;
|
||||
// }
|
||||
|
||||
// int main(void) {
|
||||
// multi(2);
|
||||
// }
|
||||
// task 5
|
||||
// int main(void) {
|
||||
// int factorial = 1;
|
||||
// int x = 5;
|
||||
|
||||
// if (x < 0) {
|
||||
// printf("error\n");
|
||||
// } else {
|
||||
// for (int i = 1; i <= x; i++) {
|
||||
// factorial *= i;
|
||||
// }
|
||||
// printf("Факториал %d = %d\n", x, factorial);
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
@@ -0,0 +1,77 @@
|
||||
## task 1
|
||||
```C
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
long quot(long a, long b) {
|
||||
return a / b;
|
||||
}
|
||||
```
|
||||

|
||||
|
||||
## task 2
|
||||
```C
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
long swap(unsigned short a) {
|
||||
return ((a & 0x00FF) << 8 | (a >> 8));
|
||||
}
|
||||
```
|
||||

|
||||
|
||||
## task 3
|
||||
```C
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
int main(void) {
|
||||
int a = 10;
|
||||
int b = 5;
|
||||
int c = a / b;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||

|
||||
|
||||
## task 4
|
||||
```C
|
||||
int multi(int x){
|
||||
return x * 2;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
multi(2);
|
||||
}
|
||||
```
|
||||

|
||||
|
||||
## task 5
|
||||
```C
|
||||
int main(void) {
|
||||
int factorial = 1;
|
||||
int x = 5;
|
||||
|
||||
if (x < 0) {
|
||||
printf("error\n");
|
||||
} else {
|
||||
for (int i = 1; i <= x; i++) {
|
||||
factorial *= i;
|
||||
}
|
||||
printf("Факториал %d = %d\n", x, factorial);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||

|
||||
Объеснение:
|
||||
* -O0: Оптимизации отключены. Цикл выполняется как есть в машинном коде
|
||||
* -O1: Включаются базовые оптимизации, но компилятор еще не делает агрессивный анализ ироний со скалярным вычислением фиксированных циклов в единое число
|
||||
* -O2: Компилятор видит, что переменная x = 5 известна на этапе компиляции, границы цикла 1 <= 5 постоянны, а тело цикла делает простые математические операции.Он полностью вычисляет значение factorial = 120 во время компиляции и заменяет весь блок for на готовую константу 120 (loop unrolling / constant folding)
|
||||
* -O3: компилятор делает все те же действия, что и на -O2 — он полностью убирает цикл и заменяет его готовым числом 120
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 47 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 41 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 59 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 56 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 97 KiB |
Reference in New Issue
Block a user