Browse Source

将 kernel\common\math\pow.c 的求幂运算优化为快速幂

Eugene 2 years ago
parent
commit
618b612754
4 changed files with 18 additions and 5 deletions
  1. 2 1
      .gitignore
  2. 2 1
      .vscode/settings.json
  3. 13 2
      kernel/common/math/pow.c
  4. 1 1
      kernel/filesystem/fat32/fat_ent.c

+ 2 - 1
.gitignore

@@ -9,4 +9,5 @@ kernel/kernel
 serial_opt.txt
 user/sys_api_lib
 
-docs/_build
+docs/_build
+draft

+ 2 - 1
.vscode/settings.json

@@ -120,7 +120,8 @@
         "screen_manager.h": "c",
         "textui.h": "c",
         "atomic.h": "c",
-        "uart.h": "c"
+        "uart.h": "c",
+        "fat_ent.h": "c"
     },
     "C_Cpp.errorSquiggles": "Enabled",
     "esbonio.sphinx.confDir": ""

+ 13 - 2
kernel/common/math/pow.c

@@ -3,8 +3,19 @@
 
 int64_t pow(int64_t x, int y)
 {
+    if (y == 0)
+        return 1;
+    if (y == 1)
+        return x;
+    if (y == 2)
+        return x * x;
     int64_t res = 1;
-    for (int i = 0; i < y; ++i)
-        res *= x;
+    while (y != 0)
+    {
+        if (y & 1)
+            res *= x;
+        y >>= 1;
+        x *= x;
+    }
     return res;
 }

+ 1 - 1
kernel/filesystem/fat32/fat_ent.c

@@ -345,7 +345,7 @@ void fat32_fill_shortname(struct vfs_dir_entry_t *dEntry, struct fat32_Directory
         }
         else
         {
-            for(int j = 8;j<11;++j)
+            for (int j = 8; j < 11; ++j)
             {
                 target->DIR_Name[j] = 'a';
             }